You are here

UpdateCacheSubscriber.php in Custom Contextual Links 8.2

File

src/EventSubscriber/UpdateCacheSubscriber.php
View source
<?php

namespace Drupal\ccl\EventSubscriber;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class EventSubscriber.
 */
class UpdateCacheSubscriber implements EventSubscriberInterface {

  /**
   * The entity storage manager.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|mixed|object
   */
  protected $entityStorage;

  /**
   * The cache backend manager.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The type of link to process for caching.
   *
   * @var string
   */
  protected $linkType;

  /**
   * UpdateCacheSubscriber constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManager $entity_manager
   *   Inject EntityTypeManager.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Inject CacheBackend service.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManager $entity_manager, CacheBackendInterface $cache_backend) {
    $this->entityStorage = $entity_manager
      ->getStorage('custom_contextual_link');
    $this->cache = $cache_backend;
    $this->linkType = 'node';
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      'ccl_update_cache' => 'onUpdateCache',
    ];
  }

  /**
   * Respond to the ccl_update_cache event.
   */
  public function onUpdateCache() {
    $linkIds = $this->entityStorage
      ->getQuery()
      ->execute();

    /** @var \Drupal\ccl\Entity\CustomContextualLink[] $links */
    $links = $this->entityStorage
      ->loadMultiple($linkIds);
    $typeLinks = [];
    foreach ($links as $link) {
      if ($link
        ->getLinkType() == $this->linkType) {
        $typeLinks[] = $link;
      }
    }
    $this
      ->processLinks($typeLinks);
  }

  /**
   * Prepare all links to be cached.
   *
   * @param \Drupal\ccl\Entity\CustomContextualLink[] $links
   *   Array of CCL config entities.
   */
  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',
    ]);
  }

}

Classes

Namesort descending Description
UpdateCacheSubscriber Class EventSubscriber.