0

Is it possible to add the field field_tags in mymodule_install() like the body field? I added a body field with node_add_body_field. I can see the field in field_config_instance table. Is there a posibility to add field_tags in hook_install() without doing it in the GUI?

Is it done by field_create_field?

if (!field_info_field('my_field')) {
  field_info_cache_clear();

  $field = array(
    'field_name' => 'my_field',
    'type' => 'custom_field_type',
    'cardinality' => 1
  );
  field_create_field($field);
}

But how to set the values for a default article or gui custom like tags field?

  • Haha, what a useful answer. Yeah it's although about field_create_field. But i don't want a custom field. I search for something equivalent like GUI's add 'existing field'. EXISTING. Is it the right way create a new field when i want to have an existing field like field_tags? – surfi Dec 3 '13 at 9:04
  • I'm not sure I would to be honest. That field isn't guaranteed to always be there as it's created by the Standard installation profile. I'd either create my own tags field, or use a combination of field_create_instance and Field inspector – Chapabu Dec 3 '13 at 9:08
2

Yeah, i know according to strict forum rules it's not fine to answer your own question :-) But anyway ...

I not realized, that the default fields are stored in table field_config and just a instance for a node in table field_config_instance.

I added the following lines to mymodule_install() of my module:

$instance = array(
  'field_name' => 'field_tags', 
  'entity_type' => 'node', 
  'label' => 'Tags',
  'bundle' => 'mynode',
  'widget' => array(
    'type' => 'textfield',
  ), 
);

I don't need field_create_field() because the field_tags already exists.

And i added a field to the custom node form:

function mymodule_node_form($form, &$form_state, $node) {
...
   $form['field_tags'][LANGUAGE_NONE] = array(
       // '#entity_type' => 'node',
      '#type' => 'textfield',
      '#title' => t('Tags'),
       // '#autocomplete_path' => 'taxonomy/autocomplete/field_tags',
      '#field_name' => 'field_tags',
       // '#bundle' => 'mymodule',
      '#element_validate' => array(
                                 0 => 'taxonomy_autocomplete_validate',
                             ),
       //    '#after_build' => array(
       //                        0 => 'field_form_element_after_build', 
       //                      ),
      '#field_parents' => array(),
      '#language' => LANGUAGE_NONE,
      '#columns' => array(
                        0 => 'tid',
                    ),
      '#parents' => array(
                       0 => 'field_tags',
                       1 => LANGUAGE_NONE,
                    ),
   );
...
}

I tried to find out the obligate values. I'm not sure if it's wise to add more values?! I just realized that some values are not nescessary (the commented ones).

  • Hi Sufri - this isn't a forum (it's a Q+A site), and answering your own question is very much encouraged; thank you for doing so! – Clive Dec 3 '13 at 16:42

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