1

I've been searching for hours, but can't seem to find a solution to this. Now, I'm able to create a plain old text field as such as this:

In moduleA.install:

if (!field_info_field('field_myfield')) {
  $field = array(
    'field_name' => 'field_myfield',
    'type' => 'text',
  );
  field_create_field($field);
}

Then, in moduleB.install, I add that field to a specific taxonomy bundle:

$instance = array(
  'field_name' => 'field_myfield',
  'entity_type' => 'taxonomy_term',
  'label' => 'MyField',
  'bundle' => 'new_terms',
  'widget' => array(
      'type' => 'text',
  ),
);
field_create_instance($instance);

What I would like is the field it creates to be a select list, instead of just a text field. I've been looking around, trying to figure it out (using "list-select" in place of type), but any time I've deviated from what's above, I get an error on field_create_instance when installing moduleB. I realize it's probably (hopefully) something as simple as knowing the proper widget name, but I either can't seem to find it, or am searching for the wrong thing.

0

The field type would be list_text (as defined in list_field_info(), and the widget type is options_select, as defined in options_field_widget_info().

Kind of tricky to track down, but this little snippet should help in future:

$entity_type = 'node';
$field_name = 'field_name';
$bundle_name = 'article';

$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">". $output .'</textarea>');

It'll get you a complete field and instance definition of an existing field, ready to use in code.

  • Would I still use "text" or "list-text" as the type when creating the field? – UnsettlingTrend Oct 21 '13 at 17:54
  • Sorry, I completely mis-understood what you were asking. I've updated the answer – Clive Oct 21 '13 at 18:03
  • 1
    Oh! Actually, the way you had it before was fine; I figured out the other bit after some testing. Thank you! – UnsettlingTrend Oct 21 '13 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.