You are here

public function DefaultTaxonomyHandler::pull in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/cms_content_sync/entity_handler/DefaultTaxonomyHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultTaxonomyHandler::pull()
  2. 2.1.x src/Plugin/cms_content_sync/entity_handler/DefaultTaxonomyHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultTaxonomyHandler::pull()

Pull the remote entity.

Overrides EntityHandlerBase::pull

File

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

Class

DefaultTaxonomyHandler
Class DefaultTaxonomyHandler.

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler

Code

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;
}