You are here

protected function EntityReferenceHandlerBase::setValues in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/EntityReferenceHandlerBase.php \Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase::setValues()
  2. 2.0.x src/Plugin/EntityReferenceHandlerBase.php \Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase::setValues()
2 calls to EntityReferenceHandlerBase::setValues()
EntityReferenceHandlerBase::pull in src/Plugin/EntityReferenceHandlerBase.php
MergeableEntityReferenceHandler::setValues in src/Plugin/cms_content_sync/field_handler/MergeableEntityReferenceHandler.php
1 method overrides EntityReferenceHandlerBase::setValues()
MergeableEntityReferenceHandler::setValues in src/Plugin/cms_content_sync/field_handler/MergeableEntityReferenceHandler.php

File

src/Plugin/EntityReferenceHandlerBase.php, line 389

Class

EntityReferenceHandlerBase
Providing a base implementation for any reference field type.

Namespace

Drupal\cms_content_sync\Plugin

Code

protected function setValues(PullIntent $intent) {
  if ($intent
    ->shouldMergeChanges() && !$this
    ->forceMergeOverwrite()) {
    return false;
  }

  /**
   * @var \Drupal\Core\Entity\EntityInterface $entity
   */
  $entity = $intent
    ->getEntity();
  $data = $intent
    ->getProperty($this->fieldName);
  $values = [];
  foreach ($data ? $data : [] as $value) {
    $reference = $this
      ->loadReferencedEntity($intent, $value);
    if ($reference) {
      $info = $intent
        ->getEmbeddedEntityData($value);
      $attributes = $this
        ->getFieldValuesForReference($reference, $intent);
      if (is_array($attributes)) {
        $values[] = array_merge($info, $attributes);
      }
      else {
        $values[] = $attributes;
      }
    }
    elseif (!$this
      ->shouldEmbedReferencedEntities()) {

      // Shortcut: If it's just one value and a normal entity_reference field, the MissingDependencyManager will
      // directly update the field value of the entity and save it. Otherwise it will request a full pull of the
      // entity. So this saves some performance for simple references.
      if ('entity_reference' === $this->fieldDefinition
        ->getType() && !$this->fieldDefinition
        ->getFieldStorageDefinition()
        ->isMultiple()) {
        $intent
          ->saveUnresolvedDependency($value, $this->fieldName);
      }
      else {
        $intent
          ->saveUnresolvedDependency($value);
      }
    }
  }
  $entity
    ->set($this->fieldName, $values);
  return true;
}