You are here

class EntityConfigBase in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php \Drupal\migrate\Plugin\migrate\destination\EntityConfigBase

Class for importing configuration entities.

This class serves as the import class for most configuration entities. It can be necessary to provide a specific entity class if the configuration entity has a compound id (see EntityFieldEntity) or it has specific setter methods (see EntityDateFormat). When implementing an entity destination for the latter case, make sure to add a test not only for importing but also for re-importing (if that is supported).

Hierarchy

Expanded class hierarchy of EntityConfigBase

7 files declare their use of EntityConfigBase
EntityBlock.php in core/modules/block/src/Plugin/migrate/destination/EntityBlock.php
Contains \Drupal\block\Plugin\migrate\destination\EntityBlock.
EntityCommentType.php in core/modules/comment/src/Plugin/migrate/destination/EntityCommentType.php
Contains \Drupal\comment\Plugin\migrate\destination\EntityCommentType.
EntityDateFormat.php in core/modules/system/src/Plugin/migrate/destination/EntityDateFormat.php
Contains \Drupal\system\Plugin\migrate\destination\EntityDateFormat.
EntityImageStyle.php in core/modules/image/src/Plugin/migrate/destination/EntityImageStyle.php
Contains \Drupal\image\Plugin\migrate\destination\EntityImageStyle.
EntityNodeType.php in core/modules/node/src/Plugin/migrate/destination/EntityNodeType.php
Contains \Drupal\node\Plugin\migrate\destination\EntityNodeType.

... See full list

File

core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php, line 26
Contains \Drupal\migrate\Plugin\migrate\destination\EntityConfigBase.

Namespace

Drupal\migrate\Plugin\migrate\destination
View source
class EntityConfigBase extends Entity {

  /**
   * {@inheritdoc}
   */
  public function import(Row $row, array $old_destination_id_values = array()) {
    if ($row
      ->isStub()) {
      throw new MigrateException('Config entities can not be stubbed.');
    }
    $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
    $ids = $this
      ->getIds();
    $id_key = $this
      ->getKey('id');
    if (count($ids) > 1) {

      // Ids is keyed by the key name so grab the keys.
      $id_keys = array_keys($ids);
      if (!$row
        ->getDestinationProperty($id_key)) {

        // Set the id into the destination in for form "val1.val2.val3".
        $row
          ->setDestinationProperty($id_key, $this
          ->generateId($row, $id_keys));
      }
    }
    $entity = $this
      ->getEntity($row, $old_destination_id_values);
    $entity
      ->save();
    if (count($ids) > 1) {

      // This can only be a config entity, content entities have their id key
      // and that's it.
      $return = array();
      foreach ($id_keys as $id_key) {
        $return[] = $entity
          ->get($id_key);
      }
      return $return;
    }
    return array(
      $entity
        ->id(),
    );
  }

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

  /**
   * Updates an entity with the contents of a row.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to update.
   * @param \Drupal\migrate\Row $row
   *   The row object to update from.
   */
  protected function updateEntity(EntityInterface $entity, Row $row) {
    foreach ($row
      ->getRawDestination() as $property => $value) {
      $this
        ->updateEntityProperty($entity, explode(Row::PROPERTY_SEPARATOR, $property), $value);
    }
    $this
      ->setRollbackAction($row
      ->getIdMap());
  }

  /**
   * Updates a (possible nested) entity property with a value.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The config entity.
   * @param array $parents
   *   The array of parents.
   * @param string|object $value
   *   The value to update to.
   */
  protected function updateEntityProperty(EntityInterface $entity, array $parents, $value) {
    $top_key = array_shift($parents);
    $entity_value = $entity
      ->get($top_key);
    if (is_array($entity_value)) {
      NestedArray::setValue($entity_value, $parents, $value);
    }
    else {
      $entity_value = $value;
    }
    $entity
      ->set($top_key, $entity_value);
  }

  /**
   * Generate an entity id.
   *
   * @param \Drupal\migrate\Row $row
   *   The current row.
   * @param array $ids
   *   The destination ids.
   *
   * @return string
   *   The generated entity id.
   */
  protected function generateId(Row $row, array $ids) {
    $id_values = array();
    foreach ($ids as $id) {
      $id_values[] = $row
        ->getDestinationProperty($id);
    }
    return implode('.', $id_values);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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. 1
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
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::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. 1
Entity::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
Entity::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
Entity::fields public function Returns an array of destination fields. Overrides MigrateDestinationInterface::fields
Entity::getEntity protected function Creates or loads an entity. 5
Entity::getEntityId protected function Get the entity id of the row. 2
Entity::getEntityTypeId protected static function Finds the entity type from configuration or plugin id. 3
Entity::getKey protected function Returns a specific entity key.
Entity::rollback public function Delete the specified destination object from the target Drupal. Overrides DestinationBase::rollback
Entity::__construct public function Construct a new entity. Overrides DestinationBase::__construct
EntityConfigBase::generateId protected function Generate an entity id.
EntityConfigBase::getIds public function Get the destination ids. Overrides MigrateDestinationInterface::getIds 3
EntityConfigBase::import public function Import the row. Overrides MigrateDestinationInterface::import 3
EntityConfigBase::updateEntity protected function Updates an entity with the contents of a row. 1
EntityConfigBase::updateEntityProperty protected function Updates a (possible nested) entity property with a value. 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 2
PluginBase::$pluginDefinition protected property The plugin implementation definition.
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
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
StringTranslationTrait::$stringTranslation protected property The string translation service.
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.