Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGI9-314: Allow date fields from paragraphs #113

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -192,16 +202,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);
}
}
}

}