Last active
May 14, 2018 23:19
-
-
Save simesy/0240e60c6c753970075fd04587f99b27 to your computer and use it in GitHub Desktop.
Offers a new "JSON Data" field when you "Add fields" in the Drupal search_api interface. If you set the field to "Full text" it will be searchable.
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 | |
| namespace Drupal\PROJECT\Plugin\search_api\processor; | |
| use Drupal\search_api\Datasource\DatasourceInterface; | |
| use Drupal\search_api\Item\ItemInterface; | |
| use Drupal\search_api\Processor\ProcessorPluginBase; | |
| use Drupal\search_api\Processor\ProcessorProperty; | |
| /** | |
| * Adds the item's URL to the indexed data. | |
| * | |
| * @SearchApiProcessor( | |
| * id = "jsonapi_data", | |
| * label = @Translation("{jsonapi} data"), | |
| * description = @Translation("Data as it appears on a JSON API endpoint"), | |
| * stages = { | |
| * "add_properties" = 0, | |
| * }, | |
| * locked = true, | |
| * hidden = true, | |
| * ) | |
| */ | |
| class JsonapiData extends ProcessorPluginBase { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { | |
| $properties = []; | |
| if (!$datasource) { | |
| $definition = [ | |
| 'label' => $this->t('{jsonapi} data'), | |
| 'description' => $this->t('Data as it appears on a JSON API endpoint'), | |
| 'type' => 'string', | |
| 'processor_id' => $this->getPluginId(), | |
| ]; | |
| $properties['jsonapi_data'] = new ProcessorProperty($definition); | |
| } | |
| return $properties; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function addFieldValues(ItemInterface $item) { | |
| $entity_type_id = $item->getDatasource()->getEntityTypeId($item->getOriginalObject()); | |
| $id = $item->getOriginalObject()->getValue()->id(); | |
| $entityManager = \Drupal::service('entity_type.manager')->getStorage($entity_type_id); | |
| $entityToJsonApi = \Drupal::service('jsonapi.entity.to_jsonapi'); | |
| $entity = $entityManager->load($id); | |
| $serialized_entity = $entityToJsonApi->serialize($entity); | |
| $fields = $this->getFieldsHelper() | |
| ->filterForPropertyPath($item->getFields(), NULL, 'jsonapi_data'); | |
| foreach ($fields as $field) { | |
| $field->addValue($serialized_entity); | |
| } | |
| } | |
| } |
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 | |
| // I'm not sure how default search might handle it, but in a custom endpoint | |
| // it looks something like this (untested) code. | |
| $index = SearchIndex::load('acquia_search_index'); | |
| $query = $index->query($options); | |
| $query->keys("some search string"); | |
| $results = $query->setSearchId($search_id)->execute(); | |
| /** @var \Drupal\search_api\Item\ItemInterface $result */ | |
| foreach ($results->getResultItems() as $item_id => $result) { | |
| if ($fieldItem = $result->getField('jsonapi_data')) { | |
| $values = $fieldItem->getValues(); | |
| $some_response_data['some_key'] = json_decode($values[0], TRUE)['data']; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment