You are here

entity_share.entity.export.inc in Entity Share 7

Class for handling Entity Export.

File

includes/entity_share.entity.export.inc
View source
<?php

/**
 * @file
 * Class for handling Entity Export.
 */

/**
 * Manage general entity export.
 */
class EntityShareEntityExport extends EntityShareEntityAbstract {
  const HOOK_PREFIX = 'es_export_';

  /**
   * Constructor. Initialize properties.
   *
   * @param object $entity
   *   Entity to export.
   */
  public function __construct($entity) {
    if (empty($entity)) {
      throw new EntityShareExportException('Entity can\\t be empty.');
    }
    parent::__construct($entity);

    // Add a entity_share property to the entity.
    if (!isset($entity->entity_share)) {
      $entity->entity_share = new stdClass();
    }

    // Reset the static cache of the entity to avoid invalid
    // entity problem (without id) when multiple exports of
    // the same entity (ex: taxonomy term etc).
    entity_get_controller($entity->entity_type)
      ->resetCache();
  }

  /**
   * Generate the export.
   *
   * @return object
   *   Exported Entity.
   */
  public function execute() {
    $this
      ->contentFieldWalk();
    return $this
      ->getEntity();
  }

  /**
   * Walk though the entity to load dependencies and prepare it for the export.
   *
   * @param object $entity
   *   Entity or sub entity to export.
   */
  public function contentFieldWalk($entity = NULL) {
    if (!isset($entity)) {
      $entity = $this
        ->getEntity();
    }
    $this
      ->unsetEntityIds($entity);
    parent::contentFieldWalk($entity);
  }

  /**
   * Manage the field.
   *
   * @param array $field_info
   *   Informations of the current field.
   * @param string $field_name
   *   Name of the current field.
   * @param object $entity
   *   Entity or sub entity to export.
   */
  protected function manageField(array $field_info, $field_name, $entity = NULL) {
    if (!isset($entity)) {
      $entity = $this
        ->getEntity();
    }
    $field_type = $field_info['type'];

    // Loop for multi language.
    foreach ($entity->{$field_name} as $lang => &$datas) {
      foreach ($datas as $delta => &$value) {

        // Hook alter.
        $context = array(
          'field_name' => $field_name,
          'field_type' => $field_type,
          'lang' => $lang,
          'delta' => $delta,
          'entity' => $entity,
          'field_info' => $field_info,
          'entity_share_entity' => $this,
        );
        drupal_alter(self::HOOK_PREFIX . 'field_data', $value, $context);
      }
    }
  }

  /**
   * Clear entity ids.
   *
   * @param object $obj
   *   Entity or sub entity to unset ids.
   *
   * @throws Exception
   *   In case of  invalid entity.
   */
  protected function unsetEntityIds($obj) {
    if (!isset($obj->entity_type)) {
      throw new Exception('Invalid entity');
    }

    // Clear ids/vids.
    $info = entity_get_info($obj->entity_type);
    $obj->{$info['entity keys']['id']} = NULL;
    if (isset($obj->{$info['entity keys']['revision']})) {
      $obj->{$info['entity keys']['revision']} = NULL;
    }
  }

}

Classes

Namesort descending Description
EntityShareEntityExport Manage general entity export.