You are here

function uuid_entities_features_export_render in Universally Unique IDentifier 7

Implements [component]_features_export_render().

Components corresponds to the name of Deploy plans. However, once exported, entities can be rendered directly from the default hook without Deploy or the initial plan.

File

./uuid.features.inc, line 41
Features support to export entities from any Deploy <em>fetch-only</em> plan.

Code

function uuid_entities_features_export_render($module_name, $components, $export = NULL) {
  $code = array();
  $code[] = '  $entities = array();';
  $code[] = '';
  foreach ($components as $component) {
    $entities = array();
    if (module_exists('deploy') && ($plan = deploy_plan_load($component))) {
      $entities = $plan
        ->getIterator();
    }
    else {
      features_include_defaults(array(
        'uuid_entities',
      ));
      $default_entities = module_invoke($module_name, 'uuid_default_entities');
      foreach ($default_entities[$component] as $entity) {
        $metadata = $entity->__metadata;
        $entity_type = $metadata['type'];
        $entity_info = entity_get_info($entity_type);
        $results = entity_uuid_load($entity_type, array(
          $entity->{$entity_info['entity keys']['uuid']},
        ), NULL, TRUE);
        if (!empty($results)) {
          $entity = reset($results);

          // Re-attach the metadata after loading the clean entity.
          $entity->__metadata = $metadata;
          $entities[] = $entity;
        }
      }
    }
    foreach ($entities as $entity) {
      $entity_type = $entity->__metadata['type'];
      $entity_info = entity_get_info($entity_type);

      // We need to remove entity id and revision keys for better consistency.
      $id_key = $entity_info['entity keys']['id'];
      if (isset($entity->{$id_key})) {
        unset($entity->{$id_key});
      }
      if (!empty($entity_info['entity keys']['revision'])) {
        $vid_key = $entity_info['entity keys']['revision'];
        if (isset($entity->{$vid_key})) {
          unset($entity->{$vid_key});
        }
        if (!empty($entity_info['entity keys']['revision uuid'])) {
          $vuuid_key = $entity_info['entity keys']['revision uuid'];
          unset($entity->{$vuuid_key});
        }
      }

      // We unset some common timestamp properties, since those will change and
      // constantly leave the feature overidden.
      $keys = array(
        'created',
        'updated',
        'changed',
        'revision_timestamp',
        'timestamp',
        'stamp',
        'current',
      );
      foreach ($keys as $key) {
        if (isset($entity->{$key})) {
          unset($entity->{$key});
        }
      }

      // Let other modules alter exported entities.
      drupal_alter('uuid_entities_features_export_entity', $entity, $entity_type);

      // Field handling.
      list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
      $instances = field_info_instances($entity_type, $bundle_name);
      foreach ($instances as $field_name => $instance) {
        $field = field_info_field($field_name);
        if (!empty($entity->{$field_name})) {
          foreach ($entity->{$field_name} as $langcode => &$items) {

            // Let other modules alter fields on exported entities.
            // We are not using drupal_alter() here, because of it's complexity
            // dealing with over two alterable arguments.
            $hook = 'uuid_entities_features_export_field_alter';
            foreach (module_implements($hook) as $module_name) {
              $function = $module_name . '_' . $hook;
              $function($entity_type, $entity, $field, $instance, $langcode, $items);
            }
            foreach ($items as &$item) {

              // We don't need to export these common field keys.
              foreach (array(
                'safe_value',
                'safe_summary',
              ) as $key) {
                if (isset($item[$key])) {
                  unset($item[$key]);
                }
              }
              uuid_entities_features_clean($item);
            }
          }
        }
      }
      uuid_entities_features_clean($entity);

      /*
       * Convert entities to array to avoid having them in JSON, returned
       * from standard implementation of $entity->export().
       */
      if (is_object($entity) && method_exists($entity, 'export')) {
        $entity = get_object_vars($entity);
      }
      $code[] = '  $entities[\'' . check_plain($component) . '\'][] = (object) ' . features_var_export($entity, '  ') . ';';
    }
  }
  $code[] = '';
  $code[] = '  return $entities;';
  return array(
    'uuid_default_entities' => implode("\n", $code),
  );
}