Skip to content

Commit

Permalink
DGI9-314: Allow date fields from paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
bibliophileaxe committed Nov 21, 2023
1 parent 98719a5 commit 7350c05
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions src/Plugin/search_api/processor/EDTFYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\controlled_access_terms\Plugin\search_api\processor;

use Drupal\Core\Entity\EntityInterface;
use EDTF\EdtfFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
Expand Down Expand Up @@ -67,10 +68,13 @@ public function defaultConfiguration() {
*/
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 = \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)", [
$fields_options[$field->getTargetEntityTypeId() . '|' . $field->getTargetBundle() . '|' . $field->get('field_name')] = $this->t("%label (%type:%bundle)", [
'%type' => $field->getTargetEntityTypeId(),
'%label' => $field->label(),
'%bundle' => $field->getTargetBundle(),
]);
Expand Down Expand Up @@ -133,15 +137,21 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {

$entity = $item->getOriginalObject()->getValue();
foreach ($this->configuration['fields'] as $field) {
list($bundle, $field_name) = explode('|', $field, 2);
if ($entity
&& $entity->bundle() == $bundle
[$entityType, $bundle, $field_name] = explode('|', $field, 3);

if ($entityType === 'paragraph') {
$edtf = $this->getDateFromParagraphField($entity, $bundle, $field_name);
}
elseif ($entity->getEntityTypeId() == $entityType
&& $entity->bundle() == $bundle
&& $entity->hasField($field_name)
&& !$entity->get($field_name)->isEmpty()) {
$edtf = trim($entity->get($field_name)->value);
}

if ($edtf) {
if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) {
if ($this->configuration['ignore_undated'] && $edtf == "XXXX") {
continue;
Expand Down Expand Up @@ -190,18 +200,42 @@ public function addFieldValues(ItemInterface $item) {
}
}
}
}
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(),
]));
} 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(),
]));
}
}
}
}
}

/**
* Gets edtf date value from a referenced paragraph.
*/
private function getDateFromParagraphField(EntityInterface $entity, string $bundle, string $field_name) {
$query = \Drupal::entityTypeManager()
->getStorage('field_config')
->getQuery();
$query->condition('bundle', $entity->bundle());
$query->condition("settings.handler_settings.target_bundles.{$bundle}", $bundle);
$paragraph_ids = $query->execute();
if (!empty($paragraph_ids)) {
$paragraph_field_config = reset($paragraph_ids);
$paragraph_field_config_array = explode('.', $paragraph_field_config);
$paragraph_field_name = end($paragraph_field_config_array);
$paragraph_entity = $entity->get($paragraph_field_name)
->referencedEntities()[0] ?? NULL;

if ($paragraph_entity->hasField($field_name)
&& !$paragraph_entity->get($field_name)->isEmpty()) {
return trim($paragraph_entity->get($field_name)->value);
}
}
}

}

0 comments on commit 7350c05

Please sign in to comment.