I have created a custom field list, but of all documentation I have read, I can't find a clear example on how to add data on the list.

The code so far is this:

/**
 * Adds field.
 */
function _my_installed_fields() {
  $t = get_t();
  return array(
    'my_dropdown_list' => array(
      'field_name' => 'my_dropdown_list',
      'label' => $t('Select an item'),
      'type' => 'list_text'
    )
  );
}
/**
 * Adds instances.
 */
function _my_installed_instances() {
  $t = get_t();
  return array(
    'my_dropdown_list' => array(
      'field_name' => 'my_dropdown_list',
      'type' => 'list_text',
      'label' => $t('Select an item'),
      'widget' => array(
        'type' => 'options_select'
      ),
      'display' => array(
        'example_node_list' => array(
          'label' => $t('Select n item'),
          'type' => 'list_text'
        )
      )
    )
  );
}

I need to let only one value to be selected and saved.

up vote 2 down vote accepted

Try to add cardinality and settings parameters:

  function _my_installed_fields() {
    $t = get_t();
    return array(
      'my_dropdown_list' => array(
        'field_name' => 'my_dropdown_list',
        'label' => $t('Select an item'),
        'type' => 'list_text',
        'cardinality' => 1,
        'settings' => array(
          'allowed_values' => array(
            1 => 'One',
            2 => 'Two',
            3 => 'Three',
          ),
        ),
      )
    );
  }
  • That worked, I have the 3 example options, but I still have the "none" option too. How to I delete that option? Where did you find those information? I would like to seek the proper documentation – Terix Apr 12 '16 at 15:46
  • 1
    I have just used search by 'type' => 'list_text' and checked the examples. As an option you can use Features module: add field manually from admin UI, save this field in feature and export feature, in generated Features module you will find properly code of your field. – skorzh Apr 12 '16 at 18:25
  • That you that helped, I want forward with my project, do you mind to give a look to my next issue? drupal.stackexchange.com/questions/197793/… – Terix Apr 12 '16 at 18:36

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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