class UpdateCacheSubscriber in Custom Contextual Links 8.2
Class EventSubscriber.
Hierarchy
- class \Drupal\ccl\EventSubscriber\UpdateCacheSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of UpdateCacheSubscriber
1 string reference to 'UpdateCacheSubscriber'
1 service uses UpdateCacheSubscriber
File
- src/
EventSubscriber/ UpdateCacheSubscriber.php, line 12
Namespace
Drupal\ccl\EventSubscriberView source
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',
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UpdateCacheSubscriber:: |
protected | property | The cache backend manager. | |
UpdateCacheSubscriber:: |
protected | property | The entity storage manager. | |
UpdateCacheSubscriber:: |
protected | property | The type of link to process for caching. | |
UpdateCacheSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
UpdateCacheSubscriber:: |
public | function | Respond to the ccl_update_cache event. | |
UpdateCacheSubscriber:: |
public | function | Prepare all links to be cached. | |
UpdateCacheSubscriber:: |
public | function | UpdateCacheSubscriber constructor. |