【问题标题】:drupal alter node edit formdrupal alter node 编辑表单
【发布时间】:2023-03-10 17:01:01
【问题描述】:

如何在创作信息发布选项部分下方的节点编辑表单中添加自定义配置区域?

【问题讨论】:

    标签: drupal-7 hook-form-alter


    【解决方案1】:

    您可以使用hook_form_FORM_ID_alter()。 下面的例子:

    function my_module_form_node_form_alter(&$form, $form_state) {
      // if you are targeting a specific content type then 
      // you can access the type:
      $type = $form['#node']->type;
      // Then
      if ($type == 'my_content_type') {
      //  use a contact settings for the sake of this example
       $form['contact'] = array(
        '#type' => 'fieldset', 
        '#title' => t('Contact settings'), 
        '#weight' => 100, 
        '#collapsible' => TRUE, 
        '#collapsed' => FALSE,
       );
       // add simple checkbox to the field set
       $form['contact']['approve'] = array(
         '#type' =>'checkbox', 
         '#title' => t('Contact me'),
       );
      } 
    }
    

    现在用于存储数据我鼓励您查看examples 项目;它有许多带有大量文档的代码示例。 此外,请查看the Form API,了解有关不同类型表单元素的更多信息。 希望这会有所帮助。

    【讨论】:

    • 非常感谢您的意见,但我的意思类似于下面的回答:
    • 你的答案和我提供的一样。
    • 非常相似,但配置区域的位置是我想要实现的。您的代码毫无疑问地工作,但我试图实现不同的布局
    • 只是对上面完美工作代码的补充说明:您可以通过使用如下的保护条件来避免条件嵌套:if ($type != 'my_content_type') { return; }。以下代码现在不是嵌套的(有意),这使您的代码更具可读性。
    【解决方案2】:

    以下代码生成附图中的最后一个菜单:

        $form['barclays_epdq'] = array(
            '#type' => 'fieldset',
            '#access' => TRUE,
            '#title' => 'Barclays ePDQ',
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
            '#group' => 'additional_settings',
            '#weight' => 100,
            'barclays_epdq_active' => array(
                '#type' => 'checkbox',
                '#title' => 'Enable this webform to send users to your Barclays ePDQ store to make a payment',
                '#default_value' => $active_state,
            ),
        );
    

    ps:表单在 hook_form_alter 中

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2011-04-03
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      相关资源
      最近更新 更多