|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\ghi_content\EntityBrowser; |
| 4 | + |
| 5 | +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
| 6 | +use Drupal\Core\Entity\Element\EntityAutocomplete; |
| 7 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | +use Drupal\ghi_content\Entity\Document; |
| 9 | +use Drupal\ghi_sections\Entity\SectionNodeInterface; |
| 10 | +use Drupal\node\NodeInterface; |
| 11 | +use Drupal\views\ViewExecutable; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 14 | + |
| 15 | +/** |
| 16 | + * Service class for article select entity browsers. |
| 17 | + * |
| 18 | + * This class contains some logic to imrove the UI and UX of the entity browser |
| 19 | + * used to select articles. |
| 20 | + */ |
| 21 | +class ArticleSelection implements ContainerInjectionInterface { |
| 22 | + |
| 23 | + /** |
| 24 | + * The view id that this service class handles. |
| 25 | + */ |
| 26 | + const VIEW_ID = 'article_selection'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Identifier for the tag filter on the view. |
| 30 | + */ |
| 31 | + const TAG_FILTER = 'tags'; |
| 32 | + |
| 33 | + /** |
| 34 | + * The entity type manager service. |
| 35 | + * |
| 36 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 37 | + */ |
| 38 | + protected $entityTypeManager; |
| 39 | + |
| 40 | + /** |
| 41 | + * The current request. |
| 42 | + * |
| 43 | + * @var \Symfony\Component\HttpFoundation\Request |
| 44 | + */ |
| 45 | + protected $request; |
| 46 | + |
| 47 | + /** |
| 48 | + * Public constructor. |
| 49 | + */ |
| 50 | + public function __construct(EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack) { |
| 51 | + $this->entityTypeManager = $entity_type_manager; |
| 52 | + $this->request = $request_stack->getCurrentRequest(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public static function create(ContainerInterface $container) { |
| 59 | + return new static( |
| 60 | + $container->get('entity_type.manager'), |
| 61 | + $container->get('request_stack'), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Act on the pre_view hook of an entity browser view. |
| 67 | + * |
| 68 | + * Pre-populate the tag filter on article selection entity browsers when |
| 69 | + * opening the selection dialog for the first time. |
| 70 | + * |
| 71 | + * @param \Drupal\views\ViewExecutable $view |
| 72 | + * The view to modifiy. |
| 73 | + * @param array $args |
| 74 | + * An array of arguments. |
| 75 | + */ |
| 76 | + public function preView(ViewExecutable $view, array $args) { |
| 77 | + $exposed_input = $view->getExposedInput(); |
| 78 | + $already_submitted = array_key_exists(self::TAG_FILTER, $exposed_input); |
| 79 | + if ($view->id() != self::VIEW_ID || $already_submitted) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Now see if we have a qualified node and if it has tags. |
| 84 | + $node = $this->getCurrentNode(); |
| 85 | + $tags = $node ? $this->getTagsFromNode($node) : NULL; |
| 86 | + if (empty($tags)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Populate the exposed tag filter with these found tags. |
| 91 | + $exposed_input = $view->getExposedInput(); |
| 92 | + $exposed_input[self::TAG_FILTER] = EntityAutocomplete::getEntityLabels($tags); |
| 93 | + $view->setExposedInput($exposed_input); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the current node from the request. |
| 98 | + * |
| 99 | + * @return \Drupal\node\NodeInterface|null |
| 100 | + * A node object if it has been found. |
| 101 | + */ |
| 102 | + private function getCurrentNode() { |
| 103 | + // Get the path to get the node entity. |
| 104 | + $original_path = $this->request->query->get('original_path'); |
| 105 | + if (!$original_path) { |
| 106 | + return NULL; |
| 107 | + } |
| 108 | + $node_key = array_values(array_filter(explode('/', $original_path), function ($part) { |
| 109 | + return str_starts_with($part, 'node.') ? $part : NULL; |
| 110 | + }))[0] ?? NULL; |
| 111 | + if (!$node_key) { |
| 112 | + return NULL; |
| 113 | + } |
| 114 | + [$entity_type_id, $entity_id] = explode('.', $node_key); |
| 115 | + return $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the tags from the given node. |
| 120 | + * |
| 121 | + * This only works with a limited set of known node types. |
| 122 | + * |
| 123 | + * @param \Drupal\node\NodeInterface $node |
| 124 | + * A node object. |
| 125 | + * |
| 126 | + * @return \Drupal\taxonomy\TermInterface[] |
| 127 | + * An array of tag entities. |
| 128 | + */ |
| 129 | + private function getTagsFromNode(NodeInterface $node) { |
| 130 | + if ($node instanceof SectionNodeInterface) { |
| 131 | + return $node->getTagEntities(); |
| 132 | + } |
| 133 | + if ($node instanceof Document) { |
| 134 | + return $node->getTags(TRUE); |
| 135 | + } |
| 136 | + return NULL; |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments