Skip to content

Commit

Permalink
Merge pull request #799 from UN-OCHA/berliner/HPC-9268
Browse files Browse the repository at this point in the history
HPC-9268: Add support for GHO mega menu
  • Loading branch information
berliner authored Nov 15, 2023
2 parents db75f17 + f8b592e commit aec80b7
Show file tree
Hide file tree
Showing 10 changed files with 565 additions and 102 deletions.
21 changes: 2 additions & 19 deletions config/core.entity_view_display.menu_link_content.main.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,9 @@ third_party_settings:
entity: layout_builder.entity
weight: 0
additional: { }
97524d06-5162-4952-bfcc-0a286ab394a7:
uuid: 97524d06-5162-4952-bfcc-0a286ab394a7
region: content
configuration:
id: 'field_block:menu_link_content:main:field_right_border'
label_display: '0'
context_mapping:
entity: layout_builder.entity
formatter:
type: boolean
label: above
settings:
format: default
format_custom_false: ''
format_custom_true: ''
third_party_settings: { }
weight: 1
additional: { }
third_party_settings: { }
layout_builder_ipe:
enabled: 0
enabled: false
layout_builder_restrictions:
allowed_block_categories: { }
entity_view_mode_restriction:
Expand Down Expand Up @@ -82,3 +64,4 @@ content:
hidden:
langcode: true
layout_builder__layout: true
search_api_excerpt: true
8 changes: 8 additions & 0 deletions config/system.menu.gho-menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
uuid: 4a951995-73ad-4d60-baf9-a1ed0a240a8b
langcode: en
status: true
dependencies: { }
id: gho-menu
label: 'GHO Menu'
description: ''
locked: false
4 changes: 3 additions & 1 deletion html/modules/custom/ghi_menu/ghi_menu.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ type: module
core_version_requirement: ^9 || ^10

dependencies:
- ghi_sections:ghi_sections
- ghi_sections:ghi_sections
- ghi_content:ghi_content
- ghi_subpages:ghi_subpages
113 changes: 113 additions & 0 deletions html/modules/custom/ghi_menu/src/GhiEntityAutocompleteMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Drupal\ghi_menu;

use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Entity\EntityAutocompleteMatcher;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\ghi_content\Entity\Article;
use Drupal\ghi_subpages\Entity\SubpageNodeInterface;

/**
* Matcher class to get autocompletion results for entity reference.
*/
class GhiEntityAutocompleteMatcher extends EntityAutocompleteMatcher {

/**
* {@inheritdoc}
*/
public function getMatches($target_id, $selection_handler, $selection_settings, $string = '') {
if ($target_id !== 'node') {
return parent::getMatches($target_id, $selection_handler, $selection_settings, $string);
}
$matches = [];
if (!isset($string)) {
return $matches;
}
// Get the matches with different limits based on type of referenced entity.
/** @var \Drupal\node\Plugin\EntityReferenceSelection\NodeSelection $handler */
$handler = $this->selectionManager->getInstance([
'target_type' => $target_id,
'handler' => $selection_handler,
'handler_settings' => $selection_settings,
]);
$match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS';
$entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10);

// Customize the labels used in autocomplete.
foreach ($entity_labels as $bundle => $values) {
$node_type = \Drupal::entityTypeManager()->getStorage('node_type')->load($bundle);
$entities = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple(array_keys($values));
foreach ($values as $entity_id => $label) {
$entity = $entities[$entity_id];
if ($entity instanceof Article) {
$custom_label = $this->getArticleLabel($entity_id, $target_id, $label);
}
elseif ($entity instanceof SubpageNodeInterface) {
$custom_label = $this->getSubpageLabel($entity_id, $target_id, $label);
}
else {
$custom_label = $label;
}

if ($entity instanceof EntityPublishedInterface && !$entity->isPublished()) {
$custom_label = $custom_label . ' - Unpublished';
}

// Create a sanitized key.
$key = "$label ($entity_id)";
$key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key)))));
$key = Tags::encode($key);
$matches[] = ['value' => $key, 'label' => $node_type->label() . ': ' . $custom_label];
}
}
return $matches;
}

/**
* Get the label for an article.
*
* @param int $entity_id
* The entity id.
* @param string $entity_type_id
* The entity type.
* @param string $label
* The label so far.
*
* @return string
* The resulting label.
*/
protected function getArticleLabel($entity_id, $entity_type_id, $label) {
/** @var \Drupal\ghi_content\Entity\Article $entity */
$entity = \Drupal::entityTypeManager()->getStorage($entity_type_id)->load($entity_id);
$tags = $entity->getTags(TRUE);
$tag_names = array_map(function ($term) {
return $term->label();
}, $tags);
$label = $label . ' (' . implode(', ', $tag_names) . ')';
return $label;
}

/**
* Get the label for a subpage.
*
* @param int $entity_id
* The entity id.
* @param string $entity_type_id
* The entity type.
* @param string $label
* The label so far.
*
* @return string
* The resulting label.
*/
protected function getSubpageLabel($entity_id, $entity_type_id, $label) {
/** @var \Drupal\ghi_subpages\Entity\SubpageNodeInterface $entity */
$entity = \Drupal::entityTypeManager()->getStorage($entity_type_id)->load($entity_id);
$parent = $entity->getParentNode();
$label = $label . ' (' . $parent->label() . ')';
return $label;
}

}
22 changes: 22 additions & 0 deletions html/modules/custom/ghi_menu/src/GhiMenuServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Drupal\ghi_menu;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;

/**
* Service provider for GHI Menus.
*/
class GhiMenuServiceProvider extends ServiceProviderBase implements ServiceProviderInterface {

/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
$definition = $container->getDefinition('entity.autocomplete_matcher');
$definition->setClass('Drupal\ghi_menu\GhiEntityAutocompleteMatcher');
}

}
Loading

0 comments on commit aec80b7

Please sign in to comment.