Skip to content

Commit

Permalink
Merge pull request #309 from esmero/ISSUE-308
Browse files Browse the repository at this point in the history
ISSUE-308: This adds {1992..1996,...} to Solr indexing + Session ISSUE-310 + ISSUE-313
  • Loading branch information
DiegoPino authored Mar 22, 2024
2 parents 4a8a450 + 8adaa80 commit 5319d10
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 24 deletions.
38 changes: 37 additions & 1 deletion src/EventSubscriber/SearchApiSolrEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,37 @@

namespace Drupal\strawberryfield\EventSubscriber;

use Drupal\search_api\Event\IndexingItemsEvent;
use Drupal\search_api\Event\SearchApiEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\search_api_solr\Event\SearchApiSolrEvents;
use Drupal\search_api_solr\Event\PreQueryEvent;
use Drupal\search_api_solr\Event\PostConvertedQueryEvent;
use Solarium\Component\ComponentAwareQueryInterface;
use Drupal\strawberryfield\StrawberryfieldSearchAPIUtilityServiceInterface;

class SearchApiSolrEventSubscriber implements EventSubscriberInterface {
private StrawberryfieldSearchAPIUtilityServiceInterface $search_api_state;

/**
* @inheritDoc
*/
public static function getSubscribedEvents() {
return [
SearchApiSolrEvents::PRE_QUERY => 'preQuery',
SearchApiSolrEvents::POST_CONVERT_QUERY => 'convertedQuery'
SearchApiSolrEvents::POST_CONVERT_QUERY => 'convertedQuery',
SearchApiEvents::INDEXING_ITEMS => 'indexingItems',
];
}

/**
* StrawberryEventDeleteFlavorSubscriber constructor.
*
* @param StrawberryfieldSearchAPIUtilityServiceInterface $search_api_state
*/
public function __construct(StrawberryfieldSearchAPIUtilityServiceInterface $search_api_state) {
$this->search_api_state = $search_api_state;
}

/**
* @param \Drupal\search_api_solr\Event\PreQueryEvent $event
Expand Down Expand Up @@ -118,4 +131,27 @@ public function convertedQuery(PostConvertedQueryEvent $event): void {
}
}
}
/**
* Reacts to the indexing items event.
*
* @param \Drupal\search_api\Event\IndexingItemsEvent $event
* The indexing items event.
*/
public function indexingItems(IndexingItemsEvent $event) {
$items = $event->getItems();
if (count($items)) {
$this->search_api_state->setIsIndexing(TRUE);
}
}


/**
* Reacts to the finished indexing items event.
*
* @param \Drupal\search_api\Event\IndexingItemsEvent $event
* The indexing items event.
*/
public function finishedIndexingItems(IndexingItemsEvent $event) {
$this->search_api_state->setIsIndexing(FALSE);
}
}
69 changes: 48 additions & 21 deletions src/Plugin/DataType/StrawberryValuesViaJmesPathFromJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,55 @@ public function process($langcode = NULL)
if ($result->isValid()) {
$edtf_value = $result->getEdtfValue();
// @todo remove once EDTF fixes their invalid Constructor for EDTF\Model\Interval that should per interface never allow NULL for start nor end date
switch(get_class($edtf_value)) {
case "EDTF\Model\Interval":
if($edtf_value->hasStartDate()) {
$values_parsed[] = date('c', $edtf_value->getMin());
if (get_class($edtf_value) == "EDTF\Model\Set") {
//means we have something like [1977, 1984/2023] or {1977, 1984/2023}
// and each entry needs to be processed like individual elements
foreach ($edtf_value->getElements() as $element) {
switch(get_class($element)) {
case "EDTF\Model\SetElement\RangeSetElement":
$values_parsed[] = date('c', $element->getMinAsUnixTimestamp());
$values_parsed[] = date('c', $element->getMaxAsUnixTimestamp());
break;
default:
// Make sure we do not index same day twice
$start_day = date('Y-m-d', $element->getMinAsUnixTimestamp());
$end_day = date('Y-m-d', $element->getMaxAsUnixTimestamp());
if ($start_day === $end_day) {
// if this is the same day just index one.
$values_parsed[] = date('c', $element->getMinAsUnixTimestamp());
}
else {
$values_parsed[] = date('c', $element->getMinAsUnixTimestamp());
$values_parsed[] = date('c', $element->getMaxAsUnixTimestamp());
}
break;
}
if($edtf_value->hasEndDate()) {
$values_parsed[] = date('c', $edtf_value->getMax());
}
break;
default:
// Make sure we do not index same day twice
$start_day = date('Y-m-d', $edtf_value->getMin());
$end_day = date('Y-m-d', $edtf_value->getMax());
if ($start_day === $end_day) {
// if this is the same day just index one.
$values_parsed[] = date('c', $edtf_value->getMin());
}
else {
$values_parsed[] = date('c', $edtf_value->getMin());
$values_parsed[] = date('c', $edtf_value->getMax());
}
break;
}
}
else {
//single entries.
switch (get_class($edtf_value)) {
case "EDTF\Model\Interval":
if ($edtf_value->hasStartDate()) {
$values_parsed[] = date('c', $edtf_value->getMin());
}
if ($edtf_value->hasEndDate()) {
$values_parsed[] = date('c', $edtf_value->getMax());
}
break;
default:
// Make sure we do not index same day twice
$start_day = date('Y-m-d', $edtf_value->getMin());
$end_day = date('Y-m-d', $edtf_value->getMax());
if ($start_day === $end_day) {
// if this is the same day just index one.
$values_parsed[] = date('c', $edtf_value->getMin());
} else {
$values_parsed[] = date('c', $edtf_value->getMin());
$values_parsed[] = date('c', $edtf_value->getMax());
}
break;
}
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,8 @@ public static function getValidIndexes() {
if (!$index->isValidDatasource($datasource_id)) {
unset($indexes[$index_id]);
}

return $indexes;
}
return $indexes;
}


Expand Down
53 changes: 53 additions & 0 deletions src/StrawberryfieldRenderCallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace Drupal\strawberryfield;

use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\views\Exception\ViewRenderElementException;
use Drupal\views\Views;

/**
* Defines a class for render callbacks.
*
* @internal
*/
final class StrawberryfieldRenderCallbacks implements TrustedCallbackInterface
{

/**
* #pre_render callback to alter views session starting crazyness during an index operation.
*/
public static function preRender($build) {
if (\Drupal::service('strawberryfield.search_api_state_helper')->isIndexing()) {
// Initialize the exposed filters so Views won't start the session.
// @see \Drupal\views\ViewExecutable::getExposedInput()
/** @var \Drupal\views\ViewExecutable|null $view */

if (!isset($build['#view'])) {
$view = Views::getView($build['#name']);
if (!$view) {
throw new ViewRenderElementException("Invalid View name ({$build['#name']}) given.");
}
$build['#view'] = $view;
}
$view = $build['#view'] ?? NULL;
if ($view) {
$view->initDisplay();
$view->setExposedInput(['' => '']);
// Disable render caching.
// $build['#cache']['max-age'] = 0;
}
}
return $build;
}

/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['preRender'];
}


}
37 changes: 37 additions & 0 deletions src/StrawberryfieldSearchAPIUtilityService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\strawberryfield;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\ParseMode\ParseModePluginManager;
use Drupal\search_api\Query\QueryInterface;
use Drupal\strawberryfield\Plugin\search_api\datasource\StrawberryfieldFlavorDatasource;

/**
* Provides the most basic Container we can keep a memory state of past Search API events.
*/
class StrawberryfieldSearchAPIUtilityService implements StrawberryfieldSearchAPIUtilityServiceInterface {

protected $isIndexing = FALSE;

public function isIndexing(): bool
{
return $this->isIndexing;
}

public function setIsIndexing(bool $isIndexing): void
{
$this->isIndexing = $isIndexing;
}


}
13 changes: 13 additions & 0 deletions src/StrawberryfieldSearchAPIUtilityServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Drupal\strawberryfield;

interface StrawberryfieldSearchAPIUtilityServiceInterface
{

public function isIndexing(): bool;


public function setIsIndexing(bool $isIndexing): void;

}
8 changes: 8 additions & 0 deletions strawberryfield.module
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\strawberryfield\Field\StrawberryFieldFileComputedItemList;
use Drupal\strawberryfield\Field\StrawberryFieldEntityComputedItemList;
use Drupal\strawberryfield\StrawberryfieldRenderCallbacks;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
Expand Down Expand Up @@ -360,3 +361,10 @@ function strawberryfield_entity_view_alter(array &$build, EntityInterface $entit
}
}
}

/**
* Implements hook_element_info_alter().
*/
function strawberryfield_element_info_alter(array &$info): void {
array_unshift($info['view']['#pre_render'], 'Drupal\strawberryfield\StrawberryfieldRenderCallbacks::preRender');
}
5 changes: 5 additions & 0 deletions strawberryfield.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ services:
class: Drupal\strawberryfield\EventSubscriber\SearchApiSolrEventSubscriber
tags:
- { name: event_subscriber }
arguments: [ '@strawberryfield.search_api_state_helper' ]
strawberryfield.early_rendering_controller_wrapper_subscriber:
class: Drupal\strawberryfield\EventSubscriber\EarlyRenderingControllerWrapperSubscriber
decorates: early_rendering_controller_wrapper_subscriber
Expand All @@ -122,3 +123,7 @@ services:
arguments: [ '@strawberryfield.early_rendering_controller_wrapper_subscriber.inner' ]
tags:
- { name: event_subscriber }
strawberryfield.search_api_state_helper:
class: \Drupal\strawberryfield\StrawberryfieldSearchAPIUtilityService
tags:
- { name: backend_overridable }

0 comments on commit 5319d10

Please sign in to comment.