-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
D8CORE-7074 Alter search api custom field to support better tokens
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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
23
src/Plugin/search_api/processor/Property/CustomValueProperty.php
This file contains 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
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; | ||
} | ||
|
||
} |
This file contains 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