From f4541667957ff241ec5a4b21d9b4c9c970ae4f59 Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Mon, 15 Apr 2024 15:01:23 -0400 Subject: [PATCH] Simplify undefined $edtf handling --- src/Plugin/search_api/processor/EDTFYear.php | 141 +++++++++---------- 1 file changed, 68 insertions(+), 73 deletions(-) diff --git a/src/Plugin/search_api/processor/EDTFYear.php b/src/Plugin/search_api/processor/EDTFYear.php index 44c75a7..55d0efe 100644 --- a/src/Plugin/search_api/processor/EDTFYear.php +++ b/src/Plugin/search_api/processor/EDTFYear.php @@ -139,14 +139,8 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form public function addFieldValues(ItemInterface $item) { $entity = $item->getOriginalObject()->getValue(); foreach ($this->configuration['fields'] as $field) { - $components = explode('|', $field, 3); - if (count($components) != 3) { - continue; - } + [$entityType, $bundle, $field_name] = explode('|', $field, 3); - [$entityType, $bundle, $field_name] = $components; - - $edtf = FALSE; if ($entityType === 'paragraph') { $edtf = $this->getDateFromParagraphField($entity, $bundle, $field_name); } @@ -156,85 +150,86 @@ public function addFieldValues(ItemInterface $item) { && !$entity->get($field_name)->isEmpty()) { $edtf = trim($entity->get($field_name)->value); } + else { + continue; + } - if ($edtf) { - if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) { - if ($this->configuration['ignore_undated'] && $edtf == "XXXX") { - continue; + if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) { + if ($this->configuration['ignore_undated'] && $edtf == "XXXX") { + continue; + } + try { + $parser = EdtfFactory::newParser(); + $years = []; + // Sets. + if (strpos($edtf, '[') !== FALSE || strpos($edtf, '{') !== FALSE) { + $dates = $parser->parse($edtf)->getEdtfValue(); + $years = array_map(function ($date) { + return $date->getYear(); + }, $dates->getDates()); } - try { - $parser = EdtfFactory::newParser(); - $years = []; - // Sets. - if (strpos($edtf, '[') !== FALSE || strpos($edtf, '{') !== FALSE) { - $dates = $parser->parse($edtf)->getEdtfValue(); - $years = array_map(function ($date) { - return $date->getYear(); - }, $dates->getDates()); + else { + // Open start dates with `../`. + if (substr($edtf, 0, 3) === '../') { + if ($this->configuration['ignore_open_start']) { + $edtf = substr($edtf, 3); + } + else { + $edtf = str_replace('../', $this->configuration['open_start_year'] . '/', $edtf); + } } - else { - // Open start dates with `../`. - if (substr($edtf, 0, 3) === '../') { - if ($this->configuration['ignore_open_start']) { - $edtf = substr($edtf, 3); - } - else { - $edtf = str_replace('../', $this->configuration['open_start_year'] . '/', $edtf); - } + // Open start dates with `/`. + if (substr($edtf, 0, 1) === '/') { + if ($this->configuration['ignore_open_start']) { + $edtf = substr($edtf, 1); } - // Open start dates with `/`. - if (substr($edtf, 0, 1) === '/') { - if ($this->configuration['ignore_open_start']) { - $edtf = substr($edtf, 1); - } - else { - $edtf = str_replace('/', $this->configuration['open_start_year'] . '/', $edtf); - } + else { + $edtf = str_replace('/', $this->configuration['open_start_year'] . '/', $edtf); } - // Open end dates with `/..`. - if (substr($edtf, -3) === '/..') { - if ($this->configuration['ignore_open_end']) { - $edtf = substr($edtf, 0, -3); - } - else { - $end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year']; - $edtf = str_replace('/..', '/' . $end_year, $edtf); - } + } + // Open end dates with `/..`. + if (substr($edtf, -3) === '/..') { + if ($this->configuration['ignore_open_end']) { + $edtf = substr($edtf, 0, -3); } - // Open end dates with `/`. - if (substr($edtf, -1) === '/') { - if ($this->configuration['ignore_open_end']) { - $edtf = substr($edtf, 0, -1); - } - else { - $end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year']; - $edtf = str_replace('/', '/' . $end_year, $edtf); - } + else { + $end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year']; + $edtf = str_replace('/..', '/' . $end_year, $edtf); } - $parsed = $parser->parse($edtf)->getEdtfValue(); - $years = range(intval(date('Y', $parsed->getMin())), intval(date('Y', $parsed->getMax()))); } - foreach ($years as $year) { - if (is_numeric($year)) { - $fields = $item->getFields(FALSE); - $fields = $this->getFieldsHelper() - ->filterForPropertyPath($fields, NULL, 'edtf_year'); - foreach ($fields as $field) { - $field->addValue($year); - } + // Open end dates with `/`. + if (substr($edtf, -1) === '/') { + if ($this->configuration['ignore_open_end']) { + $edtf = substr($edtf, 0, -1); + } + else { + $end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year']; + $edtf = str_replace('/', '/' . $end_year, $edtf); } } + $parsed = $parser->parse($edtf)->getEdtfValue(); + $years = range(intval(date('Y', $parsed->getMin())), intval(date('Y', $parsed->getMax()))); } - catch (\Throwable $e) { - \Drupal::logger('controlled_access_terms') - ->warning(t("Could not parse EDTF value '@edtf' for indexing @type/@id", - [ - '@edtf' => $edtf, - '@type' => $entity->getEntityTypeId(), - '@id' => $entity->id(), - ])); + foreach ($years as $year) { + if (is_numeric($year)) { + $fields = $item->getFields(FALSE); + $fields = $this->getFieldsHelper() + ->filterForPropertyPath($fields, NULL, 'edtf_year'); + foreach ($fields as $field) { + $field->addValue($year); + } + } } } + catch (\Throwable $e) { + \Drupal::logger('controlled_access_terms') + ->warning(t("Could not parse EDTF value '@edtf' for indexing @type/@id", + [ + '@edtf' => $edtf, + '@type' => $entity->getEntityTypeId(), + '@id' => $entity->id(), + ])); + } } } }