Created
April 27, 2024 16:28
-
-
Save andybroomfield/60003442747f4a12e40e9287031bf810 to your computer and use it in GitHub Desktop.
Test Drupal search api index event subscriber for localgov_events date field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| namespace Drupal\localgov_events\EventSubscriber; | |
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
| use Drupal\search_api\Event\SearchApiEvents; | |
| use Drupal\search_api\Event\IndexingItemsEvent; | |
| use Drupal\search_api\Utility\Utility; | |
| /** | |
| * @todo Add description for this subscriber. | |
| */ | |
| final class IndexItemsSubscriber implements EventSubscriberInterface { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function getSubscribedEvents(): array { | |
| return [ | |
| SearchApiEvents::INDEXING_ITEMS => ['indexingItems'], | |
| ]; | |
| } | |
| /** | |
| * Reacts to the indexing items event. | |
| * | |
| * @param \Drupal\search_api\Event\IndexingItemsEvent $event | |
| * The indexing items event. | |
| */ | |
| public function indexingItems(IndexingItemsEvent $event) { | |
| $index = $event->getIndex(); | |
| // dpm(get_class_methods($index)); | |
| // dpm($index->id()); | |
| $index_id = $index->id(); | |
| if ($index_id != 'localgov_events') { | |
| return; | |
| } | |
| $items = $event->getItems(); | |
| // dpm($items); | |
| foreach($items as $key => $item) { | |
| list($datasource_type, $datasource_id) = Utility::splitCombinedId($key); | |
| list($nid, $lang) = Utility::splitPropertyPath($datasource_id); | |
| // dpm(get_class_methods($item)); | |
| // dpm($item->getId()); | |
| // dpm(Utility::splitCombinedId($key)); | |
| // dpm(Utility::splitPropertyPath(Utility::splitCombinedId($key)[1])); | |
| // dpm($item->getDatasourceId()); | |
| // dpm($item->getDatasource()); | |
| // dpm(get_class_methods($item->getDatasource())); | |
| // dpm($item->getDatasource()->getItemId()); | |
| // dpm($item->getOriginalObject()); | |
| // dpm($item->getField('localgov_event_date')); | |
| $date_field = $item->getField('localgov_event_date'); | |
| // dpm(get_class($date_field)); | |
| // dpm(get_class_methods($date_field)); | |
| // dpm($date_field->getDatasourceId()); | |
| // dpm($date_field->getDatasource()); | |
| // dpm(get_class_methods($date_field->getDatasource())); | |
| // dpm($date_field->getFieldIdentifier()); | |
| // dpm($date_field->getValues()); | |
| $table_name = 'date_recur__node__localgov_event_date'; | |
| $result = \Drupal::database()->select($table_name, 'occurrences') | |
| ->fields('occurrences', ['localgov_event_date_value']) | |
| ->condition('entity_id', $nid) | |
| ->execute(); | |
| $timestamps = []; | |
| $occurances = $result->fetchAll(); | |
| // dpm(array_column($occurances, 'localgov_event_date_value')); | |
| $timestamps = array_map(function($datetime) { | |
| return strtotime($datetime); | |
| }, array_column($occurances, 'localgov_event_date_value')); | |
| $date_field->setValues($timestamps); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a test event subscriber for https://github.com/localgovdrupal/localgov_events which takes the cached date occurrences table and adds them to the search index for the date field.
Note: This doesn't check revisions as currently the date_recur table is just a single (latest) revision. But if you have installed the patch or the table changes to store all revisions then it's important to get the latest revision id and select that.