【问题标题】:D7: Field API Field not saved when using hook_form_alterD7:使用 hook_form_alter 时未保存字段 API 字段
【发布时间】:2017-05-21 09:05:18
【问题描述】:

我使用 Field API 创建了以下字段(作为示例),效果很好。由于我想添加 autocomplete 功能(已经工作,此处未显示)以及从 $_POST 变量设置 默认值,我开始使用 @987654322 更改字段@。

更改字段就像一个魅力,但是字段将不再保存到节点,甚至出现在 节点的不同位置编辑表格

<?php
  function trian_portal_enable() {
    // create assigned License field
    if (!field_info_field('field_assigned_license')){
      $field = array(
        'field_name' => 'field_assigned_license',
        'type' => 'text',
        'cardinality' => 1,
      );
      field_create_field($field);

      $instance = array(
          'field_name' => 'field_assigned_license',
          'entity_type' => 'node',
          'label' => t('Assigned License'),
          'bundle' => 'kunden_download',
          'description' => t('Enter License assigned to this download'),
          'required' => FALSE,
          'settings' => array(
             // Here you inform either or not you want this field showing up on the user profile edit form.
              'kunden_download_node_form' => 1,
          ),
          'widget' => array(
              'type' => 'textfield',
          ),
        );
        field_create_instance($instance);
    }
  }

  function trian_portal_form_alter(&$form, $form_state, $form_id) {


    if ($form_id == 'kunden_download_node_form') {


      $form['field_assigned_license'] = array(
        '#title' => t('Assigned Licence'),
        '#type' => 'textfield',
        '#default_value' => ($_REQUEST['lid']) ? $_REQUEST['lid']: '',
        '#required' => ($_REQUEST['lid']) ? 1:0,
      );

    }
  }
?>

【问题讨论】:

    标签: php drupal drupal-7 form-api drupal-field-api


    【解决方案1】:

    很棒的#drupal chanel 给了我答案(感谢@graper =))

    错在:

    $form['field_assigned_license'] = array(
            '#title' => t('Assigned Licence'),
            '#type' => 'textfield',
            '#default_value' => ($_REQUEST['lid']) ? $_REQUEST['lid']: '',
            '#required' => ($_REQUEST['lid']) ? 1:0,
          );
    

    基本上会覆盖$form['field_assigned_license'] 中保存的所有内容。正确的方法是只覆盖我想要的某些参数,例如$form['field_assigned_customer']['und'][0]['value']['#default_value'] 或将原始数组与调整合并。

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 2011-04-20
      • 2011-07-20
      • 2021-08-24
      • 2021-10-26
      相关资源
      最近更新 更多