简体   繁体   中英

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. 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

/**
 * 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. 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. This way a database table for your data will be created and Drupal will handle saving the data, populating the saved value, etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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