From 97edb6bf160747aca416190791a8f27cf6ae7b96 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Fri, 30 Apr 2021 05:12:46 +0000 Subject: [PATCH 1/2] Add intial EDTFYear processor --- src/Plugin/search_api/processor/EDTFYear.php | 110 +++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/Plugin/search_api/processor/EDTFYear.php diff --git a/src/Plugin/search_api/processor/EDTFYear.php b/src/Plugin/search_api/processor/EDTFYear.php new file mode 100644 index 0000000..3255ce4 --- /dev/null +++ b/src/Plugin/search_api/processor/EDTFYear.php @@ -0,0 +1,110 @@ + $this->t('EDTF Creation Date Year'), + 'description' => $this->t('The year the item was created'), + 'type' => 'integer', + 'is_list' => TRUE, + 'processor_id' => $this->getPluginId(), + ]; + $properties['edtf_year'] = new ProcessorProperty($definition); + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function addFieldValues(ItemInterface $item) { + + $node = $item->getOriginalObject()->getValue(); + + if ($node + && $node->hasField('field_edtf_date') + && !$node->field_edtf_date->isEmpty()) { + $edtf = $node->field_edtf_date->value; + if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) { + $years = []; + // Sets. + if (strpos($edtf, '[') !== FALSE || strpos($edtf, '{') !== FALSE) { + $years = preg_split('/(,|\.\.)/', trim($edtf, '{}[]')); + } + // Intervals. + elseif (strpos($edtf, '/') !== FALSE) { + $date_range = explode('/', $edtf); + if ($date_range[0] == '..') { + // The list of years needs to begin *somewhere*. + // This is hopefully a sensible default. + $begin = 0; + } + else { + $begin = $this->edtfToYearInt($date_range[0]); + } + if ($date_range[1] == '..') { + // Similarly, we need to end somewhere. Why not this year? + $end = intval(date("Y")); + } + else { + $end = $this->edtfToYearInt($date_range[1]); + } + $years = range($begin, $end); + } + else { + $years[] = $this->edtfToYearInt($edtf); + } + 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); + } + } + } + } + } + } + + /** + * Convert a given EDTF date string into a year integer. + */ + private function edtfToYearInt(string $edtf) { + $iso = EDTFUtils::iso8601Value($edtf); + $components = explode('-', $iso); + return intval(array_shift($components)); + } + +} From fae256dff1ad24621346ba3c8e093310d01f3aba Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Fri, 30 Apr 2021 17:09:11 +0000 Subject: [PATCH 2/2] make the edtf year processor configurable --- src/Plugin/search_api/processor/EDTFYear.php | 149 ++++++++++++++----- 1 file changed, 108 insertions(+), 41 deletions(-) diff --git a/src/Plugin/search_api/processor/EDTFYear.php b/src/Plugin/search_api/processor/EDTFYear.php index 3255ce4..a1cd6dd 100644 --- a/src/Plugin/search_api/processor/EDTFYear.php +++ b/src/Plugin/search_api/processor/EDTFYear.php @@ -2,8 +2,11 @@ namespace Drupal\controlled_access_terms\Plugin\search_api\processor; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\PluginFormInterface; use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Item\ItemInterface; +use Drupal\search_api\Plugin\PluginFormTrait; use Drupal\search_api\Processor\ProcessorPluginBase; use Drupal\search_api\Processor\ProcessorProperty; use Drupal\controlled_access_terms\EDTFUtils; @@ -18,11 +21,11 @@ * stages = { * "add_properties" = 0, * }, - * locked = true, - * hidden = true, * ) */ -class EDTFYear extends ProcessorPluginBase { +class EDTFYear extends ProcessorPluginBase implements PluginFormInterface { + + use PluginFormTrait; /** * {@inheritdoc} @@ -44,53 +47,117 @@ public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { return $properties; } + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'fields' => [], + 'open_start_year' => 0, + 'open_end_year' => '', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $formState) { + $form['#description'] = $this->t('Select the fields containing EDTF strings to extract year values for.'); + $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_type' => 'edtf']); + $fields_options = []; + foreach ($fields as $field) { + $fields_options[$field->getTargetBundle() . '|' . $field->get('field_name')] = $this->t("%label (%bundle)", [ + '%label' => $field->label(), + '%bundle' => $field->getTargetBundle(), + ]); + } + $form['fields'] = [ + '#type' => 'select', + '#multiple' => TRUE, + '#title' => $this->t('Fields'), + '#description' => $this->t('Select the fields with EDTF values to use.'), + '#options' => $fields_options, + '#default_value' => $this->configuration['fields'], + ]; + + $form['open_start_year'] = [ + '#type' => 'number', + '#title' => $this->t('Open Interval Begin Year'), + '#description' => $this->t('Sets the beginning year to be used when processing an date interval. For example, by default, "../%year" would become every year from 0 until %year', ['%year' => date("Y")]), + '#default_value' => $this->configuration['open_start_year'], + ]; + $form['open_end_year'] = [ + '#type' => 'number', + '#title' => $this->t('Open Interval End Year'), + '#description' => $this->t('Sets the last year to be used when processing an date interval. Leave blank to use the current year when last indexed. For example, by default, "2020/.." would become every year from 2020 until this year (%year).', ['%year' => date("Y")]), + '#default_value' => $this->configuration['open_end_year'], + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $formState) { + if (!is_numeric($formState->getValue('open_start_year'))) { + $formState->setError($form['open_start_year'], $this->t('Please provide an integer year value.')); + } + if (!empty($formState->getValue('open_end_year')) && !is_numeric($formState->getValue('open_end_year'))) { + $formState->setError($form['open_end_year'], $this->t('Please leave the field blank or provide an integer year value.')); + } + } + /** * {@inheritdoc} */ public function addFieldValues(ItemInterface $item) { - $node = $item->getOriginalObject()->getValue(); - - if ($node - && $node->hasField('field_edtf_date') - && !$node->field_edtf_date->isEmpty()) { - $edtf = $node->field_edtf_date->value; - if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) { - $years = []; - // Sets. - if (strpos($edtf, '[') !== FALSE || strpos($edtf, '{') !== FALSE) { - $years = preg_split('/(,|\.\.)/', trim($edtf, '{}[]')); - } - // Intervals. - elseif (strpos($edtf, '/') !== FALSE) { - $date_range = explode('/', $edtf); - if ($date_range[0] == '..') { - // The list of years needs to begin *somewhere*. - // This is hopefully a sensible default. - $begin = 0; - } - else { - $begin = $this->edtfToYearInt($date_range[0]); + $entity = $item->getOriginalObject()->getValue(); + + foreach ($this->configuration['fields'] as $field) { + list($bundle, $field_name) = explode('|', $field, 2); + if ($entity + && $entity->bundle() == $bundle + && $entity->hasField($field_name) + && !$entity->get($field_name)->isEmpty()) { + $edtf = $entity->get($field_name)->value; + if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) { + $years = []; + // Sets. + if (strpos($edtf, '[') !== FALSE || strpos($edtf, '{') !== FALSE) { + $years = preg_split('/(,|\.\.)/', trim($edtf, '{}[]')); } - if ($date_range[1] == '..') { - // Similarly, we need to end somewhere. Why not this year? - $end = intval(date("Y")); + // Intervals. + elseif (strpos($edtf, '/') !== FALSE) { + $date_range = explode('/', $edtf); + if ($date_range[0] == '..') { + // The list of years needs to begin *somewhere*. + $begin = $this->configuration['open_start_year']; + } + else { + $begin = $this->edtfToYearInt($date_range[0]); + } + if ($date_range[1] == '..') { + // Similarly, we need to end somewhere. + // Use this year if none was configured. + $end = (empty($this->configuration['open_end_year'])) ? intval(date("Y")) : $this->configuration['open_end_year']; + } + else { + $end = $this->edtfToYearInt($date_range[1]); + } + $years = range($begin, $end); } else { - $end = $this->edtfToYearInt($date_range[1]); + $years[] = $this->edtfToYearInt($edtf); } - $years = range($begin, $end); - } - else { - $years[] = $this->edtfToYearInt($edtf); - } - 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); + 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); + } } } }