You are here

public function DefaultLinkHandler::push in CMS Content Sync 2.1.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::push()
  2. 2.0.x src/Plugin/cms_content_sync/field_handler/DefaultLinkHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultLinkHandler::push()

Parameters

\Drupal\cms_content_sync\SyncIntent $intent:

Return value

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

Throws

\Drupal\cms_content_sync\Exception\SyncException

Overrides FieldHandlerBase::push

File

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

Class

DefaultLinkHandler
Providing a minimalistic implementation for any field type.

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler

Code

public function push(PushIntent $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;
  }
  $data = $entity
    ->get($this->fieldName)
    ->getValue();
  $absolute = !empty($this->settings['handler_settings']['export_as_absolute_url']);
  $result = [];
  foreach ($data as $key => $value) {
    $uri =& $data[$key]['uri'];

    // Find the linked entity and replace it's id with the UUID
    // References have following pattern: entity:entity_type/entity_id.
    preg_match('/^entity:(.*)\\/(\\d*)$/', $uri, $found);
    if (empty($found) || $absolute) {
      if ($absolute) {
        $uri = Url::fromUri($uri, [
          'absolute' => true,
        ])
          ->toString();
      }
      $result[] = [
        'uri' => $uri,
        'title' => isset($value['title']) ? $value['title'] : null,
        'options' => $value['options'],
      ];
    }
    else {
      $link_entity_type = $found[1];
      $link_entity_id = $found[2];
      $entity_manager = \Drupal::entityTypeManager();
      $link_entity = $entity_manager
        ->getStorage($link_entity_type)
        ->load($link_entity_id);
      if (empty($link_entity)) {
        continue;
      }
      if (!$this->flow
        ->getController()
        ->supportsEntity($link_entity)) {
        continue;
      }
      $result[] = $intent
        ->addReference($link_entity, [
        'title' => $value['title'],
        'options' => $value['options'],
      ]);
    }
  }
  $intent
    ->setProperty($this->fieldName, $result);
  return true;
}