Snippets
Created by
Agustin Gomez Iglesias
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | <?php
/**
* @file
* Contains \Drupal\MY_MODULE\Plugin\search_api\processor\AddCustomField.
*
* A proccesor example that adds some custom fields.
*
* @NOTE
* - Replace any occurrences of MY_MODULE with your module machine_name.
* - Name this file AddCustomField.php and place it in the correct folder.
*
* @TODO Make some tests.
*/
namespace Drupal\MY_MODULE\Plugin\search_api\processor;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
/**
* @SearchApiProcessor(
* id = "MY_MODULE_add_custom_field",
* label = @Translation("Custom field examples"),
* description = @Translation("Adds custom field examples."),
* stages = {
* "pre_index_save" = -10,
* "preprocess_index" = -30
* }
* )
*/
class AddCustomField extends ProcessorPluginBase {
/**
* {@inheritdoc}
*
*/
public function alterPropertyDefinitions(array &$properties, DatasourceInterface $datasource = NULL) {
if ($datasource) {
return;
}
// Ensure that our fields are defined.
$fields = $this->getFieldsDefinition();
foreach ($fields as $field_id => $field_definition) {
$properties[$field_id] = new DataDefinition($field_definition);
}
}
/**
* {@inheritdoc}
*/
public function preprocessIndexItems(array &$items) {
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $item) {
// First custom field will only be added if we are indexing "nid" field.
foreach ($this->filterForPropertyPath($item->getFields(), 'nid') as $field_reference) {
foreach ($this->filterForPropertyPath($item->getFields(), 'MY_MODULE_field_based_on_nid') as $nid_based_field) {
$nid_based_field->addValue('Node Id: ' . reset($field_reference->getValues()));
}
}
// We will add always this custom field, with random value.
foreach ($this->filterForPropertyPath($item->getFields(), 'MY_MODULE_field_active_standby') as $always_added_field) {
$active_standby = rand(0,1) ? 'active' : 'standby';
$always_added_field->addValue($active_standby);
}
// We will add a time() and see how it is indexed.
foreach ($this->filterForPropertyPath($item->getFields(), 'MY_MODULE_field_date_experiment') as $date_experiment_field) {
$date_experiment_field->addValue(time());
}
}
}
/**
* {@inheritdoc}
*
*/
public function preIndexSave() {
foreach ($this->getFieldsDefinition() as $field_id => $field_definition) {
// @NOTE
// If we add the type as third parameter, the user will not be able
// to change it through the UI (the type will be blocked).
$this->ensureField(NULL, $field_id, $field_definition['type']);
}
}
/**
* Helper function for defining our custom fields.
*/
protected function getFieldsDefinition() {
$fields['MY_MODULE_field_based_on_nid'] = array(
'label' => 'Custom field based on nid',
'description' => 'I will be used to make a text type field with an id if "nid" field exists.',
'type' => 'text',
'prefix' => 't', // For Solr fields I think it is ok.
);
$fields['MY_MODULE_field_active_standby'] = array(
'label' => 'Custom field always added, could be used for facets',
'description' => 'Custom field 2 of type string will be always added with values active/standby.',
'type' => 'string',
'prefix' => 's', // For Solr fields I think it is ok.
);
$fields['MY_MODULE_field_date_experiment'] = array(
'label' => 'Custom field of date type, always added',
'description' => 'I will be added as a time(), but it will be indexed differently.',
'type' => 'date',
'prefix' => 'd', // For Solr fields I think it is ok.
);
return $fields;
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.