You are here

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

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

Parameters

\Drupal\cms_content_sync\SyncIntent $intent: The request containing all pushed data

Return value

bool Whether or not the content has been pulled. FALSE is a desired state, meaning the entity should not be pulled according to config.

Throws

\Drupal\cms_content_sync\Exception\SyncException

Overrides FieldHandlerBase::pull

File

src/Plugin/cms_content_sync/field_handler/DefaultLinkHandler.php, line 56

Class

DefaultLinkHandler
Providing a minimalistic implementation for any field type.

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler

Code

public function pull(PullIntent $intent) {
  $action = $intent
    ->getAction();

  /**
   * @var \Drupal\Core\Entity\FieldableEntityInterface $entity
   */
  $entity = $intent
    ->getEntity();

  // Deletion doesn't require any action on field basis for static data.
  if (SyncIntent::ACTION_DELETE == $action) {
    return false;
  }
  if ($intent
    ->shouldMergeChanges()) {
    return false;
  }
  $data = $intent
    ->getProperty($this->fieldName);
  if (empty($data)) {
    $entity
      ->set($this->fieldName, null);
  }
  else {
    $result = [];
    foreach ($data as &$link_element) {
      if (empty($link_element['uri'])) {
        try {
          $reference = $intent
            ->loadEmbeddedEntity($link_element);
        } catch (\Exception $e) {
          $reference = null;
        }
        if ($reference) {
          $result[] = [
            'uri' => 'entity:' . $reference
              ->getEntityTypeId() . '/' . $reference
              ->id(),
            'title' => $link_element['title'],
            'options' => $link_element['options'],
          ];
        }
        elseif (!empty($reference[Entity::ENTITY_TYPE_KEY]) && !empty($link_element[Entity::BUNDLE_KEY])) {
          if ($reference) {
            $result[] = [
              'uri' => 'entity:' . $reference
                ->getEntityTypeId() . '/' . $reference
                ->id(),
              'title' => $link_element['title'],
              'options' => $link_element['options'],
            ];
          }
          elseif ($entity instanceof MenuLinkContent && 'link' == $this->fieldName) {
            $result[] = [
              'uri' => 'internal:/' . $link_element[Entity::ENTITY_TYPE_KEY] . '/' . $link_element[Entity::UUID_KEY],
              'title' => $link_element['title'],
              'options' => $link_element['options'],
            ];
          }
        }
      }
      else {
        $result[] = [
          'uri' => $link_element['uri'],
          'title' => $link_element['title'],
          'options' => $link_element['options'],
        ];
      }
    }
    $entity
      ->set($this->fieldName, $result);
  }
  return true;
}