Thanks for your response. I will check with this.
The custom block is set to be displayed in a custom content type based on the menu hierarchy. The child menu items have content tagged to them which will be dynamically displayed in the Landing page(Custom content type) as block.
This is the code for block:
<?php
/**
* @file
* Contains \Drupal\ob_landing_page\Plugin\Block\
*
*/
namespace Drupal\ob_landing_page\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\media\Entity\Media;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Provides a full list of services
*
* @Block(
* id = "ob_landing_page_block",
* admin_label = @Translation("Landing Page - Content Block"),
* category = @Translation("Orange Bus")
* )
*/
class LandingPage extends BlockBase
{
/**
* {@inheritdoc}
*/
public function build()
{
$menu = $this->buildLandingPageData();
if ($menu['is_landing_page']) {
return array(
'#cache' => array(
'max-age' => 0,
),
'#data' => array(
'menu' => $menu,
),
'#theme' => 'ob_landing_page',
);
}
}
protected function buildLandingPageData()
{
$menu = array();
$current_link = '';
$summary = '';
$icon = '';
$media = '';
// Load menu
$menu_tree = \Drupal::menuTree();
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters('main');
$main_menu = $menu_tree->load('main', $parameters);
$manipulators = array(
// Only show links that are accessible for the current user.
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
// Use the default sorting of menu links.
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$main_menu = $menu_tree->transform($main_menu, $manipulators);
$menu_tmp = $menu_tree->build($main_menu);
// Get parents
$parents = $parameters->expandedParents;
foreach ($menu_tmp['#items'] as $item) {
if (!empty($item['in_active_trail'])) {
$current_link = $this->getCurrentLink($item);
$menu['is_landing_page'] = true;
break;
}
}
// If there are no children in the menu this is not a landing page
if (!$current_link) {
$menu['is_landing_page'] = false;
return $menu;
}
// Set details for the current landing page
$menu['parent'] = array(
'title' => $current_link['title'],
'url' => $current_link['url']->getInternalPath(),
);
// loop through each child item and build array
if (!empty($current_link['below'])) {
foreach ($current_link['below'] as $child) {
// check if the URL is external as this is handled differently to internal links
$external_link = $child['url']->isExternal();
$uri = ($external_link) ? $child['url']->getUri() : $child['url']->getInternalPath();
$summary = '';
$media_icon = '';
// handle internal linked pages
if (!$external_link) {
$current_route_name = \Drupal::service('current_route_match')->getRouteName();
$nid = $this->getNodeID($uri);
$node = (!empty($nid)) ? Node::load($nid) : '';
if ($node) {
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath('/' . $uri);
$summary_available = ($node->hasField('field_ob_node_summary') && !empty($node->get('field_ob_node_summary')->getValue()[0]['target_id']));
if ($summary_available) {
$page_details = $node->get('field_ob_node_summary')->getValue()[0]['target_id'];
$paragraph = Paragraph::load($page_details);
$summary = (!$paragraph->field_ob_summary_description->isEmpty()) ? str_replace("\n", "</p>\n<p>", $paragraph->field_ob_summary_description->value) : '';
$icon = (!$paragraph->field_ob_media_library_image->isEmpty()) ? $paragraph->get('field_ob_media_library_image')->first() : '';
$fa_icon = (!$paragraph->field_ob_font_awesome_icon->isEmpty()) ? $paragraph->field_ob_font_awesome_icon->value : '';
}
if (!empty($icon)) {
$entities = $this->getMediaEntity($icon->get('entity')->getTarget());
if ($entities['file']) {
$media_icon = $this->getImageProperties($entities['file'], $entities['img']);
}
}
$menu['children'][] = array(
'title' => $child['title'],
'uri' => $path_alias,
'summary' => $summary,
'icon' => (!empty($media_icon)) ? $media_icon : '',
'fa_icon' => $fa_icon,
);
} else {
$menu['children'][] = array(
'title' => $child['title'],
'uri' => $uri,
);
}
}
// handle external links
if ($external_link) {
$menu['children'][] = array(
'title' => $child['title'],
'uri' => $uri,
);
}
}
}
return $menu;
}
public function getCurrentLink($item)
{
$item_path = $item['url']->getInternalPath();
$is_active = $item;
$current_path = \Drupal::service('path.current')->getPath();
if ($current_path == ('/' . $item_path)) {
return $is_active;
}
foreach ($is_active['below'] as $child) {
if ($child['in_active_trail']) {
$child_path = $child['url']->getInternalPath();
if ($current_path == ('/' . $child_path)) {
return $child;
} else {
return $this->getCurrentLink($child);
}
}
}
return $item;
}
public function getNodeID($path)
{
if (!empty($path)) {
$path_args = explode('/', $path);
if ($path_args[0] == '') {
if ($path_args[1] == 'node') {
return $path_args[2];
}
} else {
if ($path_args[0] == 'node') {
return $path_args[1];
}
}
}
return false;
}
/**
* Takes a media entity and returns a keyed array of entities: file, image, and media.
*
* @param $media_entity
*
* @return array containing a file, image, and media entity or null
*/
public function getMediaEntity($media_entity)
{
if ($img_entity_list = $media_entity->get('field_media_image')) {
if ($img_entity = $img_entity_list->first()) {
if ($file_entity = $img_entity->get('entity')->getTarget()) {
return ['file' => $file_entity, 'img' => $img_entity, 'media' => $media_entity];
}
}
}
return null;
}
/**
* Returns a keyed array of strings representing image src, alt, and title.
*
* @param $file_entity
* @param $img_entity
*
* @return array keyed by image property
*/
public function getImageProperties($file_entity, $img_entity)
{
return [
'src' => $file_entity->get('uri')->getString(),
'alt' => $img_entity->get('alt')->getString(),
'title' => $img_entity->get('title')->getString(),
];
}
public function getCacheTags()
{
return Cache::mergeTags(parent::getCacheTags(), ['node_list']);
}
}
And this is the twig file:
<div class="row" role="navigation">
{% for link in data.menu.children %}
<div class="col-lg-4 d-flex align-items-stretch">
<div class="card tiles">
<div class="card-body">
{% if (link.title is not empty) and (link.title == 'Reports and publications') %}
<div class="card-icon" aria-hidden="true">
<i class="fal fa-file-chart-pie"></i>
</div>
{% elseif (link.title is not empty) and (link.title == 'News')%}
<div class="card-icon" aria-hidden="true">
<i class="fal fa-newspaper"></i>
</div>
{% else %}
{% if (link.fa_icon)
is not empty %}
<div class="card-icon" aria-hidden="true">
{{ link.fa_icon | raw }}
</div>
{% else %}
{% if (link.icon)
is not empty %}
<div class="card-icon">
<img src="{{ file_url(link.icon.src) }}">
</div>
{% endif %}
{% endif %}
{% endif %}
<h3 class="card-title">
{{ link.title }}
</h3>
<p>{{ link.summary | raw }}</p>
<div class="card-cta">
<a href="{{ link.uri }}" class="card-link">View
{{ link.title }}</a>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
Hi, the site is hosted in single language. We don't have multiple languages.