Skip to content
Snippets Groups Projects
Commit 37870d4c authored by Alexandre Mallet's avatar Alexandre Mallet Committed by Alexandre Mallet
Browse files

Issue #2893845 by woprrr, skybow, kyberman: Adjust Titles of routes add/edit as xxxx

parent e76009bd
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,6 @@ use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\form_mode_manager\FormModeManagerInterface;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......
......@@ -2,8 +2,6 @@
namespace Drupal\form_mode_manager\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
/**
* Controller for entity form mode support.
*
......
......@@ -52,18 +52,48 @@ class UserFormModeController extends EntityFormModeBase {
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param string $operation
* Name of current context operation to display title (create/edit).
*
* @return string
* The page title.
*/
public function addPageTitle(RouteMatchInterface $route_match) {
$form_mode_label = $route_match->getRouteObject()->getOption('parameters')['form_mode']['label'];
return $this->t('Create @name as @form_mode_label', [
public function pageTitle(RouteMatchInterface $route_match, $operation) {
$form_mode_label = $route_match->getRouteObject()
->getOption('parameters')['form_mode']['label'];
return $this->t('@op @name as @form_mode_label', [
'@name' => 'User',
'@form_mode_label' => $form_mode_label,
'@op' => $operation,
]);
}
/**
* The _title_callback for the entity.add routes.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*
* @return string
* The page title.
*/
public function addPageTitle(RouteMatchInterface $route_match) {
return $this->pageTitle($route_match, $this->t('Create'));
}
/**
* The _title_callback for the entity.add routes.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*
* @return string
* The page title.
*/
public function editPageTitle(RouteMatchInterface $route_match) {
return $this->pageTitle($route_match, $this->t('Edit'));
}
/**
* Retrieves entity from route match.
*
......
......@@ -327,12 +327,29 @@ class RouteSubscriber extends RouteSubscriberBase {
$entity_edit_route = $collection->get("entity.{$entity_type->id()}.edit_form");
if ($has_active_mode && !empty($entity_edit_route)) {
return $this->setRoutes($entity_edit_route, $entity_type, $form_mode);
$route = $this->setRoutes($entity_edit_route, $entity_type, $form_mode);
$this->userEditEnhancements($route, $entity_type->id());
return $route;
}
return NULL;
}
/**
* Set a specific callback for Edit context of User entity.
*
* @param \Symfony\Component\Routing\Route $route
* The route object of entity.
* @param string $entity_type_id
* The ID of the entity type.
*/
private function userEditEnhancements(Route $route, $entity_type_id) {
if ('user' !== $entity_type_id) {
return;
}
$route->setDefault('_title_callback', '\Drupal\form_mode_manager\Controller\UserFormModeController::editPageTitle');
}
/**
* Set Form Mode Manager routes based on parent entity routes.
*
......
......@@ -83,6 +83,13 @@ abstract class FormModeManagerBase extends BrowserTestBase {
*/
protected $nodeFormMode;
/**
* Basic user form mode to test.
*
* @var \Drupal\Core\Entity\EntityDisplayModeInterface
*/
protected $userFormMode;
/**
* Basic form mode to test.
*
......@@ -107,17 +114,14 @@ abstract class FormModeManagerBase extends BrowserTestBase {
]);
$this->nodeFormMode = $this->drupalCreateFormMode('node');
$this->userFormMode = $this->drupalCreateFormMode('user');
$this->container->get('router.builder')->rebuildIfNeeded();
$this->drupalLogin($this->rootUser);
$this->drupalGet("admin/structure/types/manage/{$this->nodeTypeFmm1->getEntityTypeId()}/form-display");
$form_mode_id = $this->nodeFormMode->id();
$this->formModeManager = $this->container->get('form_mode.manager');
$edit = ["display_modes_custom[{$this->formModeManager->getFormModeMachineName($form_mode_id)}]" => TRUE];
$this->drupalPostForm("admin/structure/types/manage/fmm_test/form-display", $edit, t('Save'));
$this->setUpNodeFormMode();
$this->setUpUserFormMode();
$this->setUpUsers();
}
......@@ -139,13 +143,39 @@ abstract class FormModeManagerBase extends BrowserTestBase {
'administer nodes',
'administer display modes',
'use node.default form mode',
'use user.default form mode',
'use ' . $this->nodeFormMode->id() . ' form mode',
'use ' . $this->userFormMode->id() . ' form mode',
'edit any ' . $this->nodeTypeFmm1->id() . ' content',
'create ' . $this->nodeTypeFmm1->id() . ' content',
]);
$this->testUser = $this->drupalCreateUser(['access content']);
}
/**
* Helper method to create Form mode onto Node entity needed for tests.
*/
public function setUpUserFormMode() {
$this->setUpFormMode("admin/config/people/accounts/form-display", $this->userFormMode->id());
}
/**
* Helper method to create Form mode onto Node entity needed for tests.
*/
public function setUpNodeFormMode() {
$this->setUpFormMode("admin/structure/types/manage/{$this->nodeTypeFmm1->id()}/form-display", $this->nodeFormMode->id());
}
/**
* Helper method to create all users needed for tests.
*/
public function setUpFormMode($path, $form_mode_id) {
$this->drupalGet($path);
$this->formModeManager = $this->container->get('form_mode.manager');
$edit = ["display_modes_custom[{$this->formModeManager->getFormModeMachineName($form_mode_id)}]" => TRUE];
$this->drupalPostForm($path, $edit, t('Save'));
}
/**
* Tests the EntityFormMode user interface.
*/
......
......@@ -93,6 +93,38 @@ class FormModeManagerRouteTest extends FormModeManagerBase {
$this->assertSession()->statusCodeEquals(200);
}
/**
* Asserts User Edit Form Mode Manager routes exists.
*/
public function testUserEditFormModeManagerRoutes() {
$this->drupalLogin($this->adminUser);
$user_form_mode_id = $this->formModeManager->getFormModeMachineName($this->userFormMode->id());
$this->drupalGet("user/{$this->rootUser->id()}/edit/$user_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
// Test not found form mode edit.
$user_form_mode_id = $this->formModeManager->getFormModeMachineName($this->userFormMode->id());
$this->drupalGet("user/{$this->adminUser->id()}/edit/not-valid-fm");
$this->assertSession()->statusCodeEquals(404);
// Test with just permission create xxx content.
Role::load($this->testUser->getRoles()[1])
->grantPermission("administer users")
->save();
$this->drupalLogin($this->testUser);
$this->drupalGet("user/{$this->testUser->id()}/edit/$user_form_mode_id");
$this->assertSession()->statusCodeEquals(403);
// Test with permission edit any xxx content and use xxx form mode.
Role::load($this->testUser->getRoles()[1])
->grantPermission("use {$this->userFormMode->id()} form mode")
->save();
$this->drupalLogin($this->testUser);
$this->drupalGet("user/{$this->adminUser->id()}/edit/$user_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
}
/**
* Asserts List With One Form Mode Manager routes exists.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment