.../Drupal/Core/Menu/Form/MenuLinkDefaultForm.php | 15 ++++++-- .../Core/Menu/Form/MenuLinkFormInterface.php | 6 +-- .../menu_link_content.links.task.yml | 4 ++ .../menu_link_content.local_tasks.yml | 4 -- .../src/Controller/MenuController.php | 3 ++ .../src/Form/MenuLinkContentDeleteForm.php | 38 ++++++++++++++++-- .../src/Form/MenuLinkContentForm.php | 45 ++++++++++++++++------ .../src/Tests/MenuLinkContentUITest.php | 13 +------ core/modules/menu_ui/src/Form/MenuLinkEditForm.php | 2 +- 9 files changed, 93 insertions(+), 37 deletions(-) diff --git a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php index 1179bdc..ead546d 100644 --- a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php +++ b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php @@ -40,7 +40,7 @@ class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionIn protected $menuLinkManager; /** - * The menu tree service. + * The parent form selector service. * * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface */ @@ -61,7 +61,7 @@ class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionIn protected $moduleData; /** - * Constructs a new MenuLinkDefaultForm. + * Constructs a new \Drupal\Core\Menu\Form\MenuLinkDefaultForm. * * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager * The menu link manager. @@ -186,9 +186,16 @@ public function submitConfigurationForm(array &$form, array &$form_state) { /** * Helper function to get a module name. * - * This function is horrible, but core has nothing better until we add a + * @param string $module + * A module machine name. + * + * @todo This function is horrible, but core has nothing better until we add a * a method to the ModuleHandler that handles this nicely. - * @see https://drupal.org/node/2281989 + * https://drupal.org/node/2281989 + * + * @return string + * The human-readable, localized module name, or the machine name passed in + * if no matching module is found. */ protected function getModuleName($module) { // Gather module data. diff --git a/core/lib/Drupal/Core/Menu/Form/MenuLinkFormInterface.php b/core/lib/Drupal/Core/Menu/Form/MenuLinkFormInterface.php index cdc0f85..1e2e6f1 100644 --- a/core/lib/Drupal/Core/Menu/Form/MenuLinkFormInterface.php +++ b/core/lib/Drupal/Core/Menu/Form/MenuLinkFormInterface.php @@ -21,7 +21,7 @@ interface MenuLinkFormInterface extends PluginFormInterface { /** - * Injects the menu link instance. + * Injects the menu link plugin instance. * * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link * A menu link plugin instance. @@ -29,7 +29,7 @@ public function setMenuLinkInstance(MenuLinkInterface $menu_link); /** - * Form plugin helper. + * Form plugin helper which extracts a plugin definition from form values. * * @param array $form * An associative array containing the structure of the form. @@ -37,7 +37,7 @@ public function setMenuLinkInstance(MenuLinkInterface $menu_link); * An associative array containing the current state of the form. * * @return array - * The new plugin definition values takes from the form values. + * The new plugin definition values taken from the form values. */ public function extractFormValues(array &$form, array &$form_state); diff --git a/core/modules/menu_link_content/menu_link_content.links.task.yml b/core/modules/menu_link_content/menu_link_content.links.task.yml new file mode 100644 index 0000000..8195236 --- /dev/null +++ b/core/modules/menu_link_content/menu_link_content.links.task.yml @@ -0,0 +1,4 @@ +menu_link_content.link_edit: + route_name: menu_link_content.link_edit + base_route: menu_link_content.link_edit + title: Edit diff --git a/core/modules/menu_link_content/menu_link_content.local_tasks.yml b/core/modules/menu_link_content/menu_link_content.local_tasks.yml deleted file mode 100644 index 8195236..0000000 --- a/core/modules/menu_link_content/menu_link_content.local_tasks.yml +++ /dev/null @@ -1,4 +0,0 @@ -menu_link_content.link_edit: - route_name: menu_link_content.link_edit - base_route: menu_link_content.link_edit - title: Edit diff --git a/core/modules/menu_link_content/src/Controller/MenuController.php b/core/modules/menu_link_content/src/Controller/MenuController.php index 7ffa9f8..835e17b 100644 --- a/core/modules/menu_link_content/src/Controller/MenuController.php +++ b/core/modules/menu_link_content/src/Controller/MenuController.php @@ -10,6 +10,9 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\system\MenuInterface; +/** + * Define a route controller for the form for adding a menu link content entity. + */ class MenuController extends ControllerBase { /** diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentDeleteForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentDeleteForm.php index 4be850a..6c75d04 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentDeleteForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentDeleteForm.php @@ -7,8 +7,11 @@ namespace Drupal\menu_link_content\Form; +use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Entity\ContentEntityConfirmFormBase; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Url; +use Drupal\Core\Logger\LoggerChannelFactoryInterface; /** * Provides a delete form for content menu links. @@ -16,6 +19,36 @@ class MenuLinkContentDeleteForm extends ContentEntityConfirmFormBase { /** + * Logger channel. + * + * @var \Drupal\Core\Logger\LoggerChannelInterface + */ + protected $logger; + + /** + * Constructs a ContentEntityForm object. + * + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. + * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory + * The logger channel factory. + */ + public function __construct(EntityManagerInterface $entity_manager, LoggerChannelFactoryInterface $logger_factory) { + parent::__construct($entity_manager); + $this->logger = $logger_factory->get('menu'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager'), + $container->get('logger.factory') + ); + } + + /** * {@inheritdoc} */ public function getQuestion() { @@ -33,11 +66,10 @@ public function getCancelRoute() { * {@inheritdoc} */ public function submit(array $form, array &$form_state) { - $storage = $this->entityManager->getStorage('menu_link_content'); - $storage->delete(array($this->entity)); $t_args = array('%title' => $this->entity->getTitle()); + $this->entity->delete(); drupal_set_message($this->t('The menu link %title has been deleted.', $t_args)); - watchdog('menu', 'Deleted menu link %title.', $t_args, WATCHDOG_NOTICE); + $this->logger->notice('Deleted menu link %title.', $t_args); $form_state['redirect_route'] = array( 'route_name' => '', ); diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index 04cfac1..83671db 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -9,6 +9,8 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Url; +use Drupal\Core\Access\AccessManager; +use Drupal\Core\Session\AccountInterface; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -35,12 +37,12 @@ class MenuLinkContentForm extends ContentEntityForm implements MenuLinkFormInter /** * The content menu link. * - * @var \Drupal\menu_link_content\Entity\MenuLinkContent + * @var \Drupal\menu_link_content\Entity\MenuLinkContentInterface */ protected $entity; /** - * The menu tree service. + * The parent form selector service. * * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface */ @@ -61,6 +63,20 @@ class MenuLinkContentForm extends ContentEntityForm implements MenuLinkFormInter protected $languageManager; /** + * The access manager. + * + * @var \Drupal\Core\Access\AccessManager + */ + protected $accessManager; + + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $account; + + /** * Constructs a MenuLinkContentForm object. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager @@ -75,14 +91,20 @@ class MenuLinkContentForm extends ContentEntityForm implements MenuLinkFormInter * The request context. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Drupal\Core\Access\AccessManager $access_manager + * The access manager. + * @param \Drupal\Core\Session\AccountInterface $account + * The current user. */ - public function __construct(EntityManagerInterface $entity_manager, MenuParentFormSelectorInterface $menu_parent_selector, AliasManagerInterface $alias_manager, ModuleHandlerInterface $module_handler, RequestContext $request_context, LanguageManagerInterface $language_manager) { + public function __construct(EntityManagerInterface $entity_manager, MenuParentFormSelectorInterface $menu_parent_selector, AliasManagerInterface $alias_manager, ModuleHandlerInterface $module_handler, RequestContext $request_context, LanguageManagerInterface $language_manager, AccessManager $access_manager, AccountInterface $account) { parent::__construct($entity_manager, $language_manager); $this->menuParentSelector = $menu_parent_selector; $this->pathAliasManager = $alias_manager; $this->moduleHandler = $module_handler; $this->requestContext = $request_context; $this->languageManager = $language_manager; + $this->accessManager = $access_manager; + $this->account = $account; } /** @@ -95,7 +117,9 @@ public static function create(ContainerInterface $container) { $container->get('path.alias_manager'), $container->get('module_handler'), $container->get('router.request_context'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('access_manager'), + $container->get('current_user') ); } @@ -103,7 +127,8 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function setMenuLinkInstance(MenuLinkInterface $menu_link) { - // Load the entity for the entity form. + // 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']); @@ -165,7 +190,7 @@ public function submitEditForm(array &$form, array &$form_state) { } /** - * Break up a user-entered URL or path into all the relevant parts. + * Breaks up a user-entered URL or path into all the relevant parts. * * @param string $url * The user-entered URL or path. @@ -208,7 +233,6 @@ protected function extractUrl($url) { * {@inheritdoc} */ public function extractFormValues(array &$form, array &$form_state) { - $new_definition = array(); $new_definition['expanded'] = !empty($form_state['values']['expanded']) ? 1 : 0; $new_definition['hidden'] = empty($form_state['values']['enabled']) ? 1 : 0; @@ -240,7 +264,6 @@ public function extractFormValues(array &$form, array &$form_state) { * {@inheritdoc} */ public function form(array $form, array &$form_state) { - $form = parent::form($form, $form_state); $language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('menu_link_content', 'menu_link_content')); @@ -362,7 +385,7 @@ public function save(array $form, array &$form_state) { $saved = $menu_link->save(); if ($saved) { - drupal_set_message(t('The menu link has been saved.')); + drupal_set_message($this->t('The menu link has been saved.')); $form_state['redirect_route'] = array( 'route_name' => 'menu_link_content.link_edit', 'route_parameters' => array( @@ -371,7 +394,7 @@ public function save(array $form, array &$form_state) { ); } else { - drupal_set_message(t('There was an error saving the menu link.'), 'error'); + drupal_set_message($this->t('There was an error saving the menu link.'), 'error'); $form_state['rebuild'] = TRUE; } } @@ -390,7 +413,7 @@ protected function doValidate(array $form, array &$form_state) { } elseif ($extracted['route_name']) { // Users are not allowed to add a link to a page they cannot access. - $valid = \Drupal::service('access_manager')->checkNamedRoute($extracted['route_name'], $extracted['route_parameters'], \Drupal::currentUser()); + $valid = $this->accessManager->checkNamedRoute($extracted['route_name'], $extracted['route_parameters'], $this->account); } if (!$valid) { $this->setFormError('url', $form_state, $this->t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $form_state['values']['url']))); diff --git a/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php b/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php index 052495a..c805578 100644 --- a/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php +++ b/core/modules/menu_link_content/src/Tests/MenuLinkContentUITest.php @@ -11,6 +11,8 @@ /** * Tests the menu link content UI. + * + * @group Menu */ class MenuLinkContentUITest extends ContentTranslationUITest { @@ -29,17 +31,6 @@ class MenuLinkContentUITest extends ContentTranslationUITest { /** * {@inheritdoc} */ - public static function getInfo() { - return array( - 'name' => 'Menu link content translation UI', - 'description' => 'Tests the basic menu link content translation UI.', - 'group' => 'Menu', - ); - } - - /** - * {@inheritdoc} - */ public function setUp() { $this->entityTypeId = 'menu_link_content'; $this->bundle = 'menu_link_content'; diff --git a/core/modules/menu_ui/src/Form/MenuLinkEditForm.php b/core/modules/menu_ui/src/Form/MenuLinkEditForm.php index 5207e0f..3965a51 100644 --- a/core/modules/menu_ui/src/Form/MenuLinkEditForm.php +++ b/core/modules/menu_ui/src/Form/MenuLinkEditForm.php @@ -71,7 +71,7 @@ public function buildForm(array $form, array &$form_state, MenuLinkInterface $me $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', - '#value' => t('Save'), + '#value' => $this->t('Save'), '#button_type' => 'primary', ); return $form;