You are here

function _field_tools_add_instance_to_bundles in Field tools 7

Same name and namespace in other branches
  1. 8 field_tools.admin.inc \_field_tools_add_instance_to_bundles()

Helper function to clone a single field instance into multiple bundles.

Parameters

array $instance: The field instance to be added to the bundles.

array $new_instances: An array describing entity bundles on which to create field instances. Each key is an entity type machine name, each value is an array of bundle machine names of that entity.

3 calls to _field_tools_add_instance_to_bundles()
field_tools_bundle_fields_clone_from_form_submit in ./field_tools.admin.inc
Submit handler for the mass clone form.
field_tools_bundle_fields_clone_to_form_submit in ./field_tools.admin.inc
Submit handler for the mass clone to bundle form.
field_tools_field_clone_form_submit in ./field_tools.admin.inc
Submit handler for the field clone form.

File

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

Code

function _field_tools_add_instance_to_bundles($instance, $new_instances) {
  $original_display = $instance['display'];
  $field_info = field_info_field($instance['field_name']);
  $entity_types = entity_get_info();
  foreach ($new_instances as $entity_type => $bundles) {
    $bundles = array_filter($bundles);
    if (!empty($bundles)) {
      if (!_field_tools_entity_can_attach_field($entity_type, $field_info)) {
        drupal_set_message(t('Field %field_label cannot be attached to entity type %entity_type', array(
          '%field_label' => $instance['label'],
          '%entity_type' => $entity_types[$entity_type]['label'],
        )));
        continue;
      }

      // Strip out keys that are specific to the instance being copied.
      $instance = array_diff_key($instance, array_flip(array(
        'id',
        'field_id',
        'bundle',
        'entity_type',
        'deleted',
      )));

      // Only bring back displays that have matching "view mode" in this entity
      // type.
      $view_modes = $entity_types[$entity_type]['view modes'];

      // Add a key for the default display settings, so the array intersection
      // keeps them, as we always want those.
      $view_modes['default'] = TRUE;
      $instance['display'] = array_intersect_key($original_display, $view_modes);
      if (empty($instance['display'])) {

        //@todo should there be logic to handle to no matching 'view modes'
      }
      $instance['entity_type'] = $entity_type;
      foreach ($bundles as $bundle) {
        if (_field_tools_field_already_attached($entity_type, $bundle, $field_info)) {
          drupal_set_message(t('Field %field_label is already attached to %entity_type - %bundle', array(
            '%field_label' => $instance['label'],
            '%entity_type' => $entity_types[$entity_type]['label'],
            '%bundle' => $entity_types[$entity_type]['bundles'][$bundle]['label'],
          )));
          continue;
        }
        $instance['bundle'] = $bundle;
        field_create_instance($instance);
        drupal_set_message(t('Attached field %field_label to %entity_type - %bundle', array(
          '%field_label' => $instance['label'],
          '%entity_type' => $entity_types[$entity_type]['label'],
          '%bundle' => $entity_types[$entity_type]['bundles'][$bundle]['label'],
        )));
      }
    }
  }
}