I'm building a custom Drupal 8 module which will make changes to a Webform based on the derivative plugin ID. I'm able to get the derivative plugin ID from within the build()
function of the base block using $this->getDerivativeId() but It's not clear on how I can call getDerivativeId()
from mycustommodule.module
where I'm using hook_form_alter
.
Any help would be greatly appreciated. Here is what I've tried so far without success:
I first tired $this->getDerivativeId()
directly from hook_form_alter
but I get an error
"Using $this when not in object context"
Then I tried creating a new function in my base block class called "getCurrentBlockId()" where I would try to return the getDerivativeId()
value but it forced me to make the function static and once I did that I could no longer use $this->getDerivativeId()
.
$block_id = \Drupal\transcode_profile\Plugin\Block\TranscodeBlockDerivative::getCurrentBlockId();
I also tried to creating a constructor to save the derivative block ID to a variable which would be accessible across the scope of my module but failed at that as well.
In summary, I can use $this->getDerivativId()
at... \Drupal\transcode_profile\Plugin\Block\TranscodeBlockDerivative
but need to use at... \Drupal\transcode_profile\transcode_profile.module
and not sure how to do it.
Thanks in advance!
Not knowing much about your setup, but I believe you can achieve this by instantiating the plugin in question because you cannot call non-static methods statically and you can't use $this
in non-object mode respectively.
Assuming you are after a block derivative you could try:
/* @var $manager \Drupal\Core\Block\BlockManagerInterface */
$manager = \Drupal::service('plugin.manager.block');
/* @var $block \Drupal\Core\Block\BlockPluginInterface */
$block = $manager->createInstance($block_id);
$derivative_id = $block->getDerivativeId();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.