You are here

class EntityShareEntityImport in Entity Share 7

Manage general entity import.

Hierarchy

Expanded class hierarchy of EntityShareEntityImport

File

includes/entity_share.entity.import.inc, line 11
Class for handling Entity Import.

View source
class EntityShareEntityImport extends EntityShareEntityAbstract {
  const WATCHDOG_TYPE = 'entity_share_import';
  const HOOK_PREFIX = 'es_import_';

  /**
   * Generate the import.
   *
   * @return int
   *   Imported Entity Id.
   */
  public function execute() {
    $this
      ->contentFieldWalk();
    $this
      ->saveEntity($this
      ->getEntity());
    list($entity_id) = entity_extract_ids($this
      ->getEntityType(), $this
      ->getEntity());
    return $entity_id;
  }

  /**
   * Save the entity using uuid if needed.
   *
   * @param object $entity
   *   Entity or sub entity to import.
   *
   * @return object
   *   Imported Entity or sub entity.
   */
  protected function saveEntity($entity = NULL) {
    if (!isset($entity)) {
      $entity = $this
        ->getEntity();
    }
    $this
      ->setLocalEntityIds($entity);

    // @TODO Handle exception thrown by entity_save() on failure.
    entity_save($entity->entity_type, $entity);
    return $entity;
  }

  /**
   * Test if uuid already exists and get the local entity id.
   *
   * @param object $entity
   *   Entity or sub entity to import.
   *
   * @throws EntityShareImportException
   *   Import exception.
   *
   * @return array|bool
   *   Local ids of the entity, FALSE if problem.
   */
  public function getLocalEntityIds($entity = NULL) {
    if (!isset($entity)) {
      $entity = $this
        ->getEntity();
    }
    if (!is_object($entity)) {
      throw new EntityShareImportException('Object expected for $entity variable');
    }
    $entity_id = NULL;
    $revision_id = NULL;

    // Uuid.
    $entity_info = entity_get_info($entity->entity_type);
    $uuid_key = $entity_info['entity keys']['uuid'];
    if (isset($entity_info['entity keys']['revision uuid'])) {
      $vuuid_key = $entity_info['entity keys']['revision uuid'];
      if (isset($entity->{$vuuid_key})) {
        $vuuid = $entity->{$vuuid_key};
      }
    }
    $uuid = $entity->{$uuid_key};
    $entity_ids = entity_get_id_by_uuid($entity->entity_type, array(
      $uuid,
    ));
    if (!isset($entity_ids[$uuid])) {
      return FALSE;
    }
    $entity_id = $entity_ids[$uuid];
    if (isset($vuuid)) {
      $revision_ids = entity_get_id_by_uuid($entity->entity_type, array(
        $vuuid,
      ), TRUE);
      if (isset($revision_ids[$vuuid])) {
        $revision_id = $revision_ids[$vuuid];
      }
    }
    return array(
      'entity_id' => $entity_id,
      'revision_id' => $revision_id,
    );
  }

  /**
   * Set the local entity ids.
   *
   * @param object $entity
   *   Entity or sub entity to import.
   */
  public function setLocalEntityIds($entity) {
    $entity_info = entity_get_info($entity->entity_type);
    $entity_id_info = $this
      ->getLocalEntityIds($entity);

    // Specific case of importing directly some entities
    // as main entity (taxonomy_term, ...).
    $context = array(
      'entity_info' => $entity_info,
      'entity_id_info' => $entity_id_info,
    );
    drupal_alter(self::HOOK_PREFIX . 'local_entities', $entity, $context);

    // Not available locally yet.
    if (!$entity_id_info) {

      // Clean exported entity id.
      $entity->{$entity_info['entity keys']['id']} = NULL;
    }
    if (!empty($entity_id_info['entity_id'])) {

      // Get local id.
      $entity->{$entity_info['entity keys']['id']} = $entity_id_info['entity_id'];
      if (!empty($entity_id_info['revision_id']) && isset($entity_info['entity keys']['revision'])) {
        $entity->{$entity_info['entity keys']['revision']} = $entity_id_info['revision_id'];
      }
      else {

        // If there is no revision_id or if it was not found on this site, set
        // this revision flag to true to create a new revision.
        $entity->revision = TRUE;

        // This uuid_services flag, indicate to uuid module no to overwrite
        // our vuuid with a new one.
        // @TODO Check uuid_entity_presave() for any update on uuid_services flag usage.
        $entity->uuid_services = TRUE;
      }
    }
  }

  /**
   * Manage the field.
   *
   * @param array $field_info
   *   Metadata of the field.
   * @param string $field_name
   *   Name of the field.
   * @param object $entity
   *   Entity or sub entity to import.
   */
  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 after.
        $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);
      }
    }
  }

}

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::contentFieldWalk public function Walk though the entity to do special treatment in function of field type. 1
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.
EntityShareEntityAbstract::__construct public function Constructor. Initialize properties. 1
EntityShareEntityImport::execute public function Generate the import. Overrides EntityShareEntityAbstract::execute
EntityShareEntityImport::getLocalEntityIds public function Test if uuid already exists and get the local entity id.
EntityShareEntityImport::HOOK_PREFIX constant
EntityShareEntityImport::manageField protected function Manage the field. Overrides EntityShareEntityAbstract::manageField
EntityShareEntityImport::saveEntity protected function Save the entity using uuid if needed.
EntityShareEntityImport::setLocalEntityIds public function Set the local entity ids.
EntityShareEntityImport::WATCHDOG_TYPE constant