You are here

public function UltimenuBlock::build in Ultimenu 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/UltimenuBlock.php \Drupal\ultimenu\Plugin\Block\UltimenuBlock::build()

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/UltimenuBlock.php, line 146

Class

UltimenuBlock
Provides an 'Ultimenu' block.

Namespace

Drupal\ultimenu\Plugin\Block

Code

public function build() {
  $menu_name = $this
    ->delta();
  $skin = $this->configuration['skin'];
  $orientation = $this->configuration['orientation'];
  $submenu = !empty($this->configuration['submenu']);
  $provider = '';
  $skin_name = '';
  $skin_basename = '';
  $skin_css_path = '';

  // Load the specified block skin.
  if (!empty($skin)) {
    $skin_css_path = $this->ultimenuManager
      ->getSkinPath($skin);
    $skin_basename = $this->ultimenuManager
      ->getSkinName($skin_css_path);

    // Fetch the skin file name from the setting.
    list($provider, $skin_name) = array_pad(array_map('trim', explode("|", $skin, 2)), 2, NULL);
  }

  // Provide the settings for further process.
  $configs = [
    'skin' => $skin,
    'orientation' => $orientation,
    'delta' => $menu_name,
    'menu_name' => $menu_name,
    'skin_name' => $skin_name,
    'skin_provider' => $provider,
    'submenu' => $submenu,
    'submenu_position' => $submenu && !empty($this->configuration['submenu_position']) ? $this->configuration['submenu_position'] : '',
  ];

  // Build the ultimenu tree.
  $tree = $this->ultimenuManager
    ->buildMenuTree($menu_name, $configs);

  // Allows alteration of the tree before passing to template.
  // @see https://drupal.org/node/1894902
  $this->ultimenuManager
    ->getModuleHandler()
    ->alter('ultimenu_tree', $tree);

  // Build the block.
  $build = [];
  if ($tree) {
    $build = [
      '#theme' => 'ultimenu',
      '#items' => $tree,
      '#config' => $configs,
      '#delta' => $menu_name,
    ];
    $build['#attached']['library'][] = 'ultimenu/ultimenu';
    if ($skin_basename) {
      $build['#attached']['library'][] = 'ultimenu/ultimenu.skin.' . $skin_basename;
    }
  }
  return $build;
}