/** * {@inheritdoc} */ public function menuLinkAccess(AccountInterface $account, MenuLinkInterface $menu_link_plugin = NULL) { $permission = 'administer ' . $menu_link_plugin->getMenuName() . ' menu items'; $permissions = $this::getPerMenuPermissions($account); if ($account->hasPermission('administer menu') || isset($permissions[$permission])) { return AccessResult::allowed(); } return AccessResult::neutral(); }
/** * {@inheritdoc} * * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin * The plugin instance to use for this form. */ public function buildForm(array $form, array &$form_state, MenuLinkInterface $menu_link_plugin = NULL) { $form['menu_link_id'] = array('#type' => 'value', '#value' => $menu_link_plugin->getPluginId()); $class_name = $menu_link_plugin->getFormClass(); $form['#plugin_form'] = $this->classResolver->getInstanceFromDefinition($class_name); $form['#plugin_form']->setMenuLinkInstance($menu_link_plugin); $form += $form['#plugin_form']->buildConfigurationForm($form, $form_state); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save'), '#button_type' => 'primary'); return $form; }
/** * Resets a standard menu link using the UI. * * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link * The Menu link. * @param int $old_weight * Original title for menu link. */ function resetMenuLink(MenuLinkInterface $menu_link, $old_weight) { // Reset menu link. $this->drupalPostForm("admin/structure/menu/link/{$menu_link->getPluginId()}/reset", array(), t('Reset')); $this->assertResponse(200); $this->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset'); // Verify menu link. $instance = \Drupal::service('plugin.manager.menu.link')->createInstance($menu_link->getPluginId()); $this->assertEqual($old_weight, $instance->getWeight(), 'Resets to the old weight.'); }
/** * Provide a single block on the administration overview page. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu item to be displayed. * * @return array * An array of menu items, as expected by theme_admin_block_content(). */ public function getAdminBlock(MenuLinkInterface $instance) { $content = array(); // Only find the children of this link. $link_id = $instance->getPluginId(); $parameters = new MenuTreeParameters(); $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->excludeHiddenLinks(); $tree = $this->menuTree->load(NULL, $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); foreach ($tree as $key => $element) { /** @var $link \Drupal\Core\Menu\MenuLinkInterface */ $link = $element->link; $content[$key]['title'] = $link->getTitle(); $content[$key]['options'] = $link->getOptions(); $content[$key]['description'] = $link->getDescription(); $content[$key]['url'] = $link->getUrlObject(); } ksort($content); return $content; }
/** * Checks access for one menu link instance. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu link instance. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ protected function menuLinkCheckAccess(MenuLinkInterface $instance) { $access_result = NULL; if ($this->account->hasPermission('link to any page')) { $access_result = AccessResult::allowed(); } else { $url = $instance->getUrlObject(); // When no route name is specified, this must be an external link. if (!$url->isRouted()) { $access_result = AccessResult::allowed(); } else { $access_result = $this->accessManager->checkNamedRoute($url->getRouteName(), $url->getRouteParameters(), $this->account, TRUE); } } return $access_result->cachePerPermissions(); }
/** * {@inheritdoc} */ public function setMenuLinkInstance(MenuLinkInterface $menu_link) { // Load the entity for the entity form. Loading by entity ID is much faster // than loading by UUID, so use that ID if we have it. $metadata = $menu_link->getMetaData(); if (!empty($metadata['entity_id'])) { $this->entity = $this->entityManager->getStorage('menu_link_content')->load($metadata['entity_id']); } else { // Fallback to the loading by UUID. $links = $this->entityManager->getStorage('menu_link_content')->loadByProperties(array('uuid' => $menu_link->getDerivativeId())); $this->entity = reset($links); } }
/** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $new_definition = $this->extractFormValues($form, $form_state); return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId(), $new_definition); }
/** * Resets the menu link to its default settings. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu link which should be reset. * * @return \Drupal\Core\Menu\MenuLinkInterface * The reset menu link. * * @throws \Drupal\Component\Plugin\Exception\PluginException * Thrown when the menu link is not resettable. */ protected function resetInstance(MenuLinkInterface $instance) { $id = $instance->getPluginId(); if (!$instance->isResettable()) { throw new PluginException("Menu link {$id} is not resettable"); } // Get the original data from disk, reset the override and re-save the menu // tree for this link. $definition = $this->getDefinitions()[$id]; $this->overrides->deleteOverride($id); $this->treeStorage->save($definition); return $this->createInstance($id); }
/** * Checks access for one menu link instance. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu link instance. * * @return bool * TRUE if the current user can access the link, FALSE otherwise. */ protected function menuLinkCheckAccess(MenuLinkInterface $instance) { // Use the definition here since that's a lot faster than creating a Url // object that we don't need. $definition = $instance->getPluginDefinition(); // 'url' should only be populated for external links. if (!empty($definition['url']) && empty($definition['route_name'])) { $access = TRUE; } else { $access = $this->accessManager->checkNamedRoute($definition['route_name'], $definition['route_parameters'], $this->account); } return $access; }
/** * Checks access based on whether the link can be reset. * * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin * The menu link plugin being checked. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ public function linkIsResettable(MenuLinkInterface $menu_link_plugin) { return AccessResult::allowedIf($menu_link_plugin->isResettable())->setCacheMaxAge(0); }
/** * {@inheritdoc} */ public function getCacheMaxAge() { return $this->wrappedLink->getCacheMaxAge(); }
/** * Checks access for one menu link instance. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu link instance. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ protected function menuLinkCheckAccess(MenuLinkInterface $instance) { $access_result = NULL; if ($this->account->hasPermission('link to any page')) { $access_result = AccessResult::allowed(); } else { // Use the definition here since that's a lot faster than creating a Url // object that we don't need. $definition = $instance->getPluginDefinition(); // 'url' should only be populated for external links. if (!empty($definition['url']) && empty($definition['route_name'])) { $access_result = AccessResult::allowed(); } else { $access_result = $this->accessManager->checkNamedRoute($definition['route_name'], $definition['route_parameters'], $this->account, TRUE); } } return $access_result->cachePerPermissions(); }
/** * Provide a single block on the administration overview page. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu item to be displayed. * * @return array * An array of menu items, as expected by admin-block-content.html.twig. */ public function getAdminBlock(MenuLinkInterface $instance) { $content = array(); // Only find the children of this link. $link_id = $instance->getPluginId(); $parameters = new MenuTreeParameters(); $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks(); $tree = $this->menuTree->load(NULL, $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); foreach ($tree as $key => $element) { // Only render accessible links. if (!$element->access->isAllowed()) { // @todo Bubble cacheability metadata of both accessible and // inaccessible links. Currently made impossible by the way admin // blocks are rendered. continue; } /** @var $link \Drupal\Core\Menu\MenuLinkInterface */ $link = $element->link; $content[$key]['title'] = $link->getTitle(); $content[$key]['options'] = $link->getOptions(); $content[$key]['description'] = $link->getDescription(); $content[$key]['url'] = $link->getUrlObject(); } ksort($content); return $content; }
/** * Checks access based on whether the link can be reset. * * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin * The menu link plugin being checked. * * @return string * AccessInterface::ALLOW when access was granted, otherwise * AccessInterface::DENY. */ public function linkIsResettable(MenuLinkInterface $menu_link_plugin) { return $menu_link_plugin->isResettable() ? AccessInterface::ALLOW : AccessInterface::DENY; }
/** * Processes entity route parameters for a given menu link. * * @param \Drupal\menu_link_config\MenuLinkConfigInterface|\Drupal\Core\Menu\MenuLinkInterface $menu_link * The menu link to process. This is being passed in to support * static::postLoad() * @param callable $processor * An entity route parameter processor that gets the entity type ID and the * current route parameter value as arguments and can return the processed * route parameter value or NULL if it does not want to alter the value. */ public static function processEntityRouteParameters($menu_link, $processor) { /** @var \Symfony\Component\Routing\Route $route */ $route = \Drupal::service('router.route_provider')->getRouteByName($menu_link->getRouteName()); $route_parameters = $menu_link->getRouteParameters(); $changed = FALSE; foreach ($route_parameters as $name => $value) { $parameter_info = $route->getOption('parameters'); // Ignore route parameters that are not entity IDs. if (isset($parameter_info[$name]['type']) && strpos($parameter_info[$name]['type'], 'entity:') === 0) { $entity_type_id = substr($parameter_info[$name]['type'], 7); $new_value = $processor($entity_type_id, $value); if (isset($new_value)) { $route_parameters[$name] = $new_value; $changed = TRUE; } } } if ($changed) { $menu_link->set('route_parameters', $route_parameters); } }