class ToolbarMenuManager in Toolbar Menu 8
Same name and namespace in other branches
- 8.2 src/ToolbarMenuManager.php \Drupal\toolbar_menu\ToolbarMenuManager
Implement a setting form for toolbar_menu module.
Hierarchy
- class \Drupal\toolbar_menu\ToolbarMenuManager uses StringTranslationTrait
Expanded class hierarchy of ToolbarMenuManager
1 string reference to 'ToolbarMenuManager'
1 service uses ToolbarMenuManager
File
- src/
ToolbarMenuManager.php, line 13
Namespace
Drupal\toolbar_menuView source
class ToolbarMenuManager {
use StringTranslationTrait;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The current account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The toolbar menu elements entities.
*
* @var \Drupal\toolbar_menu\Entity\ToolbarMenuElement[]
*/
protected $toolbarMenuElements;
/**
* Construct a new ToolbarMenu.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Session\AccountProxyInterface $account
* The account service.
*/
public function __construct(EntityTypeManagerInterface $entity_manager, AccountProxyInterface $account) {
$this->entityManager = $entity_manager;
$this->account = $account
->getAccount();
$this->toolbarMenuElements = $this->entityManager
->getStorage('toolbar_menu_element')
->loadMultiple();
}
/**
* Get toolbar menu elements.
*
* @return \Drupal\toolbar_menu\Entity\ToolbarMenuElement[]
* An array containing toolbar menu elements.
*/
public function getToolbarMenuElements() {
$elements = [];
foreach ($this->toolbarMenuElements as $key => $element) {
if ($this->account
->hasPermission($this
->getPermissionName($element))) {
$elements[$key] = $element;
}
}
return $elements;
}
/**
* Helper to clean an ID.
*
* @param string $id
* The ID to clean.
*
* @return string
* The cleaned ID.
*/
public function cleanId($id) {
return preg_replace('/[^\\p{L}\\p{N}]/u', '-', $id);
}
/**
* Get the permission list.
*
* @return array
* The permission list.
*/
public function getPermissionList() {
$permissions = [];
foreach ($this->toolbarMenuElements as $element) {
$permissions[$this
->getPermissionName($element)] = [
'title' => $this
->t('View <em>@label</em> element in the toolbar', [
'@label' => $element
->label(),
]),
];
}
return $permissions;
}
/**
* Get the formatted permission name.
*
* @param \Drupal\toolbar_menu\Entity\ToolbarMenuElement $element
* The name of the menu.
*
* @return string
* The name of the permission.
*/
protected function getPermissionName(ToolbarMenuElement $element) {
return 'view ' . $element
->id() . ' in toolbar';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ToolbarMenuManager:: |
protected | property | The current account. | |
ToolbarMenuManager:: |
protected | property | The entity manager. | |
ToolbarMenuManager:: |
protected | property | The toolbar menu elements entities. | |
ToolbarMenuManager:: |
public | function | Helper to clean an ID. | |
ToolbarMenuManager:: |
public | function | Get the permission list. | |
ToolbarMenuManager:: |
protected | function | Get the formatted permission name. | |
ToolbarMenuManager:: |
public | function | Get toolbar menu elements. | |
ToolbarMenuManager:: |
public | function | Construct a new ToolbarMenu. |