You are here

class EntityReferenceRevisions in Entity Reference Revisions 8

Same name in this branch
  1. 8 src/Plugin/DataType/EntityReferenceRevisions.php \Drupal\entity_reference_revisions\Plugin\DataType\EntityReferenceRevisions
  2. 8 src/Plugin/views/display/EntityReferenceRevisions.php \Drupal\entity_reference_revisions\Plugin\views\display\EntityReferenceRevisions
  3. 8 src/Plugin/views/style/EntityReferenceRevisions.php \Drupal\entity_reference_revisions\Plugin\views\style\EntityReferenceRevisions
  4. 8 src/Plugin/views/row/EntityReferenceRevisions.php \Drupal\entity_reference_revisions\Plugin\views\row\EntityReferenceRevisions
  5. 8 src/Plugin/migrate/destination/EntityReferenceRevisions.php \Drupal\entity_reference_revisions\Plugin\migrate\destination\EntityReferenceRevisions

Provides entity_reference_revisions destination plugin.

Available configuration keys:

  • new_revisions: (optional) Flag to indicate if a new revision should be created instead of updating a previous default record. Only applicable when providing an entity id without a revision_id.
  • force_revision: (optional) Flag to ignore other checks and always create a revision.

Plugin annotation


@MigrateDestination(
  id = "entity_reference_revisions",
  deriver = "Drupal\entity_reference_revisions\Plugin\Derivative\MigrateEntityReferenceRevisions"
)

Hierarchy

Expanded class hierarchy of EntityReferenceRevisions

2 files declare their use of EntityReferenceRevisions
EntityReferenceRevisionsDeriverTest.php in tests/src/Kernel/Plugin/Derivative/EntityReferenceRevisionsDeriverTest.php
MigrateEntityReferenceRevisions.php in src/Plugin/Derivative/MigrateEntityReferenceRevisions.php

File

src/Plugin/migrate/destination/EntityReferenceRevisions.php, line 28

Namespace

Drupal\entity_reference_revisions\Plugin\migrate\destination
View source
class EntityReferenceRevisions extends EntityRevision implements ConfigurableInterface {

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'new_revisions' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected static function getEntityTypeId($pluginId) {

    // Remove "entity_reference_revisions:".
    // Ideally, we would call getDerivativeId(), but since this is static
    // that is not possible so we follow the same pattern as core.
    return substr($pluginId, 27);
  }

