Configuring fields

Primary tabs

Custom fields also have custom configuration, and this configuration can be changed through many modules. How is this done?

  • 2 tables keep the relevant information: field_config and field_config_instance
  • field_config notes, that a field exists, and certain basic and default information about it (one row for every field)
  • field_config_instance notes, that the field has been related to an entity (one row for every relation)

A look at field_config:

Every field has an id (an integer), a machine name, a type and a relation to a module.

Let's look at the field for tags:

  • id = 3
  • name = field_tags
  • type = taxonomy_term_reference
  • module = taxonomy

Let's look at field_config_instance too:

Looking at field_tags again is interesting:

  • articles have tags

Both tables have a very important column called data. This is where most of the actual configuration is stored. We won't look directly at that field a lot, but mostly look at manipulating it instead. First let's look at how the above rows were created in the install profile:

  $field = array(
    'field_name' => 'field_tags',
    'type' => 'taxonomy_term_reference',
    // Set cardinality to unlimited for tagging.
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => 'tags',
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);

The array called settings feeds the column called data. This is what data contains afterwards (I've shortened it a little):

a:6:{s:8:"settings";a:1:{s:14:"allowed_values";a:1:{i:0;a:2:{s:10:"vocabulary";s:4:"tags";s:6:"parent";i:0;}}}...}

Let's format that a bit:

a:6:{
  s:8:"settings";
  a:1:{
    s:14:"allowed_values";
    a:1:{
      i:0;
      a:2:{
        s:10:"vocabulary";
        s:4:"tags";
        s:6:"parent";
        i:0;
      }
    }
  }
  ...
}

Hopefully you can see the similarity between the 2 formats.

  • a = array
  • s = string
  • i = integer
  • i:0 = this integer has the value 0
  • s:8:"settings" = this string is 8 characters long and has the value settings

Assume I want to add some configuration to the term reference, how do I do that?

  • load the configuration for our field
    • $field = field_info_field('field_tags');
  • update the configuration by adding the new configuration (which will be an array structured like allowed_values above*)
    • $new_configuration = array( NEW CONFIGURATION );
    • $field['settings'] += $new_configuration;
  • save the configuration
    • field_update_field($field);

* Details about my new configuration:

    $new_configuration = array(
      'new_conf' => array(
        array(
          'newstate' => 'something',
        ),
      ),
    );

In case I want to do something a little more complicated, for a lot of fields, this might be the place to begin; it does the same but in a way that's easier to modify.

  • load the configuration for all fields
    • $fields = field_read_fields();
  • narrow it down to looking only at the configuration for tags
    • foreach ($fields as $field_name => $field) { if ($field_name == 'field_tags') { STUFFBELOW } }
  • update the configuration by adding the new configuration (which will be an array structured like allowed_values above)
    • $new_configuration = array( NEW CONFIGURATION );
    • $field['settings'] += $new_configuration;
  • save the configuration
    • field_update_field($field);