You are here

class IdsCleaner in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Plugin/SyncNormalizerDecorator/IdsCleaner.php \Drupal\content_sync\Plugin\SyncNormalizerDecorator\IdsCleaner

Provides a decorator for setting the alias to entity.

Plugin annotation


@SyncNormalizerDecorator(
  id = "id_cleaner",
  name = @Translation("IDs Cleaner"),
)

Hierarchy

Expanded class hierarchy of IdsCleaner

File

src/Plugin/SyncNormalizerDecorator/IdsCleaner.php, line 17

Namespace

Drupal\content_sync\Plugin\SyncNormalizerDecorator
View source
class IdsCleaner extends SyncNormalizerDecoratorBase {
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * @param array $normalized_entity
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   * @param $format
   * @param array $context
   */
  public function decorateNormalization(array &$normalized_entity, ContentEntityInterface $entity, $format, array $context = []) {
    $this
      ->cleanReferenceIds($normalized_entity, $entity);
    $this
      ->cleanIds($normalized_entity, $entity);
  }

  /**
   * @param $normalized_entity
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *
   * @return mixed
   */
  protected function cleanReferenceIds(&$normalized_entity, ContentEntityInterface $entity) {
    $field_definitions = $entity
      ->getFieldDefinitions();
    foreach ($field_definitions as $field_name => $field_definition) {

      // We are only interested in importing content entities.
      if (!is_a($field_definition
        ->getClass(), '\\Drupal\\Core\\Field\\EntityReferenceFieldItemList', TRUE)) {
        continue;
      }
      if (isset($normalized_entity[$field_name]) && !empty($normalized_entity[$field_name]) && is_array($normalized_entity[$field_name])) {
        $entity_type = $field_definition
          ->getFieldStorageDefinition()
          ->getSetting('target_type');
        $reflection = new \ReflectionClass(\Drupal::entityTypeManager()
          ->getDefinition($entity_type)
          ->getClass());
        if (!$reflection
          ->implementsInterface('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {
          continue;
        }
        $key = $field_definition
          ->getFieldStorageDefinition()
          ->getMainPropertyName();
        foreach ($normalized_entity[$field_name] as &$item) {
          if (!empty($item[$key])) {
            unset($item[$key]);
          }
          if (!empty($item['url'])) {
            unset($item['url']);
          }
        }
      }
    }
    return $normalized_entity;
  }

  /**
   * @param $normalized_entity
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *
   * @return mixed
   */
  protected function cleanIds(&$normalized_entity, ContentEntityInterface $entity) {
    $keys = $entity
      ->getEntityType()
      ->getKeys();
    if (isset($normalized_entity[$keys['id']])) {
      unset($normalized_entity[$keys['id']]);
    }
    if (isset($keys['revision']) && isset($normalized_entity[$keys['revision']])) {
      unset($normalized_entity[$keys['revision']]);
    }
    return $normalized_entity;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IdsCleaner::cleanIds protected function
IdsCleaner::cleanReferenceIds protected function
IdsCleaner::decorateNormalization public function Overrides SyncNormalizerDecoratorBase::decorateNormalization
IdsCleaner::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
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.
SyncNormalizerDecoratorBase::decorateDenormalization public function Apply decoration for the denormalization process. Overrides SyncNormalizerDecoratorInterface::decorateDenormalization
SyncNormalizerDecoratorBase::decorateDenormalizedEntity public function Apply decoration to the entity after the denormalization process is done. Overrides SyncNormalizerDecoratorInterface::decorateDenormalizedEntity