View source
<?php
namespace Drupal\workbench_access\Plugin\AccessControlHierarchy;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\system\MenuInterface;
use Drupal\workbench_access\AccessControlHierarchyBase;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\workbench_access\WorkbenchAccessManager;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Menu extends AccessControlHierarchyBase {
protected $menuTree;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return $instance
->setMenuTree($container
->get('menu.link_tree'));
}
public function setMenuTree(MenuLinkTreeInterface $menuTree) {
$this->menuTree = $menuTree;
return $this;
}
public function getTree() {
if (!isset($this->tree)) {
$tree = [];
$menuStorage = $this->entityTypeManager
->getStorage('menu');
foreach ($menuStorage
->loadMultiple($this->configuration['menus']) as $menu_id => $menu) {
$tree[$menu_id][$menu_id] = [
'label' => $menu
->label(),
'depth' => 0,
'parents' => [],
'weight' => 0,
'description' => $menu
->label(),
'path' => $menu
->toUrl('edit-form')
->toString(),
];
$params = new MenuTreeParameters();
$data = $this->menuTree
->load($menu_id, $params);
$this->tree = $this
->buildTree($menu_id, $data, $tree);
}
}
return $this->tree;
}
protected function buildTree($id, array $data, array &$tree) {
foreach ($data as $link_id => $link) {
$tree[$id][$link_id] = [
'id' => $link_id,
'label' => $link->link
->getTitle(),
'depth' => $link->depth,
'parents' => [],
'weight' => $link->link
->getWeight(),
'description' => $link->link
->getDescription(),
'path' => $link->link
->getUrlObject()
->toString(),
];
if ($parent = $link->link
->getParent()) {
$tree[$id][$link_id]['parents'] = array_unique(array_merge($tree[$id][$link_id]['parents'], [
$parent,
]));
$tree[$id][$link_id]['parents'] = array_unique(array_merge($tree[$id][$link_id]['parents'], $tree[$id][$parent]['parents']));
}
else {
$tree[$id][$link_id]['parents'] = [
$id,
];
}
if (isset($link->subtree)) {
uasort($link->subtree, [
$this,
'sortTree',
]);
$this
->buildTree($id, $link->subtree, $tree);
}
}
return $tree;
}
protected function sortTree($a, $b) {
if ($a->link
->getWeight() == $b->link
->getWeight()) {
return $a->link
->getTitle() > $b->link
->getTitle() ? 1 : 0;
}
return $a->link
->getWeight() > $b->link
->getWeight() ? 1 : 0;
}
public function alterForm(AccessSchemeInterface $scheme, array &$form, FormStateInterface &$form_state, ContentEntityInterface $entity) {
if (!isset($form['menu'])) {
return;
}
$element =& $form['menu'];
$menu_check = [];
$user_sections = $this->userSectionStorage
->getUserSections($scheme);
foreach ($element['link']['menu_parent']['#options'] as $id => $data) {
$parts = explode(':', $id);
$menu = array_shift($parts);
if (empty($parts)) {
$sections = [
$menu,
];
}
else {
$sections = [
implode(':', $parts),
];
}
$menu_parent = $menu . ':';
if (!empty($element['link']['menu_parent']['#default_value']) && $id != $menu_parent && empty(WorkbenchAccessManager::checkTree($scheme, $sections, $user_sections))) {
unset($element['link']['menu_parent']['#options'][$id]);
if ($id == $element['link']['menu_parent']['#default_value']) {
unset($element['link']['menu_parent']['#default_value']);
}
}
if (!isset($menu_check[$menu]) && isset($element['link']['menu_parent']['#options'][$menu . ':'])) {
if (empty(WorkbenchAccessManager::checkTree($scheme, [
$menu,
], $user_sections))) {
unset($element['link']['menu_parent']['#options'][$menu . ':']);
}
$menu_check[$menu] = TRUE;
}
}
if (!empty($element['link']['menu_parent']['#options']) && empty($element['link']['menu_parent']['#default_value'])) {
$element['link']['menu_parent']['#default_value'] = current(array_keys($element['link']['menu_parent']['#options']));
}
}
public function getEntityValues(EntityInterface $entity) {
$values = [];
$defaults = menu_ui_get_menu_link_defaults($entity);
if (!empty($defaults['id'])) {
$values = [
$defaults['id'],
];
}
return $values;
}
public function disallowedOptions($field) {
return [];
}
public function getViewsJoin($entity_type, $key, $alias = NULL) {
if ($entity_type == 'user') {
$configuration['menu'] = [
'table' => 'section_association__user_id',
'field' => 'user_id_target_id',
'left_table' => 'users',
'left_field' => $key,
'operator' => '=',
'table_alias' => 'section_association__user_id',
'real_field' => 'entity_id',
];
return $configuration;
}
else {
$configuration['menu'] = [
'table' => 'menu_tree',
'field' => 'route_param_key',
'left_table' => 'node',
'left_field' => $key,
'left_query' => "CONCAT('node=', {$alias}.{$key})",
'operator' => '=',
'table_alias' => 'menu_tree',
'real_field' => 'id',
];
}
return $configuration;
}
public function viewsData(array &$data, AccessSchemeInterface $scheme) {
$data['node']['workbench_access_section'] = [
'title' => t('Workbench Section @name', [
'@name' => $scheme
->label(),
]),
'help' => t('The sections to which this content belongs in the @name scheme.', [
'@name' => $scheme
->label(),
]),
'field' => [
'scheme' => $scheme
->id(),
'id' => 'workbench_access_section',
],
'filter' => [
'scheme' => $scheme
->id(),
'field' => 'nid',
'id' => 'workbench_access_section',
],
];
}
public function applies($entity_type_id, $bundle) {
return $entity_type_id === 'node' && in_array($bundle, $this->configuration['bundles']);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['menus'] = array_values(array_filter($form_state
->getValue('menus')));
$this->configuration['bundles'] = array_values(array_filter($form_state
->getValue('bundles')));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['menus'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Menus'),
'#description' => $this
->t('Select the menus to use.'),
'#options' => array_map(function (MenuInterface $menu) {
return $menu
->label();
}, $this->entityTypeManager
->getStorage('menu')
->loadMultiple()),
'#default_value' => $this->configuration['menus'],
];
$form['bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Content types'),
'#description' => $this
->t('Select the content types to enable access control on.'),
'#options' => array_map(function (NodeTypeInterface $node_type) {
return $node_type
->label();
}, $this->entityTypeManager
->getStorage('node_type')
->loadMultiple()),
'#default_value' => $this->configuration['bundles'],
];
return $form;
}
public function defaultConfiguration() {
return [
'menus' => [],
'bundles' => [],
];
}
public function calculateDependencies() {
$entity_type_map = [
'menu' => 'menus',
'node_type' => 'bundles',
];
$dependencies = [];
foreach ($entity_type_map as $entity_type => $configuration_key) {
$dependencies = array_merge($dependencies, $this->entityTypeManager
->getStorage($entity_type)
->loadMultiple($this->configuration[$configuration_key]));
}
return array_reduce($dependencies, function (array $carry, ConfigEntityInterface $entity) {
$carry[$entity
->getConfigDependencyKey()][] = $entity
->getConfigDependencyName();
return $carry;
}, []);
}
public function onDependencyRemoval(array $dependencies) {
$bundles = array_diff($this->configuration['bundles'], array_reduce($dependencies['config'], function (array $carry, $item) {
if (!$item instanceof NodeTypeInterface) {
return $carry;
}
$carry[] = $item
->id();
return $carry;
}, []));
$menus = array_diff($this->configuration['menus'], array_reduce($dependencies['config'], function (array $carry, $item) {
if (!$item instanceof NodeTypeInterface) {
return $carry;
}
$carry[] = $item
->id();
return $carry;
}, []));
$changed = $menus != $this->configuration['menus'] || $bundles != $this->configuration['bundles'];
$this->configuration['menus'] = $menus;
$this->configuration['bundles'] = $bundles;
return $changed;
}
}