You are here

class EntityLegalDocumentController in Entity Legal 7

Same name and namespace in other branches
  1. 7.2 entity_legal.entity_controller.inc \EntityLegalDocumentController

Entity Legal Document controller class.

Hierarchy

Expanded class hierarchy of EntityLegalDocumentController

1 string reference to 'EntityLegalDocumentController'
entity_legal_entity_info in ./entity_legal.module
Implements hook_entity_info().

File

./entity_legal.entity_controller.inc, line 10
Entity API controller classes for entity_legal module.

View source
class EntityLegalDocumentController extends EntityAPIControllerExportable {

  /**
   * {@inheritdoc}
   */
  public function save($entity) {

    // When creating a new legal document, add the document text to the bundle.
    if (!empty($entity->is_new)) {
      $instance = array(
        'field_name' => 'entity_legal_document_text',
        'entity_type' => ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME,
        'bundle' => $entity
          ->identifier(),
        'label' => 'Document text',
        'widget' => array(
          'type' => 'text_textarea_with_summary',
          'weight' => 1,
        ),
        'settings' => array(
          'display_summary' => TRUE,
        ),
        'display' => array(
          'default' => array(
            'label' => 'hidden',
            'type' => 'text_default',
          ),
          'teaser' => array(
            'label' => 'hidden',
            'type' => 'text_summary_or_trimmed',
          ),
        ),
      );
      field_create_instance($instance);
    }
    $success = parent::save($entity);

    // Flush the entity info cache to allow the new bundle to be registered.
    entity_info_cache_clear();
    return $success;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($ids, DatabaseTransaction $transaction = NULL) {

    // Delete all associated versions.
    foreach ($ids as $document_name) {
      $version_query = new EntityFieldQuery();
      $version_query
        ->entityCondition('entity_type', ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME)
        ->propertyCondition('document_name', $document_name);
      $version_result = $version_query
        ->execute();
      if (!empty($version_result) && !empty($version_result[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME])) {
        foreach (array_keys($version_result[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME]) as $version_name) {
          entity_delete(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, $version_name);
        }
      }

      // Delete field instance.
      $instances = field_read_instances(array(
        'entity_type' => 'entity_legal_document_version',
        'bundle' => $document_name,
      ), array(
        'include_inactive' => FALSE,
        'include_deleted' => FALSE,
      ));
      foreach ($instances as $instance) {
        field_delete_instance($instance, FALSE);
      }
    }
    parent::delete($ids, $transaction);
  }

  /**
   * Get all versions of a legal document entity.
   *
   * @param EntityLegalDocument $entity
   *   The legal document entity to get versions of.
   *
   * @return array
   *   All versions of this legal document entity.
   */
  public function getAllVersions(EntityLegalDocument $entity) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME)
      ->propertyCondition('document_name', $entity
      ->identifier());
    $results = $query
      ->execute();
    if (!empty($results)) {
      return entity_load(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, array_keys($results[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME]));
    }
    else {
      return array();
    }
  }

  /**
   * Override the document view to instead output the version view.
   */
  public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
    $entities = entity_key_array_by_property($entities, $this->idKey);
    $view = array();
    foreach ($entities as $entity) {
      $published_version = $entity
        ->getPublishedVersion();
      if ($published_version) {
        $key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
        $view[$this->entityType][$key] = $published_version
          ->view('full', NULL, TRUE);
      }
    }
    return $view;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildContent public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::buildContent
EntityAPIController::create public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::create
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIControllerExportable::$entityCacheByName protected property
EntityAPIControllerExportable::$nameKey protected property
EntityAPIControllerExportable::applyConditions protected function
EntityAPIControllerExportable::attachLoad protected function Overridden. Overrides DrupalDefaultEntityController::attachLoad
EntityAPIControllerExportable::buildQuery protected function Support loading by name key. Overrides EntityAPIController::buildQuery
EntityAPIControllerExportable::cacheGet protected function Overridden. Overrides DrupalDefaultEntityController::cacheGet
EntityAPIControllerExportable::cacheGetByName protected function Like cacheGet() but keyed by name.
EntityAPIControllerExportable::cacheSet protected function Overridden. Overrides DrupalDefaultEntityController::cacheSet
EntityAPIControllerExportable::export public function Overridden. Overrides EntityAPIController::export
EntityAPIControllerExportable::invoke public function Overridden to care about reverted bundle entities and to skip Rules. Overrides EntityAPIController::invoke
EntityAPIControllerExportable::load public function Overridden to support passing numeric ids as well as names as $ids. Overrides EntityAPIController::load
EntityAPIControllerExportable::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides EntityAPIController::resetCache
EntityAPIControllerExportable::__construct public function Overridden. Overrides EntityAPIController::__construct
EntityLegalDocumentController::delete public function Overridden to care about reverted entities. Overrides EntityAPIControllerExportable::delete
EntityLegalDocumentController::getAllVersions public function Get all versions of a legal document entity.
EntityLegalDocumentController::save public function Overridden to care exportables that are overridden. Overrides EntityAPIControllerExportable::save
EntityLegalDocumentController::view public function Override the document view to instead output the version view. Overrides EntityAPIControllerExportable::view