【问题标题】:Drupal hook is not applied to the websiteDrupal hook 未应用于网站
【发布时间】:2022-06-12 07:04:44
【问题描述】:

我正在与我的请求相关的网络表单中编写一个自定义钩子,它授予对在电子邮件字段中输入的电子邮件地址(由请求者)访问无线电字段(批准、拒绝、转发)的权限。

整个想法是:

请求者提交表单 -> 主管将收到一封包含链接的电子邮件,并选择是否要批准、拒绝或通过编辑表单将其传递给主管

我选择了 webform_alter 钩子来实现这个

function ach_request_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

if($form_id == 'ach_request') { //form's id

if($form['submitted_to']['#value'] === \Drupal::currentUser()->getEmail) { //if the value of the email element is equal to the email address of the current user (director);

$form['approval_state_']['#access'] = TRUE; //the radios element would be visible to the director with the same email address

}

}

}

然后我在终端中使用了 drush cr 但选择元素仍然不可见。我的代码有什么问题吗?还是我还需要更改 UI 上的任何内容?

提前谢谢你。

【问题讨论】:

    标签: php drupal hook drupal-webform


    【解决方案1】:

    当您实现挂钩时,函数名称的“挂钩”部分应替换为它所在的模块或主题名称。
    例如, 如果您尝试实现hook_form_alter,并且您已将您的函数放在名为“mycoolmodule”的自定义模块中,那么您的函数名称将是...

    /**
    * Implements hook_form_alter in a module called mycoolmodule.
    */
    function mycoolmodule_form_alter($form, $form_state, $form_id) {
      // do something with the form here
    }
    

    Understanding hooks page on drupal.org

    【讨论】:

    • 其实我在 webform.api.php 的 hook_webform_submission_form_alter 中添加了我的代码,因为我使用的是 webform。
    • 为了实现我想要做的事情,您还有其他推荐的 webform 钩子吗?我最初使用了 hook_webform_element_alter,但没有应用更改。
    • 你不想在 contrib 模块文件中放入任何代码,而且无论如何,webform.api.php 无论如何都不会使用,它只是一个显示可用功能的文件,该文件中的函数永远不会被调用。要实现钩子,您可以将函数放在自定义模块或自定义主题的 .theme 文件中
    • 有一个docs page dedicated to webform。有一个特定的 webform 钩子用于设置访问 hook_webform_element_access,这可能是您想要使用的。它在webform.api.php here 中进行了概述
    • 我刚刚了解到(在问你这个问题之后)webform.api.php 什么都不做,因为它只是一个示例。所以我在我创建的自定义文件夹中创建了一个新模块,但尚未应用更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多