View source
<?php
namespace Drupal\search_api_glossary\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;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Plugin\PluginFormTrait;
use Drupal\Component\Utility\Html;
class Glossary extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
protected $targetFieldPrefix = 'glossaryaz_';
protected $dataTypeHelper;
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$glossary_fields = $this
->getConfig();
$fields = $this->index
->getFields();
foreach ($glossary_fields as $name => $glossary_field) {
if (isset($glossary_field['glossary']) && $glossary_field['glossary'] == 1 && isset($fields[$name])) {
$definition = [
'label' => 'Glossary AZ - ' . Html::escape($fields[$name]
->getPrefixedLabel()),
'description' => 'Glossary AZ - ' . Html::escape($fields[$name]
->getPrefixedLabel()),
'type' => 'string',
'processor_id' => $this
->getPluginId(),
'hidden' => TRUE,
];
$new_field_name = $this
->makeFieldName($name);
$properties[$new_field_name] = new ProcessorProperty($definition);
}
}
}
return $properties;
}
public function addFieldValues(ItemInterface $item) {
$glossary_helper = \Drupal::service('search_api_glossary.helper');
$item_fields = $item
->getFields();
$glossary_fields_conf = $this
->getConfig();
foreach ($item_fields as $name => $field) {
if ($field
->isHidden() == FALSE && $this
->testType($field
->getType()) && $this
->checkFieldName($name) == FALSE && isset($glossary_fields_conf[$name])) {
$glossary_field_conf = $glossary_fields_conf[$name]['glossary'];
if (isset($glossary_field_conf) && $glossary_field_conf == 1 && !empty($field
->getValues())) {
$source_field_value = $field
->getValues()[0];
$glossary_field_name = $this
->makeFieldName($name);
$glossary_value = $glossary_helper
->glossaryGetter($source_field_value, $glossary_fields_conf[$name]['grouping']);
$glossary_field = $item_fields[$glossary_field_name];
if (empty($glossary_field
->getValues())) {
$glossary_field
->addValue($glossary_value);
}
}
}
}
}
public function defaultConfiguration() {
return [
'field_enabled' => 0,
'grouping_defaults' => [
'grouping_other' => 'grouping_other',
'grouping_az' => 0,
'grouping_09' => 0,
],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$fields = $this->index
->getFields();
$form['glossarytable'] = [
'#type' => 'table',
'#header' => [
$this
->t('Field'),
$this
->t('Glossary Grouping'),
],
];
foreach ($fields as $name => $field) {
if ($field
->isHidden() == FALSE && $this
->testType($field
->getType()) && $this
->checkFieldName($name) == FALSE) {
$glossary_fields = $this
->getConfig();
$field_enabled = isset($glossary_fields[$name]['glossary']) ? $glossary_fields[$name]['glossary'] : $this->configuration['field_enabled'];
$field_grouping = isset($glossary_fields[$name]['grouping']) ? $glossary_fields[$name]['grouping'] : $this->configuration['grouping_defaults'];
$form['glossarytable'][$name]['glossary'] = [
'#type' => 'checkbox',
'#title' => Html::escape($field
->getPrefixedLabel()),
'#default_value' => $field_enabled,
];
$form['glossarytable'][$name]['grouping'] = [
'#type' => 'checkboxes',
'#description' => t('When grouping is enabled, individual values such as 1, 2, 3 will get grouped like "0-9"'),
'#options' => [
'grouping_az' => 'Group Alphabetic (A-Z)',
'grouping_09' => 'Group Numeric (0-9)',
'grouping_other' => 'Group Non Alpha Numeric (#)',
],
'#default_value' => $field_grouping,
'#required' => FALSE,
'#states' => [
'visible' => [
[
':input[name="processors[glossary][settings][glossarytable][' . $name . '][glossary]"]' => [
'checked' => TRUE,
],
],
],
],
];
}
}
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$glossarytable = $form_state
->getValues('glossarytable');
$this
->setConfig($glossarytable);
}
public function preIndexSave() {
$glossary_fields = $this
->getConfig();
$fields = $this->index
->getFields();
foreach ($glossary_fields as $name => $glossary_field) {
if (isset($glossary_field['glossary']) && $glossary_field['glossary'] == 1 && isset($fields[$name])) {
$new_field_name = $this
->makeFieldName($name);
$field = $this
->ensureField(NULL, $new_field_name, 'string');
$field
->setHidden();
}
}
}
protected function testType($type) {
return $this
->getDataTypeHelper()
->isTextType($type, [
'text',
'string',
'integer',
]);
}
public function getDataTypeHelper() {
return $this->dataTypeHelper ?: \Drupal::service('search_api.data_type_helper');
}
protected function makeFieldName($name) {
return $this->targetFieldPrefix . $name;
}
public function getFieldName($name) {
return str_replace($this->targetFieldPrefix, '', $name);
}
public function checkFieldName($name) {
if (substr($name, 0, strlen($this->targetFieldPrefix)) === $this->targetFieldPrefix) {
return TRUE;
}
return FALSE;
}
public function getConfig() {
if (isset($this->configuration['glossarytable'])) {
return unserialize($this->configuration['glossarytable'])['glossarytable'];
}
}
protected function setConfig(array $configuration) {
$this
->setConfiguration([
'glossarytable' => serialize($configuration),
]);
}
}