Skip to content

Commit

Permalink
OEL-155: Add upper case processor & date parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
drishu committed May 28, 2021
1 parent 54e9106 commit 32adb75
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
66 changes: 61 additions & 5 deletions src/Plugin/search_api/backend/SearchApiEuropaSearchBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Site\Settings;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\search_api\Backend\BackendPluginBase;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\PluginFormTrait;
Expand All @@ -26,8 +27,6 @@
/**
* Europa Search backend for Search API.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
*
* @SearchApiBackend(
* id = "search_api_europa_search",
* label = @Translation("Europa Search"),
Expand Down Expand Up @@ -496,7 +495,7 @@ protected function getDocuments(IndexInterface $index, array $items): array {

/** @var \Drupal\search_api\Item\FieldInterface $field */
foreach ($item_fields as $name => $field) {
$this->addIndexField($metadata, $name, $field->getValues(), $field->getType());
$this->prepareField($metadata, $name, $field->getValues(), $field->getType());
}

$document->setMetadata($metadata);
Expand Down Expand Up @@ -543,7 +542,7 @@ protected function destructReference(string $reference): array {
/**
* Helper method for indexing.
*
* Adds $value with field name $key to the document $doc. The format of $value
* Adds $value with field name $key to the document. The format of $value
* is the same as specified in
* \Drupal\search_api\Backend\BackendSpecificInterface::indexItems().
*
Expand All @@ -556,10 +555,17 @@ protected function destructReference(string $reference): array {
* @param string $type
* The field type.
*/
protected function addIndexField(array &$metadata, string $key, array $values, $type): void {
protected function prepareField(array &$metadata, string $key, array $values, $type): void {
foreach ($values as $value) {
if (NULL !== $value) {
switch ($type) {
case 'date':
$value = $this->formatDate($value);
if ($value === FALSE) {
continue(2);
}
break;

case 'boolean':
$value = (bool) $value;
break;
Expand Down Expand Up @@ -588,4 +594,54 @@ protected function addIndexField(array &$metadata, string $key, array $values, $
}
}

/**
* Tries to format given date for ingestion.
*
* @param int|string $input
* The date to format (timestamp or string).
*
* @return bool|string
* The formatted date as string or FALSE in case of invalid input.
*/
public function formatDate($input) {
try {
$input = is_numeric($input) ? (int) $input : new \DateTime($input, timezone_open(DateTimeItemInterface::STORAGE_TIMEZONE));
}
catch (\Exception $e) {
return FALSE;
}

switch (TRUE) {
case $input instanceof \DateTimeInterface:
$input = clone $input;
break;

case \is_string($input):
case is_numeric($input):
// If date/time string: convert to timestamp first.
if (\is_string($input)) {
$input = strtotime($input);
}
try {
$input = new \DateTime('@' . $input);
}
catch (\Exception $e) {
$input = FALSE;
}
break;

default:
$input = FALSE;
break;
}

if ($input) {
// When we get here the input is always a datetime object.
$input = $input->setTimezone(new \DateTimeZone('UTC'));
return $input->format(\DateTimeInterface::RFC3339_EXTENDED);
}

return FALSE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\oe_search\Plugin\search_api\processor;

use Drupal\search_api\Processor\FieldsProcessorPluginBase;

/**
* Strips HTML tags from fulltext fields and decodes HTML entities.
*
* @SearchApiProcessor(
* id = "search_api_europa_search_processor_upper_keys",
* label = @Translation("Upper Case Field Keys"),
* description = @Translation("Upper case the field keys."),
* stages = {
* "pre_index_save" = 0,
* "preprocess_index" = 0,
* }
* )
*/
class SearchApiEuropaSearchUpperProcessorUpperKeys extends FieldsProcessorPluginBase {

/**
* {@inheritdoc}
*/
public function preprocessIndexItems(array $items) {
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $item) {
$fields = $item->getFields();
$uppercase_fields = [];
foreach ($fields as $name => $field) {
$uppercase_fields[strtoupper($name)] = $field;
}
$item->setFields($uppercase_fields);
}
}

}

0 comments on commit 32adb75

Please sign in to comment.