View source
<?php
namespace Drupal\search_api\Plugin\search_api\processor\Property;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\FieldInterface;
use Drupal\search_api\Processor\ConfigurablePropertyBase;
use Drupal\search_api\Processor\ConfigurablePropertyInterface;
use Drupal\search_api\Utility\Utility;
class AggregatedFieldProperty extends ConfigurablePropertyBase {
use StringTranslationTrait;
public function defaultConfiguration() {
return [
'type' => 'union',
'fields' => [],
];
}
public function buildConfigurationForm(FieldInterface $field, array $form, FormStateInterface $form_state) {
$index = $field
->getIndex();
$configuration = $field
->getConfiguration();
$form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
$form['#tree'] = TRUE;
$form['type'] = [
'#type' => 'radios',
'#title' => $this
->t('Aggregation type'),
'#description' => $this
->t('Apart from the @union type, all types will result in just a single value.', [
'@union' => $this
->t('Union'),
]),
'#options' => $this
->getTypes(),
'#default_value' => $configuration['type'],
'#required' => TRUE,
];
foreach ($this
->getTypes('description') as $type => $description) {
$form['type'][$type]['#description'] = $description;
}
$form['fields'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Contained fields'),
'#options' => [],
'#attributes' => [
'class' => [
'search-api-checkboxes-list',
],
],
'#default_value' => $configuration['fields'],
'#required' => TRUE,
];
$datasource_labels = $this
->getDatasourceLabelPrefixes($index);
$properties = $this
->getAvailableProperties($index);
$field_options = [];
foreach ($properties as $combined_id => $property) {
list($datasource_id, $name) = Utility::splitCombinedId($combined_id);
if (!$datasource_id && $name == 'aggregated_field') {
continue;
}
$label = $datasource_labels[$datasource_id] . $property
->getLabel();
$field_options[$combined_id] = Utility::escapeHtml($label);
if ($property instanceof ConfigurablePropertyInterface) {
$description = $property
->getFieldDescription($field);
}
else {
$description = $property
->getDescription();
}
$form['fields'][$combined_id] = [
'#attributes' => [
'title' => $this
->t('Machine name: @name', [
'@name' => $name,
]),
],
'#description' => $description,
];
}
asort($field_options, SORT_NATURAL);
$selected = array_flip($configuration['fields']);
$form['fields']['#options'] = array_intersect_key($field_options, $selected);
$form['fields']['#options'] += array_diff_key($field_options, $selected);
$missing_properties = array_diff($configuration['fields'], array_keys($properties));
if ($missing_properties) {
foreach ($missing_properties as $combined_id) {
list(, $property_path) = Utility::splitCombinedId($combined_id);
if (strpos($property_path, ':')) {
$form['fields'][$combined_id] = [
'#type' => 'value',
'#value' => $combined_id,
];
}
}
}
return $form;
}
public function submitConfigurationForm(FieldInterface $field, array &$form, FormStateInterface $form_state) {
$values = [
'type' => $form_state
->getValue('type'),
'fields' => array_keys(array_filter($form_state
->getValue('fields'))),
];
$field
->setConfiguration($values);
}
public function getFieldDescription(FieldInterface $field) {
$index = $field
->getIndex();
$available_properties = $this
->getAvailableProperties($index);
$datasource_label_prefixes = $this
->getDatasourceLabelPrefixes($index);
$configuration = $field
->getConfiguration();
$fields = [];
foreach ($configuration['fields'] as $combined_id) {
list($datasource_id, $property_path) = Utility::splitCombinedId($combined_id);
$label = $property_path;
if (isset($available_properties[$combined_id])) {
$label = $available_properties[$combined_id]
->getLabel();
}
$fields[] = $datasource_label_prefixes[$datasource_id] . $label;
}
$type = $this
->getTypes()[$configuration['type']];
$arguments = [
'@type' => $type,
'@fields' => implode(', ', $fields),
];
return $this
->t('A @type aggregation of the following fields: @fields.', $arguments);
}
protected function getTypes($info = 'label') {
switch ($info) {
case 'label':
return [
'union' => $this
->t('Union'),
'concat' => $this
->t('Concatenation'),
'sum' => $this
->t('Sum'),
'count' => $this
->t('Count'),
'max' => $this
->t('Maximum'),
'min' => $this
->t('Minimum'),
'first' => $this
->t('First'),
'last' => $this
->t('Last'),
'first_char' => $this
->t('First letter'),
];
case 'description':
return [
'union' => $this
->t('The Union aggregation does an union operation of all the values of the field. 2 fields with 2 values each become 1 field with 4 values.'),
'concat' => $this
->t('The Concatenation aggregation concatenates the text data of all contained fields.'),
'sum' => $this
->t('The Sum aggregation adds the values of all contained fields numerically.'),
'count' => $this
->t('The Count aggregation takes the total number of contained field values as the aggregated field value.'),
'max' => $this
->t('The Maximum aggregation computes the numerically largest contained field value.'),
'min' => $this
->t('The Minimum aggregation computes the numerically smallest contained field value.'),
'first' => $this
->t('The First aggregation will simply keep the first encountered field value.'),
'last' => $this
->t('The Last aggregation will keep the last encountered field value.'),
'first_char' => $this
->t('The “First letter” aggregation uses just the first letter of the first encountered field value as the aggregated value. This can, for example, be used to build a Glossary view.'),
];
}
return [];
}
protected function getDatasourceLabelPrefixes(IndexInterface $index) {
$prefixes = [
NULL => $this
->t('General') . ' » ',
];
foreach ($index
->getDatasources() as $datasource_id => $datasource) {
$prefixes[$datasource_id] = $datasource
->label() . ' » ';
}
return $prefixes;
}
protected function getAvailableProperties(IndexInterface $index) {
$properties = [];
$datasource_ids = $index
->getDatasourceIds();
$datasource_ids[] = NULL;
foreach ($datasource_ids as $datasource_id) {
foreach ($index
->getPropertyDefinitions($datasource_id) as $property_path => $property) {
$properties[Utility::createCombinedId($datasource_id, $property_path)] = $property;
}
}
return $properties;
}
}