You are here

class DefaultTaxonomyHandler in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/cms_content_sync/entity_handler/DefaultTaxonomyHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultTaxonomyHandler
  2. 2.0.x src/Plugin/cms_content_sync/entity_handler/DefaultTaxonomyHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultTaxonomyHandler

Class DefaultTaxonomyHandler.

Plugin annotation


@EntityHandler(
  id = "cms_content_sync_default_taxonomy_handler",
  label = @Translation("Default Taxonomy"),
  weight = 90
)

Hierarchy

Expanded class hierarchy of DefaultTaxonomyHandler

1 file declares its use of DefaultTaxonomyHandler
PullIntent.php in src/PullIntent.php

File

src/Plugin/cms_content_sync/entity_handler/DefaultTaxonomyHandler.php, line 21

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler
View source
class DefaultTaxonomyHandler extends EntityHandlerBase {
  public const MAP_BY_LABEL_SETTING = 'map_by_label';
  public const USER_REVISION_PROPERTY = 'revision_user';

  /**
   * {@inheritdoc}
   */
  public static function supports($entity_type, $bundle) {
    return 'taxonomy_term' == $entity_type;
  }

  /**
   * {@inheritdoc}
   */
  public function getHandlerSettings($current_values, $type = 'both') {
    $options = parent::getHandlerSettings($current_values, $type);
    if ('push' !== $type) {
      $options[self::MAP_BY_LABEL_SETTING] = [
        '#type' => 'checkbox',
        '#title' => 'Map by name',
        '#default_value' => isset($current_values[self::MAP_BY_LABEL_SETTING]) ? $current_values[self::MAP_BY_LABEL_SETTING] : ($this
          ->shouldMapByLabel() ? 1 : 0),
      ];
    }
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPushOptions() {
    return [
      PushIntent::PUSH_DISABLED,
      PushIntent::PUSH_AUTOMATICALLY,
      PushIntent::PUSH_AS_DEPENDENCY,
      PushIntent::PUSH_MANUALLY,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPreviewOptions() {
    return [
      'table' => 'Table',
      'preview_mode' => 'Preview mode',
    ];
  }

  /**
   * @param \EdgeBox\SyncCore\Interfaces\Configuration\IDefineEntityType $definition
   */
  public function updateEntityTypeDefinition(&$definition) {
    parent::updateEntityTypeDefinition($definition);
    $definition
      ->addObjectProperty('parent', false);
  }

  /**
   * {@inheritdoc}
   */
  public function getForbiddenFields() {
    return array_merge(parent::getForbiddenFields(), [
      'parent',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function pull(PullIntent $intent) {
    $action = $intent
      ->getAction();
    if ($this
      ->ignorePull($intent)) {
      return false;
    }

    /**
     * @var \Drupal\Core\Entity\FieldableEntityInterface $entity
     */
    $entity = $intent
      ->getEntity();
    if (SyncIntent::ACTION_DELETE == $action) {
      if ($entity) {
        return $this
          ->deleteEntity($entity);
      }
      return false;
    }
    if (!$entity) {
      $entity_type = \Drupal::entityTypeManager()
        ->getDefinition($intent
        ->getEntityType());
      $label_property = $entity_type
        ->getKey('label');
      if ($this
        ->shouldMapByLabel()) {
        $existing = \Drupal::entityTypeManager()
          ->getStorage('taxonomy_term')
          ->loadByProperties([
          $label_property => $intent
            ->getOperation()
            ->getName(),
        ]);
        $existing = reset($existing);
        if (!empty($existing)) {
          return true;
        }
      }
      $base_data = [
        $entity_type
          ->getKey('bundle') => $intent
          ->getBundle(),
        $label_property => $intent
          ->getOperation()
          ->getName(),
      ];
      $base_data[$entity_type
        ->getKey('uuid')] = $intent
        ->getUuid();
      $storage = \Drupal::entityTypeManager()
        ->getStorage($intent
        ->getEntityType());
      $entity = $storage
        ->create($base_data);
      if (!$entity) {
        throw new SyncException(SyncException::CODE_ENTITY_API_FAILURE);
      }
      $intent
        ->setEntity($entity);
    }
    $parent_reference = $intent
      ->getProperty('parent');
    if ($parent_reference && ($parent = $intent
      ->loadEmbeddedEntity($parent_reference))) {
      $entity
        ->set('parent', [
        'target_id' => $parent
          ->id(),
      ]);
    }
    else {
      $entity
        ->set('parent', [
        'target_id' => 0,
      ]);
      if (!empty($parent_reference)) {
        $intent
          ->saveUnresolvedDependency($parent_reference, 'parent');
      }
    }
    if (!$this
      ->setEntityValues($intent)) {
      return false;
    }
    return true;
  }

  /**
   * {@inheritdoc}
   */
  public function push(PushIntent $intent, EntityInterface $entity = null) {

    /**
     * @var \Drupal\file\FileInterface $entity
     */
    if (!$entity) {
      $entity = $intent
        ->getEntity();
    }
    if (!parent::push($intent)) {
      return false;
    }
    $term_storage = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term');
    $parents = $term_storage
      ->loadParents($entity
      ->id());
    if (count($parents)) {
      $parent_term = reset($parents);
      $parent = $intent
        ->addDependency($parent_term);
      $intent
        ->setProperty('parent', $parent);
    }

    // Since taxonomy terms ain't got a created date, we set the changed
    // date instead during the first push.
    $status_entity = $intent
      ->getEntityStatus();
    if (is_null($status_entity
      ->getLastPush())) {
      $intent
        ->setProperty('created', (int) $entity
        ->getChangedTime());
    }
    return true;
  }

  /**
   * If set, terms will not be pulled if an identical term already exists. Instead, this term will be mapped when
   * pulling content that references it.
   */
  protected function shouldMapByLabel() {
    return isset($this->settings['handler_settings'][self::MAP_BY_LABEL_SETTING]) && 1 == $this->settings['handler_settings'][self::MAP_BY_LABEL_SETTING];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultTaxonomyHandler::getAllowedPreviewOptions public function Overrides EntityHandlerInterface::getAllowedPreviewOptions
DefaultTaxonomyHandler::getAllowedPushOptions public function Get the allowed push options. Overrides EntityHandlerBase::getAllowedPushOptions
DefaultTaxonomyHandler::getForbiddenFields public function Provide a list of fields that are not allowed to be pushed or pulled. These fields typically contain all label fields that are pushed separately anyway (we don't want to set IDs and revision IDs of entities for example, but only use the UUID for… Overrides EntityHandlerBase::getForbiddenFields
DefaultTaxonomyHandler::getHandlerSettings public function Get the handler settings. Overrides EntityHandlerBase::getHandlerSettings
DefaultTaxonomyHandler::MAP_BY_LABEL_SETTING public constant
DefaultTaxonomyHandler::pull public function Pull the remote entity. Overrides EntityHandlerBase::pull
DefaultTaxonomyHandler::push public function Overrides EntityHandlerBase::push
DefaultTaxonomyHandler::shouldMapByLabel protected function If set, terms will not be pulled if an identical term already exists. Instead, this term will be mapped when pulling content that references it.
DefaultTaxonomyHandler::supports public static function Check if this handler supports the given entity type. Overrides EntityHandlerInterface::supports
DefaultTaxonomyHandler::updateEntityTypeDefinition public function Overrides EntityHandlerBase::updateEntityTypeDefinition
DefaultTaxonomyHandler::USER_REVISION_PROPERTY public constant Overrides EntityHandlerBase::USER_REVISION_PROPERTY
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
EntityHandlerBase::$bundleName protected property
EntityHandlerBase::$entityTypeName protected property
EntityHandlerBase::$flow protected property A sync instance.
EntityHandlerBase::$logger protected property A logger instance.
EntityHandlerBase::$settings protected property
EntityHandlerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EntityHandlerBase::createNew protected function 1
EntityHandlerBase::deleteEntity protected function Delete a entity.
EntityHandlerBase::getAllowedPullOptions public function Get the allowed pull options. Overrides EntityHandlerInterface::getAllowedPullOptions 2
EntityHandlerBase::getStaticFields protected function Get a list of fields that can't be updated.
EntityHandlerBase::hasLabelProperty protected function Check whether the entity type supports having a label. 2
EntityHandlerBase::ignorePull protected function Check if the pull should be ignored. 2
EntityHandlerBase::ignorePush protected function Check if the entity should not be ignored from the push. 2
EntityHandlerBase::isEntityTypeTranslatable protected function
EntityHandlerBase::pushReferencedMenuItems protected function Whether or not menu item references should be pushed.
EntityHandlerBase::REVISION_TRANSLATION_AFFECTED_PROPERTY public constant 2
EntityHandlerBase::saveEntity protected function 1
EntityHandlerBase::setEntityValues protected function Set the values for the pulled entity. 2
EntityHandlerBase::setSourceUrl protected function
EntityHandlerBase::USER_PROPERTY public constant 2
EntityHandlerBase::validateHandlerSettings public function Validate the settings defined above. $form and $form_state are the same as in the Form API. $settings_key is the index at $form['sync_entities'] for this handler instance. Overrides EntityHandlerInterface::validateHandlerSettings
EntityHandlerBase::__construct public function Constructs a Drupal\rest\Plugin\ResourceBase object. Overrides PluginBase::__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.