public function FloatingMenuBlock::build in Floating 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/ FloatingMenuBlock.php, line 24
Class
- FloatingMenuBlock
- Provides a menu block
Namespace
Drupal\floating_menu\Plugin\BlockCode
public function build() {
$config = $this
->getConfiguration();
$max_items = $config['count_of_items'];
if (empty($max_items)) {
$max_items = 5;
}
$menu_item_variables = [];
for ($i = 0; $i < $max_items; $i++) {
$untranslated_url = $config['menu_item'][$i]['menu_item_target_url'];
if (!empty($untranslated_url) && substr($untranslated_url, 0, 1) == '/') {
$langManager = \Drupal::service('language_manager');
$path = \Drupal::service('path.alias_manager')
->getPathByAlias($untranslated_url, 'fi');
$translated_url = \Drupal::service('path.alias_manager')
->getAliasByPath($path, $langManager
->getCurrentLanguage()
->getId());
if ($langManager
->getCurrentLanguage()
->getId() != $langManager
->getDefaultLanguage()
->getId()) {
$translated_url = '/' . $langManager
->getCurrentLanguage()
->getId() . $translated_url;
}
}
else {
$translated_url = $untranslated_url;
}
if (!empty($config['menu_item'][$i]['menu_item_icon_url'])) {
$menu_item_variables[] = [
'popup_html' => [
'#markup' => $config['menu_item'][$i]['menu_item_popup_html']['value'],
],
'url' => $translated_url,
'icon_url' => $config['menu_item'][$i]['menu_item_icon_url'],
];
}
}
return array(
'#theme' => 'floating_menu_block',
'#attached' => array(
'library' => array(
'floating_menu/floating-menu',
),
),
'#menu_items' => $menu_item_variables,
);
}