You are here

entity_share.entity.abstract.inc in Entity Share 7

Class for handling Entity Export Abstract.

File

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

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

/**
 * Entity Share Entity class.
 */
abstract class EntityShareEntityAbstract {

  /**
   * Original entity.
   *
   * @var object
   *   Original entity.
   */
  protected $originalEntity;

  /**
   * Entity.
   *
   * @var object
   *   Working entity.
   */
  protected $entity;

  /**
   * Internal Drupal fields list.
   *
   * @var array
   *   Internal Drupal fields list for exclusion.
   */
  protected $internalFields = array(
    'vid',
    'uid',
    'title',
    'log',
    'status',
    'comment',
    'promote',
    'sticky',
    'vuuid',
    'nid',
    'type',
    'language',
    'created',
    'changed',
    'tnid',
    'translate',
    'uuid',
    'revision_timestamp',
    'revision_uid',
    'metatags',
    'rdf_mapping',
    'entity_type',
    'translations',
    'name',
    'picture',
    'data',
    'print_html_display',
    'print_html_display_comment',
    'print_html_display_urllist',
    'print_pdf_display',
    'print_pdf_display_comment',
    'print_pdf_display_urllist',
    'print_pdf_size',
    'print_pdf_orientation',
    'item_id',
    'revision_id',
    'default_revision',
    'archived',
  );

  /**
   * Value sub dimensions.
   *
   * @var array
   *   Value sub dimensions to treat for embedded medias in RTE.
   */
  protected $rteKeys = array(
    'value',
    'safe_value',
    'summary',
    'safe_summary',
  );

  /**
   * Constructor. Initialize properties.
   *
   * @param object $entity
   *   Entity to share.
   */
  public function __construct($entity) {
    if (empty($entity)) {
      throw new EntityShareException('Entity can\\t be empty.');
    }
    $this->originalEntity = $entity;
    $this->entity = $entity;
  }

  /**
   * Get the original unmodified entity.
   *
   * @return object
   *   Original entity to share.
   */
  public function getOriginalEntity() {
    return $this->originalEntity;
  }

  /**
   * Get the modified entity.
   *
   * @return object
   *   Entity to share.
   */
  public function getEntity() {
    return $this->entity;
  }

  /**
   * Get the current entity type.
   *
   * @return mixed
   *   Type of the Entity.
   */
  public function getEntityType() {
    return isset($this->entity->entity_type) ? $this->entity->entity_type : FALSE;
  }

  /**
   * Walk though the entity to do special treatment in function of field type.
   *
   * @param object $entity
   *   Entity or sub entity to share.
   */
  public function contentFieldWalk($entity = NULL) {
    if (!isset($entity)) {
      $entity = $this
        ->getEntity();
    }

    // Fix some exceptions or warnings linked to the json conversion.
    if (isset($entity->translations) && !is_object($entity->translations)) {
      $entity->translations = (object) $entity->translations;
    }

    // Load additional needed information.
    foreach ($entity as $field_name => $field_value) {
      if (in_array($field_name, $this->internalFields)) {
        continue;
      }

      // Needed to detect that a field is a collection field.
      $field_info = field_info_field($field_name);
      if (!isset($field_info['type'])) {
        continue;
      }
      $this
        ->manageField($field_info, $field_name, $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 share.
   */
  protected abstract function manageField(array $field_info, $field_name, $entity = NULL);

  /**
   * Execute the treatment.
   *
   * @return mixed
   *   Exported or imported entity.
   */
  public abstract function execute();

}

Classes

Namesort descending Description
EntityShareEntityAbstract Entity Share Entity class.