This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
HideTaxonomyNotInMenu.php
161 lines (137 loc) · 5.25 KB
/
HideTaxonomyNotInMenu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Drupal\acm_sku\Plugin\facets\processor;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Removes the taxonomy terms from facet items which are not included in menu.
*
* @FacetsProcessor(
* id = "hide_taxonomy_not_in_menu",
* label = @Translation("Hide Taxonomy items not in Menu."),
* description = @Translation("Hides the taxonomy terms not included in menu."),
* stages = {
* "build" = 5
* }
* )
*/
class HideTaxonomyNotInMenu extends ProcessorPluginBase implements BuildProcessorInterface, ContainerFactoryPluginInterface {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The connector config object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $connectorConfig;
/**
* Constructs a new object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
$this->connectorConfig = $config_factory->get('acm.connector');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('language_manager'),
$container->get('entity_type.manager'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
// Don't do anything if results set is empty. We can face this case
// when search results are empty.
if (empty($results)) {
return $results;
}
$source = $facet->getFacetSource();
// Support multiple entity types when using Search API.
if (($source instanceof SearchApiDisplay) &&
($facet->getUseHierarchy())) {
$field_id = $facet->getFieldIdentifier();
// Load the index from the source, load the definition from the
// datasource.
/** @var \Drupal\facets\FacetSource\SearchApiFacetSourceInterface $source */
$index = $source->getIndex();
$field = $index->getField($field_id);
// Determine the target entity type.
$entity_type = $field->getDataDefinition()
->getPropertyDefinition('entity')
->getTargetDefinition()
->getEntityTypeId();
// Process taxonomy terms & remove items not included in menu.
if ($entity_type == 'taxonomy_term') {
$langcode = $this->languageManager->getCurrentLanguage()->getId();
$ids = [];
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as $delta => $result) {
$ids[$delta] = $result->getRawValue();
}
// Load all indexed entities of this type.
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadMultiple($ids);
// Loop over all results.
foreach ($results as $i => $result) {
$term = isset($entities[$ids[$i]]) ? $entities[$ids[$i]] : NULL;
if (($term instanceof TermInterface) && $term->hasTranslation($langcode)) {
$term = $term->getTranslation($langcode);
}
// Display the term if included in menu and status is enabled.
if (($term instanceof TermInterface) &&
($term->bundle() == $this->connectorConfig->get('category_vid')) &&
($term->get('field_category_include_menu')->getString()) &&
($term->get('field_commerce_status')->getString())) {
continue;
}
// Remove from results if either term load failed or not included
// in menu or status is disabled.
unset($results[$i]);
}
}
}
// Return the results with the new display values.
return $results;
}
}