|
5 | 5 | use Drupal\Core\Block\BlockBase;
|
6 | 6 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
7 | 7 | use Drupal\ghi_base_objects\Traits\ShortNameTrait;
|
8 |
| -use Drupal\ghi_plans\Entity\Plan; |
9 | 8 | use Drupal\ghi_sections\Entity\SectionNodeInterface;
|
10 | 9 | use Drupal\ghi_sections\Traits\SectionPathTrait;
|
11 | 10 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
@@ -113,46 +112,15 @@ public function build() {
|
113 | 112 | /**
|
114 | 113 | * Build the section switcher options.
|
115 | 114 | *
|
116 |
| - * @return \Drupal\ghi_sections\Entity\SectionNodeInterface[] |
117 |
| - * An array of section nodes to be used as options. |
| 115 | + * @return \Drupal\ghi_sections\Entity\SectionNodeInterface[]|null |
| 116 | + * An array of section nodes to be used as options or NULL. |
118 | 117 | */
|
119 | 118 | private function buildSectionSwitcherOptions() {
|
120 |
| - |
121 | 119 | $section_node = $this->getSectionNode();
|
122 | 120 | if (!$section_node) {
|
123 | 121 | return NULL;
|
124 | 122 | }
|
125 |
| - |
126 |
| - $base_object = $section_node->getBaseObject(); |
127 |
| - $sections = []; |
128 |
| - if (!$section_node->get('field_year')->isEmpty()) { |
129 |
| - // This is either a global section page or a section page with a base |
130 |
| - // object that needs an additional year specified. |
131 |
| - $args = array_filter([ |
132 |
| - 'type' => $section_node->bundle(), |
133 |
| - 'field_base_object' => $base_object?->id(), |
134 |
| - ]); |
135 |
| - $candidates = $this->entityTypeManager->getStorage($section_node->getEntityTypeId())->loadByProperties($args); |
136 |
| - foreach ($candidates as $candidate) { |
137 |
| - $year = $candidate->get('field_year')->value; |
138 |
| - $sections[$year] = $candidate; |
139 |
| - } |
140 |
| - } |
141 |
| - elseif ($base_object && $base_object->hasField('field_focus_country')) { |
142 |
| - // This is a section page with no year but with a focus country field, |
143 |
| - // e.g. a plan based section page. |
144 |
| - $sections = $this->getSectionsByBaseObjectFocusCountry(); |
145 |
| - } |
146 |
| - elseif ($base_object) { |
147 |
| - // This is a section page with no year, e.g. a plan based section page. |
148 |
| - $sections = $this->getSectionsByBaseObjectCountryReference(); |
149 |
| - } |
150 |
| - |
151 |
| - if (empty($sections)) { |
152 |
| - return NULL; |
153 |
| - } |
154 |
| - |
155 |
| - return $sections; |
| 123 | + return $this->sectionManager->getRelatedSections($section_node); |
156 | 124 | }
|
157 | 125 |
|
158 | 126 | /**
|
@@ -180,122 +148,4 @@ private function getSectionNode() {
|
180 | 148 | return $section_node instanceof SectionNodeInterface ? $section_node : NULL;
|
181 | 149 | }
|
182 | 150 |
|
183 |
| - /** |
184 |
| - * Get switcher options by a country reference on the sections base objects. |
185 |
| - * |
186 |
| - * @return \Drupal\ghi_sections\Entity\SectionNodeInterface[] |
187 |
| - * An array of section nodes keyed by the base object original id |
188 |
| - */ |
189 |
| - private function getSectionsByBaseObjectFocusCountry() { |
190 |
| - $options = []; |
191 |
| - $section_node = $this->getSectionNode(); |
192 |
| - $base_object = $section_node->getBaseObject(); |
193 |
| - if (!$base_object || !$base_object->hasField('field_focus_country') || $base_object->get('field_focus_country')->isEmpty()) { |
194 |
| - return $options; |
195 |
| - } |
196 |
| - $focus_country = $base_object->get('field_focus_country')->entity; |
197 |
| - |
198 |
| - // Find other object candidates that have the same focus country. |
199 |
| - /** @var \Drupal\ghi_base_objects\Entity\BaseObjectInterface[] $base_object_candidates */ |
200 |
| - $base_object_candidates = $this->entityTypeManager->getStorage($base_object->getEntityTypeId())->loadByProperties([ |
201 |
| - 'type' => $base_object->bundle(), |
202 |
| - 'field_focus_country' => $focus_country->id(), |
203 |
| - ]); |
204 |
| - |
205 |
| - // If base object is a plan, thus looking for other plan base objects, |
206 |
| - // apply filtering based on the plan type. |
207 |
| - if ($base_object instanceof Plan) { |
208 |
| - $base_object_candidates = array_filter($base_object_candidates, function (Plan $base_object_candidate) use ($base_object) { |
209 |
| - // If the current base object is of type RRP, we want to retain only |
210 |
| - // candidates that are also RRPs. If it's not an RRP, we only want |
211 |
| - // other candiates that are not RRPs either. |
212 |
| - return $base_object->isRrp() ? $base_object_candidate->isRrp() : !$base_object_candidate->isRrp(); |
213 |
| - }); |
214 |
| - } |
215 |
| - |
216 |
| - return $this->getSectionOptionsForBaseObjects($section_node, $base_object_candidates); |
217 |
| - } |
218 |
| - |
219 |
| - /** |
220 |
| - * Get switcher options by a country reference on the sections base objects. |
221 |
| - * |
222 |
| - * @return \Drupal\ghi_sections\Entity\SectionNodeInterface[] |
223 |
| - * An array of section nodes keyed by the base object original id |
224 |
| - */ |
225 |
| - private function getSectionsByBaseObjectCountryReference() { |
226 |
| - $options = []; |
227 |
| - $section_node = $this->getSectionNode(); |
228 |
| - $base_object = $section_node->getBaseObject(); |
229 |
| - if (!$base_object || !$base_object->hasField('field_country') || $base_object->get('field_country')->isEmpty()) { |
230 |
| - return $options; |
231 |
| - } |
232 |
| - |
233 |
| - // Get the list of all countries associated with this object. |
234 |
| - $country_ids = array_map(function ($country) { |
235 |
| - return $country->id(); |
236 |
| - }, $base_object->get('field_country')->referencedEntities()); |
237 |
| - |
238 |
| - // Find other object candidates that have at least one of these countries |
239 |
| - // associated. |
240 |
| - /** @var \Drupal\ghi_base_objects\Entity\BaseObjectInterface[] $base_object_candidates */ |
241 |
| - $base_object_candidates = $this->entityTypeManager->getStorage($base_object->getEntityTypeId())->loadByProperties([ |
242 |
| - 'type' => $base_object->bundle(), |
243 |
| - 'field_country' => $country_ids, |
244 |
| - ]); |
245 |
| - |
246 |
| - // Then filter out the ones that don't share the full set of countries. |
247 |
| - $base_object_candidates = array_filter($base_object_candidates, function ($base_object_candidate) use ($country_ids) { |
248 |
| - $candidate_country_ids = array_map(function ($country) { |
249 |
| - return $country->id(); |
250 |
| - }, $base_object_candidate->get('field_country')->referencedEntities()); |
251 |
| - return empty(array_diff($country_ids, $candidate_country_ids)) && count($candidate_country_ids) == count($country_ids); |
252 |
| - }); |
253 |
| - if (empty($base_object_candidates)) { |
254 |
| - return $options; |
255 |
| - } |
256 |
| - return $this->getSectionOptionsForBaseObjects($section_node, $base_object_candidates); |
257 |
| - } |
258 |
| - |
259 |
| - /** |
260 |
| - * Get the section options for the given base object. |
261 |
| - * |
262 |
| - * @param \Drupal\ghi_sections\Entity\SectionNodeInterface $section_node |
263 |
| - * The current section node. |
264 |
| - * @param \Drupal\ghi_base_objects\Entity\BaseObjectInterface[] $base_objects |
265 |
| - * The base objects. |
266 |
| - * |
267 |
| - * @return \Drupal\ghi_sections\Entity\SectionNodeInterface[] |
268 |
| - * An array of section nodes keyed by the base object original id. |
269 |
| - */ |
270 |
| - private function getSectionOptionsForBaseObjects(SectionNodeInterface $section_node, array $base_objects) { |
271 |
| - $base_object = $section_node->getBaseObject(); |
272 |
| - // Then load the sections associated to these objects. |
273 |
| - /** @var \Drupal\ghi_sections\Entity\SectionNodeInterface[] $section_candidates */ |
274 |
| - $section_candidates = $this->entityTypeManager->getStorage($section_node->getEntityTypeId())->loadByProperties([ |
275 |
| - 'type' => $section_node->bundle(), |
276 |
| - 'field_base_object' => array_keys($base_objects), |
277 |
| - ]); |
278 |
| - foreach ($section_candidates as $section_candidate) { |
279 |
| - if (!$section_candidate->access('view')) { |
280 |
| - continue; |
281 |
| - } |
282 |
| - $options[$section_candidate->getBaseObject()->getSourceId()] = $section_candidate; |
283 |
| - } |
284 |
| - |
285 |
| - // Sort the options. |
286 |
| - if ($base_object->hasField('field_year')) { |
287 |
| - // If the base object has a year field, use that for sorting. |
288 |
| - usort($options, function ($section_a, $section_b) { |
289 |
| - $year_a = $section_a->getBaseObject()->get('field_year')->value; |
290 |
| - $year_b = $section_b->getBaseObject()->get('field_year')->value; |
291 |
| - return $year_a - $year_b; |
292 |
| - }); |
293 |
| - } |
294 |
| - else { |
295 |
| - // Otherwise just use the base objects original id as a best guess. |
296 |
| - ksort($options); |
297 |
| - } |
298 |
| - return array_reverse($options); |
299 |
| - } |
300 |
| - |
301 | 151 | }
|
0 commit comments