You are here

function bundle_copy_import_process in Bundle Copy 7.2

1 call to bundle_copy_import_process()
bundle_copy_import_submit in ./bundle_copy.module
Submit callback: import data.

File

./bundle_copy.module, line 327
Bundle copy.

Code

function bundle_copy_import_process($data) {
  $modules = module_list();
  $bc_info = bundle_copy_get_info();

  // Create bundles.
  foreach ($data['bundles'] as $key => $bundle) {
    $entity_type = '';
    if (is_object($bundle)) {
      $entity_type = $bundle->bc_entity_type;
    }
    elseif (is_array($bundle)) {
      $entity_type = $bundle['bc_entity_type'];
    }
    if (!empty($entity_type)) {
      $existing_bundles = _bundle_copy_bundle_info($entity_type);
      $bundle_save_callback = $bc_info[$entity_type]['bundle_save_callback'];

      // Validate a bundle name.
      $bundle_name_validate = $bc_info[$entity_type]['bundle_name_validate'];
      $bundle_name = $bundle->type ? $bundle->type : $bundle->machine_name;
      $content_name = $bundle->name;
      $is_type_exist = in_array($content_name, $existing_bundles);
      $machine_name = preg_replace('@[^a-z0-9]+@', '_', strtolower($bundle_name));
      if ($bundle_name === $machine_name) {
        if (!isset($existing_bundles[$bundle_name])) {
          if ($is_type_exist) {
            drupal_set_message(t('The human-readable name %bundle is already taken', array(
              '%bundle' => $bundle->name,
            )));
            return;
          }
          else {
            $bundle_info = $bundle_save_callback($bundle);
            drupal_set_message(t('%bundle bundle has been created.', array(
              '%bundle' => $bundle->name,
            )));
          }
        }
        else {
          $orignal_name = node_type_load($bundle_name);
          if ($is_type_exist && $content_name != $orignal_name->name) {
            drupal_set_message(t('The human-readable name %bundle is already taken', array(
              '%bundle' => $bundle->name,
            )));
            return;
          }
          else {
            $bundle_info = $bundle_save_callback($bundle);
            drupal_set_message(t('%bundle bundle has been updated.', array(
              '%bundle' => $bundle->name,
            )));
          }
        }
      }
      else {
        drupal_set_message('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
        return;
      }
    }
  }

  // Create or update fields and their instances.
  if (isset($data['fields'])) {
    foreach ($data['fields'] as $key => $field) {

      // Check if the field module exists.
      $module = $field['module'];
      if (!isset($modules[$module])) {
        drupal_set_message(t('%field_name field could not be created because the module %module is disabled or missing.', array(
          '%field_name' => $key,
          '%module' => $module,
        )), 'error');
        continue;
      }
      if (isset($data['instances'][$key])) {

        // Create or update field.
        $prior_field = field_read_field($field['field_name'], array(
          'include_inactive' => TRUE,
        ));
        if (!$prior_field) {
          field_create_field($field);
          drupal_set_message(t('%field_name field has been created.', array(
            '%field_name' => $key,
          )));
        }
        else {
          $field['id'] = $prior_field['id'];
          field_update_field($field);
          drupal_set_message(t('%field_name field has been updated.', array(
            '%field_name' => $key,
          )));
        }

        // Create or update field instances.
        foreach ($data['instances'][$key] as $ikey => $instance) {

          // Make sure the needed key exists.
          if (!isset($instance['field_name'])) {
            continue;
          }
          $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
          if (!$prior_instance) {
            field_create_instance($instance);
            drupal_set_message(t('%field_name instance has been created for @bundle in @entity_type.', array(
              '%field_name' => $key,
              '@bundle' => $instance['bundle'],
              '@entity_type' => $instance['entity_type'],
            )));
          }
          else {
            $instance['id'] = $prior_instance['id'];
            $instance['field_id'] = $prior_instance['field_id'];
            field_update_instance($instance);
            drupal_set_message(t('%field_name instance has been updated for @bundle in @entity_type.', array(
              '%field_name' => $key,
              '@bundle' => $instance['bundle'],
              '@entity_type' => $instance['entity_type'],
            )));
          }
        }
      }
    }
  }

  // Create / update fieldgroups.
  if (isset($data['fieldgroups'])) {
    if (module_exists('field_group')) {
      ctools_include('export');
      $existing_field_groups = field_group_info_groups();
      foreach ($data['fieldgroups'] as $identifier => $fieldgroup) {
        if (isset($existing_field_groups[$fieldgroup->entity_type][$fieldgroup->bundle][$fieldgroup->mode][$fieldgroup->group_name])) {
          $existing = $existing_field_groups[$fieldgroup->entity_type][$fieldgroup->bundle][$fieldgroup->mode][$fieldgroup->group_name];
          $fieldgroup->id = $existing->id;
          if (!isset($fieldgroup->disabled)) {
            $fieldgroup->disabled = FALSE;
          }
          ctools_export_crud_save('field_group', $fieldgroup);
          ctools_export_crud_set_status('field_group', $fieldgroup, $fieldgroup->disabled);
          drupal_set_message(t('%fieldgroup fieldgroup has been updated for @bundle in @entity_type.', array(
            '%fieldgroup' => $fieldgroup->label,
            '@bundle' => $fieldgroup->bundle,
            '@entity_type' => $fieldgroup->entity_type,
          )));
        }
        else {
          unset($fieldgroup->id);
          unset($fieldgroup->export_type);
          if (!isset($fieldgroup->disabled)) {
            $fieldgroup->disabled = FALSE;
          }
          ctools_export_crud_save('field_group', $fieldgroup);
          $fieldgroup->export_type = 1;
          ctools_export_crud_set_status('field_group', $fieldgroup, $fieldgroup->disabled);
          drupal_set_message(t('%fieldgroup fieldgroup has been saved for @bundle in @entity_type.', array(
            '%fieldgroup' => $fieldgroup->label,
            '@bundle' => $fieldgroup->bundle,
            '@entity_type' => $fieldgroup->entity_type,
          )));
        }
      }
    }
    else {
      drupal_set_message(t('The fieldgroups could not be saved because the <em>Field group</em> module is disabled or missing.'), 'error');
    }
  }

  // Clear caches.
  field_info_cache_clear();
  if (module_exists('field_group')) {
    cache_clear_all('field_groups', 'cache_field');
  }
}