Skip to content

Instantly share code, notes, and snippets.

@bjmiller121
Last active September 29, 2015 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjmiller121/3909ce232b8e0d4a9b89 to your computer and use it in GitHub Desktop.
Save bjmiller121/3909ce232b8e0d4a9b89 to your computer and use it in GitHub Desktop.
Creating multiple fields and field instances in an update hook
// Define new fields
$new_fields = array(
'field_NAME_OF_FIELD' => array(
'entity_type' => 'node',
'bundle' => 'page',
'field_definition' => array(
'type' => 'text',
// Other field details here. Exact keys depend on the type of field being added.
// Best thing to do is create one on your local exactly how you want it (through
// the UI), and then running field_read_field() and copying data below.
),
'instance_definition' => array(
'label' => 'Field Label',
// Here too, there are many keys. Exact options depend on the field.
// You should do the same as above (create through the UI on your local)
// then call field_read_instance() to get the details and copy them in
// here.
// NOTE: Make sure display is hidden if you don't want it rendered.
),
),
'field_NAME_OF_FIELD' => array(
'entity_type' => 'node',
'bundle' => 'page',
'field_definition' => array(
'type' => 'text',
// Other field details here. Exact keys depend on the type of field being added.
// Best thing to do is create one on your local exactly how you want it (through
// the UI), and then running field_read_field() and copying data below.
),
'instance_definition' => array(
'label' => 'Field Label',
// Here too, there are many keys. Exact options depend on the field.
// You should do the same as above (create through the UI on your local)
// then call field_read_instance() to get the details and copy them in
// here.
// NOTE: Make sure display is hidden if you don't want it rendered.
),
),
);
foreach ($new_fields as $field_name => $field_settings) {
$entity_type = $field_settings['entity_type'];
$bundle = $field_settings['bundle'];
$field_settings['field_definition']['field_name'] = $field_name;
$field_settings['instance_definition']['field_name'] = $field_name;
// Add the new field if it doesn't already exist
if (!$field = field_read_field($field_name)) {
try {
// We save of the result of field_create_field() for use later. See below.
$field = field_create_field($field_settings['field_definition']);
print('Created the "' . $field_name . '" field.');
}
catch (FieldException $e) {
throw new DrupalUpdateException('Error creating field "field_form_name" with message: ' . $e->getMessage());
}
}
// Don't bother if the field already exists.
if (!field_read_instance($entity_type, $field_name, $bundle)) {
// Apply bundle-specific settings.
$field_settings['instance_definition']['entity_type'] = $entity_type;
$field_settings['instance_definition']['bundle'] = $bundle;
try {
field_create_instance($field_settings['instance_definition']);
print('Created a "' . $field_name . '" field instance for the "' . $bundle . '" ' . $entity_type . ' bundle.');
}
catch (FieldException $e) {
throw new DrupalUpdateException('Error creating field instance for the "' . $bundle . '" bundle: ' . $e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment