You are here

public function DefaultMenuLinkContentHandler::pull in CMS Content Sync 8

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

Pull the remote entity.

Overrides EntityHandlerBase::pull

File

src/Plugin/cms_content_sync/entity_handler/DefaultMenuLinkContentHandler.php, line 148

Class

DefaultMenuLinkContentHandler
Class DefaultMenuLinkContentHandler, providing a minimalistic implementation for menu items, making sure they're referenced correctly by UUID.

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler

Code

public function pull(PullIntent $intent) {
  $link = $intent
    ->getProperty('link');
  if (isset($link[0]['uri'])) {
    $uri = $link[0]['uri'];
    preg_match('@^internal:/([a-z_0-9]+)\\/([a-z0-9-]+)$@', $uri, $found);
    if (!empty($found)) {
      $referenced = \Drupal::service('entity.repository')
        ->loadEntityByUuid($found[1], $found[2]);
      if (!$referenced) {
        $this->resolveDependent = [
          Entity::ENTITY_TYPE_KEY => $found[1],
          Entity::UUID_KEY => $found[2],
          'data' => [
            'enabled' => (bool) $intent
              ->getProperty('enabled')[0]['value'],
          ],
        ];
        $intent
          ->overwriteProperty('enabled', [
          [
            'value' => 0,
          ],
        ]);
      }
    }
  }
  elseif (!empty($link[0][Entity::ENTITY_TYPE_KEY]) && !empty($link[0][Entity::UUID_KEY])) {
    $referenced = $intent
      ->loadEmbeddedEntity($link[0]);
    if (!$referenced) {
      $this->resolveDependent = array_merge($link[0], [
        'data' => [
          'enabled' => (bool) $intent
            ->getProperty('enabled')[0]['value'],
        ],
      ]);
      $intent
        ->overwriteProperty('enabled', [
        [
          'value' => 0,
        ],
      ]);
    }
  }
  if (!parent::pull($intent)) {
    return false;
  }
  if ($this->resolveDependent) {
    $intent
      ->saveUnresolvedDependency($this->resolveDependent, 'link', $this->resolveDependent['data']);
  }
  return true;
}