You are here

public function UpdateCacheSubscriber::processLinks in Custom Contextual Links 8.2

Prepare all links to be cached.

Parameters

\Drupal\ccl\Entity\CustomContextualLink[] $links: Array of CCL config entities.

1 call to UpdateCacheSubscriber::processLinks()
UpdateCacheSubscriber::onUpdateCache in src/EventSubscriber/UpdateCacheSubscriber.php
Respond to the ccl_update_cache event.

File

src/EventSubscriber/UpdateCacheSubscriber.php, line 92

Class

UpdateCacheSubscriber
Class EventSubscriber.

Namespace

Drupal\ccl\EventSubscriber

Code

public function processLinks(array $links) {
  $nodeCache = [
    'globalLinks' => [],
    'contentTypeLinks' => [],
    'nodeLinks' => [],
  ];
  foreach ($links as $link) {
    $css = $link
      ->getAdvancedOption('css');
    $target = $link
      ->getAdvancedOption('target');
    $url = $link
      ->get('link');
    $element = [
      'title' => $link
        ->get('title'),
      'url' => strpos($url, '/') !== 0 ? $url : 'internal:' . $url,
      'urlOptions' => [
        'query' => $link
          ->getAdvancedOption('query'),
        'fragment' => $link
          ->getAdvancedOption('anchor'),
      ],
      'attributes' => [
        'class' => $css ? explode(' ', $css) : '',
        'target' => $target != 'default' ? $target : '',
      ],
    ];
    switch ($link
      ->getLinkOption('node_option')) {
      case 'node':
        $nodeCache['nodeLinks'][$link
          ->getLinkOption('node_id')][] = $element;
        break;
      case 'ct':
        $nodeCache['contentTypeLinks'][$link
          ->getLinkOption('node_type')][] = $element;
        break;
      case 'global':
        $nodeCache['globalLinks'][] = $element;
        break;
    }
  }
  $this->cache
    ->set('ccl:nodes', $nodeCache, CacheBackendInterface::CACHE_PERMANENT, [
    'ccl',
  ]);
}