0

Im using field_create_instance to create all my defined fields, and use the correct field types and widget types and such, but Im not sure how to set the widget options.

In my use case, Im using the colorfield module and am trying to save the default options for the widget so the content type automagically uses farbtastic but I'm not the greatest drupal dev yet and am not sure how to do that.

Right now, my instance looks like:

'fill_color' => array(
    'field_name' => $fields['field_map_fill_color']['field_name'],
    'label' => 'The inactive state colors',
    'bundle' => 'nji_map',
    'entity_type' => 'node',
    'widget'      => array(
        'type'    => 'colorfield_unified_textfield'
    ),
    'description' => 'This is the color of the inactive color of the state.',

),

So that sets the field to use the colorfield field widget alright, but I need to set it up so it also uses the farbtastic option. I took a look at the module and theres a hook called hook_field_info with this option:

    'instance_settings' => array(
        'colorfield_enable_colorpicker' => TRUE,
        'colorfield_colorpicker_type' => 'farbtastic',
    ),

I looked at the drupal field_create_instance hook and i feel like it needs to go into widget->type right? Well I cant seem to get it to work.

Ive also tried:

    'background_color' => array(
        'field_name' => $fields['field_map_background_color']['field_name'],
        'label' => 'The background color',
        'bundle' => 'nji_map',
        'entity_type' => 'node',
        'description' => 'The background color is the color between.',
        'widget'      => array(
            'type'    => 'colorfield_unified_textfield',
        ),

        'instance_settings' => array(
            'colorfield_enable_colorpicker' => TRUE,
            'colorfield_colorpicker_type' => 'farbtastic',
        ),

    ),

2 Answers 2

0

I think it should look like this :

    'field_instance' => array(
  'bundle' => 'fo',
  'default_formatter' => 'text_default',
  'default_value' => NULL,
  'default_widget' => 'text_textfield',
  'deleted' => '0',
  'description' => 'blah blah blah.',
  'display' => array(
    'default' => array(
      'label' => 'above',
      'module' => 'text',
      'settings' => array(),
      'type' => 'text_default',
      'weight' => 0,
    ),
  ),
  'entity_type' => 'taxonomy_term',
  'field_name' => 'test',
  'instance_settings' => array(
    'text_processing' => 0,
  ),
  'label' => ' test',
  'required' => FALSE,
  'settings' => array(
    'max_length' => 255,
    'text_processing' => 0,
    'user_register_form' => FALSE,
  ),
  'title' => 'test',
  'widget' => array(
    'module' => 'text',
    'settings' => array(
      'size' => 60,
    ),
    'type' => 'text_textfield',
    'weight' => 31,
  ),
),
1
  • yup features is 1 way to go. another way is to build the field type in the admin ui and use the drupal api to export the field to code by hand ... steindom.com/articles/…
    – tenken
    Commented Dec 4, 2013 at 18:18
0

In a moment of clarity I checked out the "features" plugin and realized that I could make the proper configurations with the manage fields UI and then export the code I needed to produce:

$field_instances['node-nji_map-field_map_background_color'] = array(
    'bundle' => 'nji_map',
    'default_value' => NULL,
    'deleted' => 0,
    'description' => 'The background color is the color between.',
    'display' => array(
      'default' => array(
        'label' => 'above',
        'module' => 'colorfield',
        'settings' => array(
          'height' => 20,
          'width' => 20,
        ),
        'type' => 'colorfield_color_swatch',
        'weight' => 5,
      ),
      'teaser' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => 0,
      ),
    ),
    'entity_type' => 'node',
    'field_name' => 'field_map_background_color',
    'instance_settings' => array(
      'colorfield_colorpicker_type' => 'farbtastic',
      'colorfield_enable_colorpicker' => TRUE,
    ),
    'label' => 'The background color',
    'required' => 0,
    'settings' => array(
      'colorfield_colorpicker_type' => 'farbtastic',
      'colorfield_enable_colorpicker' => TRUE,
      'user_register_form' => FALSE,
    ),
    'widget' => array(
      'active' => 1,
      'module' => 'colorfield',
      'settings' => array(
        'colorfield_options' => array(
          'colorfield_colorpicker_type' => 'farbtastic',
          'colorfield_enable_colorpicker' => 1,
        ),
      ),
      'type' => 'colorfield_unified_textfield',
      'weight' => 35,
    ),
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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