You are here

function field_tools_import_fields in Field tools 7

Import fields to a bundle.

Parameters

string $entity_type: The entity type of the target bundle

string $bundle_name: The name of the target bundle

array $fields: An array of fields to be imported, as obtained from field_tools_get_field_code().

1 call to field_tools_import_fields()
field_tools_bundle_import_form_submit in ./field_tools.admin.inc
Submit handler for the import form.

File

./field_tools.admin.inc, line 988
Contains admin callbacks for the Field tools module.

Code

function field_tools_import_fields($entity_type, $bundle_name, $fields) {
  if (count($fields)) {
    foreach ($fields as $field_name => $item) {
      $field_type = field_info_field_types($item['field']['type']);
      if (!$field_type) {
        drupal_set_message(t('Unknown field type %field_type. Field %field_name has not been created.', array(
          '%field_type' => $item['field']['type'],
          '%field_name' => $field_name,
        )), 'error');
        continue;
      }
      $field = field_info_field($field_name);
      if (!$field) {
        field_create_field($item['field']);
        drupal_set_message(t('Field %field has been created.', array(
          '%field' => $field_name,
        )));
      }
      $instance = field_info_instance($entity_type, $field_name, $bundle_name);
      if (!$instance) {
        $item['instance']['bundle'] = $bundle_name;
        $item['instance']['entity_type'] = $entity_type;
        $item['field_id'] = $field['id'];
        field_create_instance($item['instance']);
        drupal_set_message(t('Field %field has been added to bundle %bundle.', array(
          '%field' => $field_name,
          '%bundle' => $bundle_name,
        )));
      }
      else {
        drupal_set_message(t('Field %field has not been added to bundle %bundle because the bundle already contains this field.', array(
          '%field' => $field_name,
          '%bundle' => $bundle_name,
        )), 'error');
      }
    }
  }
}