forked from bmcclure/drupal-search_filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchFiltersMapping.php
151 lines (111 loc) · 3.96 KB
/
SearchFiltersMapping.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
<?php
namespace Drupal\search_filters;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\VocabularyInterface;
class SearchFiltersMapping {
protected $mapping = [];
protected $config;
public function __construct($mapping = NULL) {
$this->config = \Drupal::config('search_filters.settings');
if (is_null($mapping)) {
$mapping = $this->config->get('bundle_mapping');
}
$this->setMapping($mapping);
}
public function setMapping($mapping = NULL) {
$this->mapping = [];
if (is_array($mapping)) {
$this->mapping = $mapping;
} elseif (is_string($mapping)) {
$bundle_mapping = preg_split('/(\r\n|\r|\n)/', $mapping);
foreach ($bundle_mapping as $line) {
list($entity_type, $bundle, $values) = explode(':', $line, 3);
$values = explode('|', $values);
foreach ($values as $value) {
$this->mapping[$entity_type][$bundle][] = $value;
}
}
}
}
public function shouldSetFilters(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return FALSE;
}
$config = \Drupal::config('search_filters.settings');
if (empty($config->get('vocabulary')) || empty($config->get('field_name'))) {
return FALSE;
}
$vocabulary = Vocabulary::load($config->get('vocabulary'));
if (!$vocabulary instanceof VocabularyInterface) {
return FALSE;
}
$field_name = $config->get('field_name');
if (!$entity->hasField($field_name)) {
return FALSE;
}
if (!array_key_exists($entity->getEntityTypeId(), $this->mapping)) {
return FALSE;
}
return (array_key_exists($entity->bundle(), $this->mapping[$entity->getEntityTypeId()])
|| array_key_exists('*', $this->mapping[$entity->getEntityTypeId()]));
}
public function setFiltersOnEntity(EntityInterface $entity) {
if (!$this->shouldSetFilters($entity)) {
return;
}
/** @var FieldableEntityInterface $entity */
$this->clearFilterValues($entity);
foreach ([$entity->bundle(), '*'] as $bundle_key) {
if (array_key_exists($bundle_key, $this->mapping[$entity->getEntityTypeId()])) {
foreach ($this->mapping[$entity->getEntityTypeId()][$bundle_key] as $term_name) {
$this->setFilterOnEntity($term_name, $entity);
}
}
}
}
protected function clearFilterValues(FieldableEntityInterface $entity) {
$config = \Drupal::config('search_filters.settings');
$field_name = $config->get('field_name');
if ($entity->hasField($field_name)) {
$entity->get($field_name)->setValue([]);
}
}
protected function setFilterOnEntity($term_name, FieldableEntityInterface $entity) {
if (strpos($term_name, '[') === 0) {
$name_field = str_replace(['[', ']'], '', $term_name);
if ($entity->hasField($name_field) && !$entity->get($name_field)->isEmpty()) {
$field = $entity->get($name_field);
$term_name = $field->value;
if ($field->getSetting('allowed_values')) {
$term_name = $field->getSetting('allowed_values')[$term_name];
}
}
}
$config = \Drupal::config('search_filters.settings');
$properties = [
'vid' => $config->get('vocabulary'),
'name' => $term_name,
];
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties($properties);
$term = reset($terms);
if (empty($term)) {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->create($properties);
$term->save();
}
$field_name = $config->get('field_name');
$values = $entity->get($field_name)->getValue();
$exists = FALSE;
foreach ($values as $item) {
if ($item['target_id'] == $term->id()) {
$exists = TRUE;
break;
}
}
if (!$exists) {
$values[] = ['target_id' => $term->id()];
}
$entity->get($field_name)->setValue($values);
}
}