【问题标题】:How To Get Current Derivative Block ID From Within hook_form_alter Function in Drupal 8?如何从 Drupal 8 中的 hook_form_alter 函数中获取当前衍生块 ID?
【发布时间】:2020-06-03 22:00:54
【问题描述】:

我正在构建一个自定义 Drupal 8 模块,它将根据衍生插件 ID 对 Webform 进行更改。我可以使用 $this->getDerivativeId() 从基本块的 build() 函数中获取派生插件 ID,但不清楚如何从 @ 调用 getDerivativeId() 987654323@ 我使用的是hook_form_alter

任何帮助将不胜感激。这是我迄今为止尝试过的但没有成功的方法:

  1. 我首先直接从hook_form_alter 厌倦了$this->getDerivativeId(),但我得到了一个错误

    “不在对象上下文中使用 $this”

  2. 然后我尝试在我的基块类中创建一个名为“getCurrentBlockId()”的新函数,我将尝试返回 getDerivativeId() 值,但它迫使我将函数设为静态,一旦我这样做了,我就不能再使用$this->getDerivativeId()

    $block_id = \Drupal\transcode_profile\Plugin\Block\TranscodeBlockDerivative::getCurrentBlockId();
    
  3. 我还尝试创建一个构造函数来将派生块 ID 保存到一个变量,该变量可以在我的模块范围内访问,但也失败了。

总之,我可以使用$this->getDerivativId()... \Drupal\transcode_profile\Plugin\Block\TranscodeBlockDerivative 但需要使用... \Drupal\transcode_profile\transcode_profile.module 不知道该怎么做。

提前致谢!

【问题讨论】:

    标签: php oop drupal block drupal-8


    【解决方案1】:

    不太了解您的设置,但我相信您可以通过实例化相关插件来实现这一点,因为您不能静态调用非静态方法,也不能分别在非对象模式下使用 $this

    假设你是在一个块衍生之后,你可以尝试:

    /* @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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多