You are here

class SeedsManager in Seeds Toolbar 8

Helper function for SeedsToolbar.

Hierarchy

Expanded class hierarchy of SeedsManager

File

src/SeedsManager.php, line 11

Namespace

Drupal\seeds_toolbar
View source
class SeedsManager {

  /**
   * Language manager.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected $languageManager;

  /**
   * Current user.
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Indicates if the "block_content_permissions" module exists.
   *
   * @var bool
   */
  protected $blockContentPermissionsModule;

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $module_handler;

  /**
   * Constructor.
   */
  public function __construct($language_manager, $current_user, $entity_type_manager, $module_handler) {
    $this->languageManager = $language_manager;
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
    $this->module_handler = $module_handler;
    $this->blockContentPermissionsModule = $module_handler
      ->moduleExists('block_content_permissions');
  }

  /**
   * Build "Add" menu item.
   */
  public function buildMenu($entity_type, $route) {

    // Seeds toolbar should not depend on any entity type, se we check if the entity type exists or not.
    if ($this->entityTypeManager
      ->hasDefinition($entity_type)) {
      $entities = $this->entityTypeManager
        ->getStorage($entity_type)
        ->loadMultiple();
    }
    else {
      return [];
    }
    $items = [];
    foreach ($entities as $entity) {
      $id = $entity
        ->id();
      $url = Url::fromRoute($route, [
        $entity_type => $id,
      ]);

      // Block content are a special case, so we want to handle it.
      if ($entity_type == 'block_content_type') {
        $access = $this
          ->handleBlockPermission($entity);
      }
      else {
        $access = $this
          ->userHasAccess($url);
      }
      if ($access) {
        $items[$id] = Link::fromTextAndUrl($entity
          ->label(), $url);
      }
    }
    if (empty($items)) {
      return [];
    }
    else {
      return [
        '#menu_name' => $entity_type . '-add-menu',
        '#items' => $items,
        '#theme' => 'seeds_add_menu',
      ];
    }
  }

  /**
   * Check if the user has access to internal link.
   */
  private function userHasAccess($url) {
    return $url
      ->access($this->currentUser);
  }

  /**
   * Check if block_content_permission enabled.
   */
  private function handleBlockPermission($block_type) {
    if ($this->blockContentPermissionsModule) {
      $type_id = $block_type
        ->id();
      return $this->currentUser
        ->hasPermission("create {$type_id} block content");
    }
    else {
      return $this->currentUser
        ->hasPermission("administer blocks");
    }
  }

  /**
   * Get current language direction.
   */
  public function getDirection() {
    $dir = $this->languageManager
      ->getCurrentLanguage()
      ->getDirection();
    return $dir == 'ltr' ? 'left' : 'right';
  }

  /**
   * Sort main toolbar links.
   */
  public static function sortTabs(array $items, array $first_tabs) {
    $additonal_items = [];
    foreach ($items as $id => &$item) {
      if (in_array($id, $first_tabs)) {

        // Always put the defined tabs first.
        $item['#weight'] = array_search($id, $first_tabs);
      }
      elseif ($id == 'admin_toolbar_tools') {

        // Always put admin_toolbar_tools at the last tab.
        $item['#weight'] = 1000;
      }
      else {

        // Add additonal toolbar items which are added using hook_toolbar
        // Add a temp id to use it later.
        $item['temp_id'] = $id;

        // If the item doesn't have weight, assume it is 0.
        if (!isset($item['#weight'])) {
          $item['#weight'] = 0;
        }
        $additonal_items[] = $item;
        unset($items[$id]);
      }
    }

    // Sort the additional items by weight, then normalize them to be positive numbers.
    usort($additonal_items, function ($a, $b) {
      if ($a['#weight'] == $b['#weight']) {
        return 0;
      }
      return (int) $a['#weight'] > (int) $b['#weight'] ? 1 : -1;
    });

    // Add them to the items array.
    foreach ($additonal_items as $sorted_id => $additonal_item) {
      $additonal_item['#weight'] = min(count($first_tabs) + $sorted_id, 999);
      $id = $additonal_item['temp_id'];
      unset($additonal_item['temp_id']);
      $items[$id] = $additonal_item;
    }
    return $items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SeedsManager::$blockContentPermissionsModule protected property Indicates if the "block_content_permissions" module exists.
SeedsManager::$currentUser protected property Current user.
SeedsManager::$entityTypeManager protected property Entity type manager.
SeedsManager::$languageManager protected property Language manager.
SeedsManager::$module_handler protected property Module handler.
SeedsManager::buildMenu public function Build "Add" menu item.
SeedsManager::getDirection public function Get current language direction.
SeedsManager::handleBlockPermission private function Check if block_content_permission enabled.
SeedsManager::sortTabs public static function Sort main toolbar links.
SeedsManager::userHasAccess private function Check if the user has access to internal link.
SeedsManager::__construct public function Constructor.