简体   繁体   English

Drupal,hook_form_alter在节点中添加字段编辑/创建

[英]Drupal, hook_form_alter add field in node edit / create

I have a node and I need to populate a field programmatically, so here is what I do : 我有一个节点,我需要以编程方式填充字段,所以这是我的工作:

 $campaigns = $client->get_campaigns();
    $tab_campaign = array(""=>"Dernière newsletter");
    foreach ($campaigns->response as $camp){
      $tab_campaign[$camp->CampaignID] = $camp->Name;
    }


    $form['field_last_newsletter'] = array(
      '#type' => 'select',
      '#required' => true,
      '#options' => $tab_campaign,
      '#title' => 'Choisir la dernière newsletter',
    );
  }

This work, I have my select field populated but when I select one and click on save nothing is saved, if I come back to the edit page the select have the default value, what I am doing wrong ? 这项工作,我的选择字段已填充,但是当我选择一个并单击“保存”时,不保存任何内容,如果返回编辑页面,则选择具有默认值,我在做什么错了?

Thanks. 谢谢。

I think you're looking for a allowed_values_function setting for option fields. 我认为您正在寻找选项字段的allowed_values_function设置。 It is a perfect solution for fields with dynamic options. 对于具有动态选项的字段,这是一个完美的解决方案。

First, you need to change the current field settings to use the function to set the allowed values. 首先,您需要更改当前字段设置以使用该功能设置允许的值。 To do this, modify the field settings in features (if used): 为此,请修改功能中的字段设置(如果使用):

// Exported field_base: 'field_last_newsletter'
// my_module.features.field_base.inc
$field_bases['field_last_newsletter'] = array(
  // ....
  'settings' => array(
    'allowed_values' => array(),
    'allowed_values_function' => 'my_module_field_last_newsletter_allowed_values',
  ),
  // ....
);

If you do not use features, you can make this change by executing the PHP code or using hook_update_N 如果您不使用功能,则可以通过执行PHP代码或使用hook_update_N进行hook_update_N

/**
 * Implements hook_update_N().
 * Update the field_last_newsletter field settings to use callback for allowed_values.
 */
function my_module_update_N(&$sandbox) {
  // get default status for field using machine name of field
  $default_stats_field = field_info_field('field_last_newsletter');
  // unset the allowed values
  $default_stats_field['settings']['allowed_values'] = '';
  // function name that provides array of values
  $default_stats_field['settings']['allowed_values_function'] = 'my_module_field_last_newsletter_allowed_values';
  // update value with new value.
  field_update_field($default_stats_field);
}

After saving the new settings, you need to implement the callback function for the dynamic allowed values. 保存新设置后,您需要为动态允许值实现回调函数。

/**
 * Allowed values callback for field_last_newsletter.
 */
function my_module_field_last_newsletter_allowed_values() {
  // ...
  $campaigns = $client->get_campaigns();
  $tab_campaign = array(""=>"Dernière newsletter");
  foreach ($campaigns->response as $camp){
    $tab_campaign[$camp->CampaignID] = $camp->Name;
  }

  return $tab_campaign;
}

The issue here is that you are defining a field in code and since this was not created through the UI a database table to store it's value was not created. 这里的问题是,您正在代码中定义一个字段,并且由于未通过UI创建该字段,因此未创建用于存储其值的数据库表。 I would suggest you create this field through the UI (/admin/structure/types/manage/xxxxx/fields) and in your hook_form_alter you just change the #options array to populate it. 我建议您通过UI(/ admin / structure / types / manage / xxxxx / fields)创建此字段,然后在hook_form_alter中,只需更改#options数组即可填充它。 This way a database table for your data will be created and Drupal will handle saving the data, populating the saved value, etc... 这样,将为您的数据创建一个数据库表,Drupal将处理保存数据,填充保存的值等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM