You are here

public function OgMenuBlock::build in Organic Groups Menu (OG Menu) 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/OgMenuBlock.php, line 172

Class

OgMenuBlock
Provides a generic Menu block.

Namespace

Drupal\og_menu\Plugin\Block

Code

public function build() {
  $menu_name = $this
    ->getMenuName();
  $parameters = $this->menuTree
    ->getCurrentRouteMenuTreeParameters($menu_name);

  // Adjust the menu tree parameters based on the block's configuration.
  $level = $this->configuration['level'];
  $depth = $this->configuration['depth'];
  $parameters
    ->setMinDepth($level);

  // When the depth is configured to zero, there is no depth limit. When depth
  // is non-zero, it indicates the number of levels that must be displayed.
  // Hence this is a relative depth that we must convert to an actual
  // (absolute) depth, that may never exceed the maximum depth.
  if ($depth > 0) {
    $parameters
      ->setMaxDepth(min($level + $depth - 1, $this->menuTree
      ->maxDepth()));
  }
  $tree = $this->menuTree
    ->load($menu_name, $parameters);
  $manipulators = array(
    array(
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ),
    array(
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ),
  );
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  $build = $this->menuTree
    ->build($tree);
  if (!$tree) {
    $route_name = 'entity.ogmenu_instance.create';

    /** @var \Drupal\Core\Entity\EntityInterface $og_entity */
    $og_entity = $this
      ->getContext('og')
      ->getContextData()
      ->getValue();
    $route_parameters = [
      'ogmenu' => $this
        ->getDerivativeId(),
      'og_group_entity_type' => $og_entity
        ->getEntityTypeId(),
      'og_group' => $og_entity
        ->id(),
    ];
    $access = $this->accessManager
      ->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
    $build['create'] = array(
      '#theme' => 'menu_local_action',
      '#link' => array(
        'title' => $this
          ->t('Add menu'),
        'url' => Url::fromRoute('entity.ogmenu_instance.create', $route_parameters),
      ),
      '#access' => $access,
    );
  }
  $menu_instance = $this
    ->getOgMenuInstance();
  if ($menu_instance instanceof OgMenuInstanceInterface) {
    $build['#contextual_links']['ogmenu'] = array(
      'route_parameters' => array(
        'ogmenu_instance' => $menu_instance
          ->id(),
      ),
    );
  }
  if ($menu_instance) {
    $menu_name = $menu_instance
      ->getType();
    $build['#theme'] = 'menu__og__' . strtr($menu_name, '-', '_');
  }
  return $build;
}