protected function MenuPerRoleFunctionalTestBase::createOrUpdateMenuLink in Menu Per Role 8
Helper method to create or update a menu link.
Parameters
string $menuLinkTitle: The menu link title.
array $showMenuRoles: The roles which can see menu link.
array $hideMenuRoles: The roles which can't see menu link.
Throws
\Drupal\Core\Entity\EntityStorageException
1 call to MenuPerRoleFunctionalTestBase::createOrUpdateMenuLink()
- MenuPerRoleAccessTest::prepareMenuLinkAndExpectations in tests/
src/ Functional/ MenuPerRoleAccessTest.php - Prepare expectations for more performant testing.
File
- tests/
src/ Functional/ MenuPerRoleFunctionalTestBase.php, line 89
Class
- MenuPerRoleFunctionalTestBase
- Base class for Menu Per Role tests.
Namespace
Drupal\Tests\menu_per_role\FunctionalCode
protected function createOrUpdateMenuLink(string $menuLinkTitle, array $showMenuRoles, array $hideMenuRoles) : void {
$menu_link_storage = $this->entityTypeManager
->getStorage('menu_link_content');
/** @var \Drupal\menu_link_content\MenuLinkContentInterface[] $existing_menu_links */
$existing_menu_links = $menu_link_storage
->loadByProperties([
'title' => $menuLinkTitle,
]);
if (empty($existing_menu_links)) {
$menuLink = $menu_link_storage
->create([
'title' => $menuLinkTitle,
'link' => [
'uri' => 'internal:/',
],
'menu_name' => 'menu1',
'menu_per_role__show_role' => $showMenuRoles,
'menu_per_role__hide_role' => $hideMenuRoles,
]);
}
else {
$menuLink = array_shift($existing_menu_links);
$menuLink
->set('menu_per_role__show_role', $showMenuRoles);
$menuLink
->set('menu_per_role__hide_role', $hideMenuRoles);
}
$menuLink
->save();
}