class AggregatedFieldProperty in Search API 8
Defines an "aggregated field" property.
Hierarchy
- class \Drupal\Core\TypedData\DataDefinition implements \Drupal\Core\TypedData\ArrayAccess, DataDefinitionInterface uses TypedDataTrait
- class \Drupal\search_api\Processor\ProcessorProperty implements ProcessorPropertyInterface
- class \Drupal\search_api\Processor\ConfigurablePropertyBase implements ConfigurablePropertyInterface
- class \Drupal\search_api\Plugin\search_api\processor\Property\AggregatedFieldProperty uses StringTranslationTrait
- class \Drupal\search_api\Processor\ConfigurablePropertyBase implements ConfigurablePropertyInterface
- class \Drupal\search_api\Processor\ProcessorProperty implements ProcessorPropertyInterface
Expanded class hierarchy of AggregatedFieldProperty
See also
\Drupal\search_api\Plugin\search_api\processor\AggregatedFields
2 files declare their use of AggregatedFieldProperty
- AggregatedFields.php in src/
Plugin/ search_api/ processor/ AggregatedFields.php - AggregatedFieldsTest.php in tests/
src/ Unit/ Processor/ AggregatedFieldsTest.php
File
- src/
Plugin/ search_api/ processor/ Property/ AggregatedFieldProperty.php, line 18
Namespace
Drupal\search_api\Plugin\search_api\processor\PropertyView source
class AggregatedFieldProperty extends ConfigurablePropertyBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'type' => 'union',
'fields' => [],
];
}
/**
* {@inheritdoc}
*/
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);
// Do not include the "aggregated field" property.
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,
];
}
// Set the field options in a way that sorts them first by whether they are
// selected (to quickly see which one are included) and second by their
// labels.
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);
// Make sure we do not remove nested fields (which can be added via config
// but won't be present in the UI).
$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;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
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);
}
/**
* Retrieves information about available aggregation types.
*
* @param string $info
* (optional) One of "label" or "description", to indicate what values
* should be returned for the types.
*
* @return array
* An array of the identifiers of the available types mapped to, depending
* on $info, their labels, their data types or their descriptions.
*/
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 [];
}
/**
* Retrieves label prefixes for an index's datasources.
*
* @param \Drupal\search_api\IndexInterface $index
* The search index.
*
* @return string[]
* An associative array mapping datasource IDs (and an empty string for
* datasource-independent properties) to their label prefixes.
*/
protected function getDatasourceLabelPrefixes(IndexInterface $index) {
$prefixes = [
NULL => $this
->t('General') . ' » ',
];
foreach ($index
->getDatasources() as $datasource_id => $datasource) {
$prefixes[$datasource_id] = $datasource
->label() . ' » ';
}
return $prefixes;
}
/**
* Retrieve all properties available on the index.
*
* The properties will be keyed by combined ID, which is a combination of the
* datasource ID and the property path. This is used internally in this class
* to easily identify any property on the index.
*
* @param \Drupal\search_api\IndexInterface $index
* The search index.
*
* @return \Drupal\Core\TypedData\DataDefinitionInterface[]
* All the properties available on the index, keyed by combined ID.
*
* @see \Drupal\search_api\Utility::createCombinedId()
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AggregatedFieldProperty:: |
public | function |
Constructs a configuration form for a field based on this property. Overrides ConfigurablePropertyInterface:: |
|
AggregatedFieldProperty:: |
public | function |
Gets the default configuration for this property. Overrides ConfigurablePropertyBase:: |
|
AggregatedFieldProperty:: |
protected | function | Retrieve all properties available on the index. | |
AggregatedFieldProperty:: |
protected | function | Retrieves label prefixes for an index's datasources. | |
AggregatedFieldProperty:: |
public | function |
Retrieves the description for a field based on this property. Overrides ConfigurablePropertyBase:: |
|
AggregatedFieldProperty:: |
protected | function | Retrieves information about available aggregation types. | |
AggregatedFieldProperty:: |
public | function |
Submits a configuration form for a field based on this property. Overrides ConfigurablePropertyBase:: |
|
ConfigurablePropertyBase:: |
public | function |
Validates a configuration form for a field based on this property. Overrides ConfigurablePropertyInterface:: |
|
DataDefinition:: |
protected | property | The array holding values for all definition keys. | |
DataDefinition:: |
public | function |
Adds a validation constraint. Overrides DataDefinitionInterface:: |
|
DataDefinition:: |
public static | function | Creates a new data definition. | 5 |
DataDefinition:: |
public static | function |
Creates a new data definition object. Overrides DataDefinitionInterface:: |
5 |
DataDefinition:: |
public | function |
Returns the class used for creating the typed data object. Overrides DataDefinitionInterface:: |
1 |
DataDefinition:: |
public | function |
Returns a validation constraint. Overrides DataDefinitionInterface:: |
|
DataDefinition:: |
public | function |
Returns an array of validation constraints. Overrides DataDefinitionInterface:: |
1 |
DataDefinition:: |
public | function |
Returns the data type of the data. Overrides DataDefinitionInterface:: |
2 |
DataDefinition:: |
public | function |
Returns a human readable description. Overrides DataDefinitionInterface:: |
|
DataDefinition:: |
public | function |
Returns a human readable label. Overrides DataDefinitionInterface:: |
|
DataDefinition:: |
public | function |
Returns the value of a given setting. Overrides DataDefinitionInterface:: |
2 |
DataDefinition:: |
public | function |
Returns the array of settings, as required by the used class. Overrides DataDefinitionInterface:: |
2 |
DataDefinition:: |
public | function |
Determines whether the data value is computed. Overrides DataDefinitionInterface:: |
|
DataDefinition:: |
public | function |
Determines whether the data value is internal. Overrides DataDefinitionInterface:: |
1 |
DataDefinition:: |
public | function |
Determines whether the data is read-only. Overrides DataDefinitionInterface:: |
|
DataDefinition:: |
public | function |
Determines whether a data value is required. Overrides DataDefinitionInterface:: |
|
DataDefinition:: |
public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. | |
DataDefinition:: |
public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. | |
DataDefinition:: |
public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. | |
DataDefinition:: |
public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. | |
DataDefinition:: |
public | function | Sets the class used for creating the typed data object. | |
DataDefinition:: |
public | function | Sets whether the data is computed. | |
DataDefinition:: |
public | function | Sets an array of validation constraints. | |
DataDefinition:: |
public | function | Sets the data type. | 1 |
DataDefinition:: |
public | function | Sets the human-readable description. | |
DataDefinition:: |
public | function | Sets the whether the data value should be internal. | |
DataDefinition:: |
public | function | Sets the human-readable label. | |
DataDefinition:: |
public | function | Sets whether the data is read-only. | |
DataDefinition:: |
public | function | Sets whether the data is required. | |
DataDefinition:: |
public | function | Sets a definition setting. | 2 |
DataDefinition:: |
public | function | Sets the array of settings, as required by the used class. | 2 |
DataDefinition:: |
public | function | Returns all definition values as array. | |
DataDefinition:: |
public | function | Constructs a new data definition object. | 1 |
DataDefinition:: |
public | function | 2 | |
ProcessorProperty:: |
public | function |
Retrieves the ID of the processor which defines this property. Overrides ProcessorPropertyInterface:: |
|
ProcessorProperty:: |
public | function |
Determines whether this property should be hidden from the UI. Overrides ProcessorPropertyInterface:: |
|
ProcessorProperty:: |
public | function |
Returns whether the data is multi-valued, i.e. a list of data items. Overrides DataDefinition:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TypedDataTrait:: |
protected | property | The typed data manager used for creating the data types. | |
TypedDataTrait:: |
public | function | Gets the typed data manager. | 2 |
TypedDataTrait:: |
public | function | Sets the typed data manager. | 2 |