  /**
   * {@inheritdoc}
   */
  protected function save(ContentEntityInterface $entity, array $oldDestinationIdValues = []) {
    $entity
      ->save();
    return [
      $this
        ->getKey('id') => $entity
        ->id(),
      $this
        ->getKey('revision') => $entity
        ->getRevisionId(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    if ($revision_key = $this
      ->getKey('revision')) {
      $id_key = $this
        ->getKey('id');
      $ids[$id_key]['type'] = 'integer';

      // TODO: Improve after https://www.drupal.org/node/2783715 is finished.
      $ids[$revision_key]['type'] = 'integer';
      if ($this
        ->isTranslationDestination()) {
        if ($revision_key = $this
          ->getKey('langcode')) {
          $ids[$revision_key]['type'] = 'string';
        }
        else {
          throw new MigrateException('This entity type does not support translation.');
        }
      }
      return $ids;
    }
    throw new MigrateException('This entity type does not support revisions.');
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntity(Row $row, array $oldDestinationIdValues) {
    $entity_id = $oldDestinationIdValues ? array_shift($oldDestinationIdValues) : $this
      ->getEntityId($row);
    $configuration = $this
      ->getConfiguration();
    if (isset($configuration['force_revision']) && $configuration['force_revision'] == TRUE) {
      $revision_id = NULL;
    }
    else {
      $revision_id = $oldDestinationIdValues ? array_pop($oldDestinationIdValues) : $row
        ->getDestinationProperty($this
        ->getKey('revision'));
    }

    // If a specific revision_id is supplied and exists, assert the entity_id
    // matches (if supplied), and update the revision.

    /** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityInterface $entity */
    if (!empty($revision_id) && ($entity = $this->storage
      ->loadRevision($revision_id))) {
      if (!empty($entity_id) && $entity
        ->id() != $entity_id) {
        throw new MigrateException("The revision_id exists for this entity type, but does not belong to the given entity id");
      }
      $entity = $this
        ->updateEntity($entity, $row) ?: $entity;
    }
    elseif (!empty($entity_id) && ($entity = $this->storage
      ->load($entity_id))) {

      // If so configured, create a new revision while updating.
      if (!empty($this->configuration['new_revisions'])) {
        $entity
          ->setNewRevision(TRUE);
      }
      $entity = $this
        ->updateEntity($entity, $row) ?: $entity;
    }
    else {

      // Attempt to ensure we always have a bundle.
      if ($bundle = $this
        ->getBundle($row)) {
        $row
          ->setDestinationProperty($this
          ->getKey('bundle'), $bundle);
      }

      // Stubs might need some required fields filled in.
      if ($row
        ->isStub()) {
        $this
          ->processStubRow($row);
      }
      $entity = $this->storage
        ->create($row
        ->getDestination())
        ->enforceIsNew(TRUE);
      $entity
        ->setNewRevision(TRUE);
    }
    $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function rollback(array $destination_identifiers) {
    if ($this
      ->isTranslationDestination()) {
      $this
        ->rollbackTranslation($destination_identifiers);
    }
    else {
      $this
        ->rollbackNonTranslation($destination_identifiers);
    }
  }

  /**
   * Rollback translation destinations.
   *
   * @param array $destination_identifiers
   *   The IDs of the destination object to delete.
   */
  protected function rollbackTranslation(array $destination_identifiers) {
    $entity = $this->storage
      ->loadRevision(array_pop($destination_identifiers));
    if ($entity && $entity instanceof TranslatableInterface) {
      if ($key = $this
        ->getKey('langcode')) {
        if (isset($destination_identifiers[$key])) {
          $langcode = $destination_identifiers[$key];
          if ($entity
            ->hasTranslation($langcode)) {

            // Make sure we don't remove the default translation.
            $translation = $entity
              ->getTranslation($langcode);
            if (!$translation
              ->isDefaultTranslation()) {
              $entity
                ->removeTranslation($langcode);
              $entity
                ->save();
            }
          }
        }
      }
    }
  }

  /**
   * Rollback non-translation destinations.
   *
   * @param array $destination_identifiers
   *   The IDs of the destination object to delete.
   */
  protected function rollbackNonTranslation(array $destination_identifiers) {
    $revision_id = array_pop($destination_identifiers);
    $entity = $this->storage
      ->loadRevision($revision_id);
    if ($entity) {
      if ($entity
        ->isDefaultRevision()) {
        $entity
          ->delete();
      }
      else {
        $this->storage
          ->deleteRevision($revision_id);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
DestinationBase::$migration protected property The migration.
DestinationBase::$rollbackAction protected property The rollback action to be saved for the last imported item.
DestinationBase::$supportsRollback protected property Indicates whether the destination can be rolled back.
DestinationBase::checkRequirements public function Checks if requirements for this plugin are OK. Overrides RequirementsInterface::checkRequirements
DestinationBase::getDestinationModule public function Gets the destination module handling the destination data. Overrides MigrateDestinationInterface::getDestinationModule 1
DestinationBase::rollbackAction public function The rollback action for the last imported item. Overrides MigrateDestinationInterface::rollbackAction
DestinationBase::setRollbackAction protected function For a destination item being updated, set the appropriate rollback action.
DestinationBase::supportsRollback public function Whether the destination can be rolled back or not. Overrides MigrateDestinationInterface::supportsRollback
Entity::$bundles protected property The list of the bundles of this entity type.
Entity::$storage protected property The entity storage.
Entity::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
Entity::fields public function Returns an array of destination fields. Overrides MigrateDestinationInterface::fields
Entity::getBundle public function Gets the bundle for the row taking into account the default.
Entity::getEntityId protected function Gets the entity ID of the row. 2
Entity::getKey protected function Returns a specific entity key.
EntityContentBase::$deprecatedProperties protected property
EntityContentBase::$entityFieldManager protected property Entity field manager.
EntityContentBase::$fieldTypeManager protected property Field type plugin manager.
EntityContentBase::create public static function Creates an instance of the plugin. Overrides Entity::create 2
EntityContentBase::import public function Overrides MigrateDestinationInterface::import 3
EntityContentBase::isEntityValidationRequired public function Returns a state of whether an entity needs to be validated before saving. Overrides MigrateValidatableEntityInterface::isEntityValidationRequired
EntityContentBase::isTranslationDestination public function
EntityContentBase::processStubRow protected function Populates as much of the stub row as possible. 3
EntityContentBase::updateEntity protected function Updates an entity with the new values from row. 3
EntityContentBase::validateEntity public function Validates the entity. Overrides MigrateValidatableEntityInterface::validateEntity
EntityFieldDefinitionTrait::getDefinitionFromEntity protected function Gets the field definition from a specific entity base field.
EntityReferenceRevisions::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
EntityReferenceRevisions::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
EntityReferenceRevisions::getEntity protected function Gets the entity. Overrides EntityRevision::getEntity
EntityReferenceRevisions::getEntityTypeId protected static function Finds the entity type from configuration or plugin ID. Overrides EntityFieldDefinitionTrait::getEntityTypeId
EntityReferenceRevisions::getIds public function Gets the destination IDs. Overrides EntityRevision::getIds
EntityReferenceRevisions::rollback public function Delete the specified destination object from the target Drupal. Overrides EntityContentBase::rollback
EntityReferenceRevisions::rollbackNonTranslation protected function Rollback non-translation destinations.
EntityReferenceRevisions::rollbackTranslation protected function Rollback translation destinations.
EntityReferenceRevisions::save protected function Saves the entity. Overrides EntityRevision::save
EntityReferenceRevisions::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
EntityRevision::getHighestId public function Returns the highest ID tracked by the implementing plugin. Overrides EntityContentBase::getHighestId
EntityRevision::__construct public function Constructs a content entity. Overrides EntityContentBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.