You are here

function _bundle_copy_export_data in Bundle Copy 7.2

Same name and namespace in other branches
  1. 7 bundle_copy.module \_bundle_copy_export_data()

Creates export data.

Parameters

$entity_type: The name of the entity_type

$selected_data: The selected data.

1 call to _bundle_copy_export_data()
bundle_copy_export in ./bundle_copy.module
Menu callback: present the export page.

File

./bundle_copy.module, line 507
Bundle copy.

Code

function _bundle_copy_export_data($entity_type, $selected_data) {
  ctools_include('export');
  $bc_info = bundle_copy_get_info();
  $selected_bundles = $selected_data[1]['bundles'];
  $selected_fields = $selected_data[2]['fields'];
  $selected_fieldgroups = isset($selected_data[2]['fieldgroups']) ? $selected_data[2]['fieldgroups'] : array();
  $full_fieldgroups = isset($selected_data[2]['fieldgroups-full']) ? $selected_data[2]['fieldgroups-full'] : array();
  $data = $instances = array();
  $fields = field_info_fields();
  foreach ($selected_bundles as $bkey => $binfo) {
    if ($bkey !== $binfo) {
      continue;
    }
    $field_instances = field_info_instances($entity_type, $bkey);
    ksort($field_instances);

    // Bundles export data.
    $bundle_info_callback = $bc_info[$entity_type]['bundle_export_callback'];
    $bundle_info = $bundle_info_callback($bkey, $entity_type);
    if (is_object($bundle_info)) {
      $bundle_info->bc_entity_type = $entity_type;
    }
    elseif (is_array($bundle_info)) {
      $bundle_info['bc_entity_type'] = $entity_type;
    }
    $data['bundles'][$bkey] = $bundle_info;

    // Fields export data.
    foreach ($selected_fields as $fkey => $finfo) {
      if ($fkey === $finfo) {
        if (!isset($data['fields'][$fkey])) {
          unset($fields[$fkey]['id']);
          $data['fields'][$fkey] = $fields[$fkey];
        }
        if (isset($field_instances[$fkey])) {
          unset($field_instances[$fkey]['id']);
          unset($field_instances[$fkey]['field_id']);
          $instances[$fkey][] = $field_instances[$fkey];
        }

        // Field Collection Export data.
        if ($fields[$fkey]['type'] == 'field_collection' && $fields[$fkey]['module'] == 'field_collection') {
          $fc_fields = _bc_copy_field_collection_export($fields, $fields[$fkey]['field_name']);
          foreach ($fc_fields as $fc_fkey => $fc_finfo) {
            if (!isset($data['fields'][$fc_fkey])) {
              unset($fields[$fc_fkey]['id']);
              $data['fields'][$fc_fkey] = $fields[$fc_fkey];
            }
            $instances[$fc_fkey][] = $fc_finfo['instance'];
          }
        }
      }
    }
  }
  ksort($instances);
  $data['instances'] = $instances;

  // Field group export data.
  if (!empty($selected_fieldgroups)) {
    foreach ($selected_fieldgroups as $key => $value) {
      if ($value !== 0) {
        $data['fieldgroups'][$full_fieldgroups[$key]->identifier] = $full_fieldgroups[$key];
      }
    }
  }
  return '$data = ' . ctools_var_export($data) . ';';
}