You are here

class ToolbarMenuManager in Toolbar Menu 8.2

Same name and namespace in other branches
  1. 8 src/ToolbarMenuManager.php \Drupal\toolbar_menu\ToolbarMenuManager

Implement a setting form for toolbar_menu module.

Hierarchy

Expanded class hierarchy of ToolbarMenuManager

1 string reference to 'ToolbarMenuManager'
toolbar_menu.services.yml in ./toolbar_menu.services.yml
toolbar_menu.services.yml
1 service uses ToolbarMenuManager
toolbar_menu.manager in ./toolbar_menu.services.yml
Drupal\toolbar_menu\ToolbarMenuManager

File

src/ToolbarMenuManager.php, line 13

Namespace

Drupal\toolbar_menu
View source
class ToolbarMenuManager {
  use StringTranslationTrait;

  /**
   * 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_type_manager
   *   The entity manager.
   * @param \Drupal\Core\Session\AccountProxyInterface $account
   *   The account service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $account) {
    $this->account = $account
      ->getAccount();
    $this->toolbarMenuElements = $entity_type_manager
      ->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

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
ToolbarMenuManager::$account protected property The current account.
ToolbarMenuManager::$toolbarMenuElements protected property The toolbar menu elements entities.
ToolbarMenuManager::cleanId public function Helper to clean an ID.
ToolbarMenuManager::getPermissionList public function Get the permission list.
ToolbarMenuManager::getPermissionName protected function Get the formatted permission name.
ToolbarMenuManager::getToolbarMenuElements public function Get toolbar menu elements.
ToolbarMenuManager::__construct public function Construct a new ToolbarMenu.