menu_link_content_visibility.module in Custom Menu Links Visibility 8
File
menu_link_content_visibility.module
View source
<?php
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\menu_link_content\Plugin\Menu\MenuLinkContent;
use Drupal\menu_link_content_visibility\MenuLinkContentVisibilityCache;
use Drupal\menu_link_content_visibility\MenuLinkContentVisibilityChecker;
function menu_link_content_visibility_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type
->id() === 'menu_link_content') {
$fields['visibility'] = BaseFieldDefinition::create('menu_link_content_visibility')
->setLabel(t('Visibility'))
->setDisplayOptions('form', [
'type' => 'menu_link_content_visibility',
'weight' => 30,
]);
}
return $fields;
}
function menu_link_content_visibility_preprocess_menu(&$variables) {
$variables['items'] = _menu_link_content_visibility_preprocess_menu_items($variables['items']);
if (empty($variables['#cache']['contexts'])) {
$variables['#cache']['contexts'] = [];
}
$variables['#cache']['contexts'] = Cache::mergeContexts($variables['#cache']['contexts'], _menu_link_content_visibility_preprocess_menu_items_cache_contexts($variables['items']));
if (empty($variables['#cache']['tags'])) {
$variables['#cache']['tags'] = [];
}
$variables['#cache']['tags'] = Cache::mergeTags($variables['#cache']['tags'], _menu_link_content_visibility_preprocess_menu_items_cache_tags($variables['items']));
}
function _menu_link_content_visibility_preprocess_menu_items($items) {
foreach ($items as $key => $item) {
if (!_menu_link_content_visibility_preprocess_menu_item_is_visible($item)) {
unset($items[$key]);
}
else {
$items[$key]['below'] = _menu_link_content_visibility_preprocess_menu_items($item['below']);
if (empty($items[$key]['below'])) {
$items[$key]['is_expanded'] = FALSE;
}
}
}
return $items;
}
function _menu_link_content_visibility_preprocess_menu_item_is_visible($item) {
$original_link = $item['original_link'];
if ($original_link instanceof MenuLinkContent) {
$entity_id = $original_link
->getMetaData()['entity_id'];
return MenuLinkContentVisibilityChecker::createFromID(Drupal::getContainer(), $entity_id)
->isVisible();
}
return TRUE;
}
function _menu_link_content_visibility_preprocess_menu_items_cache_contexts($items) {
$contexts = [];
foreach ($items as $key => $item) {
$contexts = Cache::mergeContexts($contexts, _menu_link_content_visibility_preprocess_menu_item_cache_contexts($item), _menu_link_content_visibility_preprocess_menu_items_cache_contexts($item['below']));
}
return $contexts;
}
function _menu_link_content_visibility_preprocess_menu_item_cache_contexts($item) {
$original_link = $item['original_link'];
if ($original_link instanceof MenuLinkContent) {
$entity_id = $original_link
->getMetaData()['entity_id'];
return MenuLinkContentVisibilityCache::createFromID(Drupal::getContainer(), $entity_id)
->getCacheContexts();
}
return [];
}
function _menu_link_content_visibility_preprocess_menu_items_cache_tags($items) {
$tags = [];
foreach ($items as $key => $item) {
$tags = Cache::mergeTags($tags, _menu_link_content_visibility_preprocess_menu_item_cache_tags($item), _menu_link_content_visibility_preprocess_menu_items_cache_tags($item['below']));
}
return $tags;
}
function _menu_link_content_visibility_preprocess_menu_item_cache_tags($item) {
$original_link = $item['original_link'];
if ($original_link instanceof MenuLinkContent) {
$entity_id = $original_link
->getMetaData()['entity_id'];
return MenuLinkContentVisibilityCache::createFromID(Drupal::getContainer(), $entity_id)
->getCacheTags();
}
return [];
}
function menu_link_content_visibility_preprocess_superfish(&$variables) {
$variables['menu_items']['#tree'] = _menu_link_content_visibility_preprocess_superfish_items($variables['menu_items']['#tree']);
if (empty($variables['#cache']['contexts'])) {
$variables['#cache']['contexts'] = [];
}
$variables['#cache']['contexts'] = Cache::mergeContexts($variables['#cache']['contexts'], _menu_link_content_visibility_preprocess_superfish_items_cache_contexts($variables['menu_items']['#tree']));
if (empty($variables['#cache']['tags'])) {
$variables['#cache']['tags'] = [];
}
$variables['#cache']['tags'] = Cache::mergeTags($variables['#cache']['tags'], _menu_link_content_visibility_preprocess_superfish_items_cache_tags($variables['menu_items']['#tree']));
}
function _menu_link_content_visibility_preprocess_superfish_items($items) {
foreach ($items as $key => $item) {
if (!_menu_link_content_visibility_preprocess_superfish_item_is_visible($item)) {
unset($items[$key]);
}
else {
$items[$key]->subtree = _menu_link_content_visibility_preprocess_superfish_items($item->subtree);
}
}
return $items;
}
function _menu_link_content_visibility_preprocess_superfish_item_is_visible($item) {
$original_link = $item->link;
if ($original_link instanceof MenuLinkContent) {
$entity_id = $original_link
->getMetaData()['entity_id'];
return MenuLinkContentVisibilityChecker::createFromID(Drupal::getContainer(), $entity_id)
->isVisible();
}
return TRUE;
}
function _menu_link_content_visibility_preprocess_superfish_items_cache_contexts($items) {
$contexts = [];
foreach ($items as $key => $item) {
$contexts = Cache::mergeContexts($contexts, _menu_link_content_visibility_preprocess_superfish_item_cache_contexts($item), _menu_link_content_visibility_preprocess_superfish_items_cache_contexts($item->subtree));
}
return $contexts;
}
function _menu_link_content_visibility_preprocess_superfish_item_cache_contexts($item) {
$original_link = $item->link;
if ($original_link instanceof MenuLinkContent) {
$entity_id = $original_link
->getMetaData()['entity_id'];
return MenuLinkContentVisibilityCache::createFromID(Drupal::getContainer(), $entity_id)
->getCacheContexts();
}
return [];
}
function _menu_link_content_visibility_preprocess_superfish_items_cache_tags($items) {
$tags = [];
foreach ($items as $key => $item) {
$tags = Cache::mergeTags($tags, _menu_link_content_visibility_preprocess_superfish_item_cache_tags($item), _menu_link_content_visibility_preprocess_superfish_items_cache_tags($item->subtree));
}
return $tags;
}
function _menu_link_content_visibility_preprocess_superfish_item_cache_tags($item) {
$original_link = $item->link;
if ($original_link instanceof MenuLinkContent) {
$entity_id = $original_link
->getMetaData()['entity_id'];
return MenuLinkContentVisibilityCache::createFromID(Drupal::getContainer(), $entity_id)
->getCacheTags();
}
return [];
}