4

This is the code I actually have:

$instance = array(
    'field_name' => FIELD_PROJECT_TAGS,
    'entity_type' => 'node',
    'label' => $vocabulary->name,
    'bundle' => 'portfolio_project',
    'required' => true,
    'widget' => array(
        'type' => 'taxonomy_autocomplete',
        'behaviors' => array(
            'multiple values' => FIELD_CARDINALITY_UNLIMITED,
        ),
    ),
    'display' => array(
        'default' => array(
            'type' => 'taxonomy_term_reference_link',
            'weight' => 10,
        ),
        'teaser' => array(
            'type' => 'taxonomy_term_reference_link',
            'weight' => 10,
        ),
    )
);
field_create_instance($instance);

What's wrong with this? Why does the portfolio module allways install this field with allowing only one value?

Hope somebody can help. I'm bit frustrated.

2

Hopefully this will prove useful: To create a field that allows multiple values, you set the cardinality to -1 when using field_create_field. Then all instances will allow multiple values (provided the widget supports that).

Example:

field_create_field([
  'cardinality' => -1,
  'field_name' => 'field_unlimited',
  'type' => 'special_content',
  'entity_types' => ['node'],
]);
1
  • This is good, but actually rather than using -1, the constant FIELD_CARDINALITY_UNLIMITED should be used: 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    – Jaypan
    Sep 17 '18 at 10:21
1

As reported in Field API, the array indexes used for the widget array are the following ones:

  • type (string) The type of the widget, such as text_textfield. Widget types are defined by modules that implement hook_field_widget_info().
  • settings (array) A sub-array of key/value pairs of widget-type-specific settings. Each field widget type module defines and documents its own widget settings.
  • weight (float) The weight of the widget relative to the other elements in entity edit forms.
  • module (string, read-only) The name of the module that implements the widget type.

It is hook_field_widget_info() that defines the behaviors of the field widgets.

function hook_field_widget_info() {
  return array(
    'text_textfield' => array(
      'label' => t('Text field'), 
      'field types' => array('text'), 
      'settings' => array('size' => 60), 
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT, 
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ), 
    'text_textarea' => array(
      'label' => t('Text area (multiple rows)'), 
      'field types' => array('text_long'), 
      'settings' => array('rows' => 5), 
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT, 
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ), 
    'text_textarea_with_summary' => array(
      'label' => t('Text area with a summary'), 
      'field types' => array('text_with_summary'), 
      'settings' => array(
        'rows' => 20,
        'summary_rows' => 5,
      ), 
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT, 
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

The module you are writing should implement that hook, in order to allow the field to use multiple values.

The taxonomy widget allows to enter multiple values by entering them separated with a comma; if you are expecting the widget to appear more than one time on the page, then it is not how that widget works. Other widgets can probably use more form fields to contain multiple values.

3
  • I think you misunderstand my question. Your answer is great and there are a few things that will help me in the future. But ... the taxonomy plugin allready provide a taxonomy_autocomplete widget which I use. I wonder about the constant FIELD_BEHAVIOR_CUSTOM which means that the widget is allowed to deal with one or more values. Look in the API for taxonomy_field_widget_info(). What am I doing wrong? Thanks in Advanced again!
    – Johnny
    Mar 30 '11 at 8:14
  • @Johnny: The behaviors array index is not passed to field_create_instance(), as reported in the documentation I linked to.
    – apaderno
    Mar 30 '11 at 8:42
  • Okay, apart from that (thats my mistake in understanding the architecture of Drupal): The default taxonomy_autocomplete widget seems to dissallow multiple values. But why?
    – Johnny
    Mar 30 '11 at 8:55

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.