0

I have programmatically created a few content types and fields for said content types in a module I'm building, but I need to add classes to said fields.

So if I have a text field called "background_color", I would like to add a class to it called "color-picker".

Im added the instance of the field to my bundle like:

...
    'background_color' => array(
        'field_name' => $fields['field_background_color']['field_name'],
        'label' => 'The stroke color',
        'bundle' => 'nji_map',
        'entity_type' => 'node',
        'attributes' =>array('class'=>array('minicolors')),
        'widget'      => array(
            'type'    => 'text_textfield',
        ),
        'display' => array(
            'default' => array(
            'label' => 'above',
            'type' => 'text_textfield',
            ),
        ),
        'description' => 'The background color is the color between.',

    )....

Edit - Here is more of my code (node the full code snippet because there are too many fields)..

        $fields = array(
        'field_map_background_color' => array(
            'field_name' => 'field_map_background_color',
            'type' => 'text'
        ),...

$instances =  array(
    'background_color' => array(
        'field_name' => $fields['field_map_background_color']['field_name'],
        'label' => 'The background color',
        'bundle' => 'nji_map',
        'entity_type' => 'node',
        'attributes' =>array('class'=>array('minicolors')),
        'settings' => array(
            'prefix' => '<div class = "minicolors">',
            'suffix' => '</div>',
        ),
        'widget'      => array(
            'type'    => 'text_textfield',
        ),
        'display' => array(
            'default' => array(
            'label' => 'above',
            'type' => 'text_textfield',
            ),
        ),
        'description' => 'The background color is the color between.',

    ), ....

  foreach ($fields as $field) {
    field_create_field($field);
  }

  foreach ($instances as $instance) {
    field_create_instance($instance);
  }
0

Do something like this to wrap this field in a DIV class --

...
    'background_color' => array(
        'field_name' => $fields['field_background_color']['field_name'],
        'label' => 'The stroke color',
        'bundle' => 'nji_map',
        'entity_type' => 'node',
        'attributes' =>array('class'=>array('minicolors')),
        'widget'      => array(
            'type'    => 'text_textfield',
        ),
        'settings' => array(
          'prefix' => '<div class = "my_field_div_class">',
          'suffix' => '</div>',
        ),
        'display' => array(
            'default' => array(
            'label' => 'above',
            'type' => 'text_textfield',
            ),
        ),
        'description' => 'The background color is the color between.',

    )....

You would notice I have added settings along with other properties of the instance, where we can add prefix, sufix, min, max etc. I hope it works..

  • Hmm, that doesnt seem to be working. Neither the class nor settings seem to do anything. I just checked the fields via the developer inspector and the field isnt being wrapped nor is it taking the class. – rugbert Jul 1 '13 at 18:32
  • @rugbert It's working in similar fashion for me – RajeevK Jul 1 '13 at 18:46
  • HMM. Are you also creating these fields via field_create_instance()? – rugbert Jul 1 '13 at 19:09
  • @rugbert Yes, can you please post full code ? – RajeevK Jul 1 '13 at 19:19
  • @rugbert - It should work. I don't see any issue in it. I have also done in same way & settings works for me.. – RajeevK Jul 2 '13 at 7:04

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.