You are here

class EntityShareEntityExport in Entity Share 7

Manage general entity export.

Hierarchy

Expanded class hierarchy of EntityShareEntityExport

File

includes/entity_share.entity.export.inc, line 11
Class for handling Entity Export.

View source
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;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityShareEntityAbstract::$entity protected property Entity.
EntityShareEntityAbstract::$internalFields protected property Internal Drupal fields list.
EntityShareEntityAbstract::$originalEntity protected property Original entity.
EntityShareEntityAbstract::$rteKeys protected property Value sub dimensions.
EntityShareEntityAbstract::getEntity public function Get the modified entity.
EntityShareEntityAbstract::getEntityType public function Get the current entity type.
EntityShareEntityAbstract::getOriginalEntity public function Get the original unmodified entity.
EntityShareEntityExport::contentFieldWalk public function Walk though the entity to load dependencies and prepare it for the export. Overrides EntityShareEntityAbstract::contentFieldWalk
EntityShareEntityExport::execute public function Generate the export. Overrides EntityShareEntityAbstract::execute
EntityShareEntityExport::HOOK_PREFIX constant
EntityShareEntityExport::manageField protected function Manage the field. Overrides EntityShareEntityAbstract::manageField
EntityShareEntityExport::unsetEntityIds protected function Clear entity ids.
EntityShareEntityExport::__construct public function Constructor. Initialize properties. Overrides EntityShareEntityAbstract::__construct