Skip to content

Commit

Permalink
Merge pull request #113 from bibliophileaxe/DGI9-314
Browse files Browse the repository at this point in the history
DGI9-314: Allow date fields from paragraphs
  • Loading branch information
rosiel authored Nov 29, 2023
2 parents e799093 + d921940 commit b7d6c2d
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions src/Plugin/search_api/processor/EDTFYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Drupal\controlled_access_terms\Plugin\search_api\processor;

use EDTF\EdtfFactory;
use Drupal\controlled_access_terms\EDTFUtils;
use Drupal\Core\Entity\EntityInterface;
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;
use EDTF\EdtfFactory;

/**
* Adds the item's creation year to the indexed data.
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 @@ -210,16 +220,41 @@ 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(),
]));
\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 b7d6c2d

Please sign in to comment.