Skip to content

Commit

Permalink
D8CORE-7074 Alter search api custom field to support better tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Nov 2, 2023
1 parent ca3c9fd commit fa1b349
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Plugin/search_api/processor/CustomValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Drupal\stanford_profile_helper\Plugin\search_api\processor;

use Drupal\Core\Entity\EntityInterface;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Plugin\search_api\processor\CustomValue as SearchApiCustomValue;
use Drupal\stanford_profile_helper\Plugin\search_api\processor\Property\CustomValueProperty;

/**
* Extends original custom value search api field to support token_or module.
*/
class CustomValue extends SearchApiCustomValue {

/**
* {@inheritdoc}
*/
function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];

if (!$datasource) {
$definition = [
'label' => $this->t('Custom value'),
'description' => $this->t('Index a custom value with replacement tokens.'),
'type' => 'string',
'processor_id' => $this->getPluginId(),
];
$properties['custom_value'] = new CustomValueProperty($definition);
}

return $properties;
}

/**
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {
// Get all of the "custom_value" fields on this item.
$fields = $this->getFieldsHelper()
->filterForPropertyPath($item->getFields(), NULL, 'custom_value');
// If the indexed item is an entity, we can pass that as data to the token
// service. Otherwise, only global tokens are available.
$entity = $item->getOriginalObject()->getValue();
if ($entity instanceof EntityInterface) {
$data = [$entity->getEntityTypeId() => $entity];
}
else {
$data = [];
}

$token = $this->getToken();
foreach ($fields as $field) {
$config = $field->getConfiguration();
if (empty($config['value'])) {
continue;
}

$field_value = $token->replace($config['value'], $data, ['clear' => TRUE]);
if ($field_value !== '') {
$field->addValue($field_value);
}
}
}

}
23 changes: 23 additions & 0 deletions src/Plugin/search_api/processor/Property/CustomValueProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Drupal\stanford_profile_helper\Plugin\search_api\processor\Property;

use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Item\FieldInterface;
use Drupal\search_api\Plugin\search_api\processor\Property\CustomValueProperty as SearchApiCustomValueProperty;

/**
* Increases max length from the contrib module.
*/
class CustomValueProperty extends SearchApiCustomValueProperty {

/**
* {@inheritDoc}
*/
public function buildConfigurationForm(FieldInterface $field, array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($field, $form, $form_state);
$form['value']['#maxlength'] = 1000;
return $form;
}

}
7 changes: 7 additions & 0 deletions stanford_profile_helper.module
Original file line number Diff line number Diff line change
Expand Up @@ -1077,3 +1077,10 @@ function _stanford_profile_is_decoupled(): bool {
return (bool) \Drupal::state()
->get('stanford_profile_helper.decoupled', FALSE);
}

/**
* Implements hook_search_api_processor_info_alter().
*/
function stanford_profile_helper_search_api_processor_info_alter(array &$processors) {
$processors['custom_value']['class'] = '\Drupal\stanford_profile_helper\Plugin\search_api\processor\CustomValue';
}

0 comments on commit fa1b349

Please sign in to comment.