Retrieving a list from Content Type field in Drupal 7

橙三吉。 提交于 2021-02-04 19:48:45

问题


I'm sort of new to Drupal 7.

I am using Drupal Form API and I need to use a drop-down showing a list of states via the mymodule_forms hook.

$form['work_state'] = array(
  '#title' => t('Work State'),
  '#type' => 'select',
  ...
);

I already have a list of states defined in a Content Type field.

How would one go around loading the Content Type (ie: forms_stipend) and retrieving the field (ie: field_states). After that is retrieved, I can start populating the available list of states into the code shown above.

Thanks in advance for your help as they're always appreciated!


回答1:


Assuming your field is a list type, you can grab the allowed values from the field using the field_info_field() function:

$info = field_info_field('field_states');
$options = $info['settings']['allowed_values'];

$form['work_state'] = array(
  '#title' => t('Work State'),
  '#type' => 'select',
  '#options' => $options
);


来源:https://stackoverflow.com/questions/10841842/retrieving-a-list-from-content-type-field-in-drupal-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!