You are here

class AggregatedFieldProperty in Search API 8

Defines an "aggregated field" property.

Hierarchy

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\Property
View 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

Namesort descending Modifiers Type Description Overrides
AggregatedFieldProperty::buildConfigurationForm public function Constructs a configuration form for a field based on this property. Overrides ConfigurablePropertyInterface::buildConfigurationForm
AggregatedFieldProperty::defaultConfiguration public function Gets the default configuration for this property. Overrides ConfigurablePropertyBase::defaultConfiguration
AggregatedFieldProperty::getAvailableProperties protected function Retrieve all properties available on the index.
AggregatedFieldProperty::getDatasourceLabelPrefixes protected function Retrieves label prefixes for an index's datasources.
AggregatedFieldProperty::getFieldDescription public function Retrieves the description for a field based on this property. Overrides ConfigurablePropertyBase::getFieldDescription
AggregatedFieldProperty::getTypes protected function Retrieves information about available aggregation types.
AggregatedFieldProperty::submitConfigurationForm public function Submits a configuration form for a field based on this property. Overrides ConfigurablePropertyBase::submitConfigurationForm
ConfigurablePropertyBase::validateConfigurationForm public function Validates a configuration form for a field based on this property. Overrides ConfigurablePropertyInterface::validateConfigurationForm
DataDefinition::$definition protected property The array holding values for all definition keys.
DataDefinition::addConstraint public function Adds a validation constraint. Overrides DataDefinitionInterface::addConstraint
DataDefinition::create public static function Creates a new data definition. 5
DataDefinition::createFromDataType public static function Creates a new data definition object. Overrides DataDefinitionInterface::createFromDataType 5
DataDefinition::getClass public function Returns the class used for creating the typed data object. Overrides DataDefinitionInterface::getClass 1
DataDefinition::getConstraint public function Returns a validation constraint. Overrides DataDefinitionInterface::getConstraint
DataDefinition::getConstraints public function Returns an array of validation constraints. Overrides DataDefinitionInterface::getConstraints 1
DataDefinition::getDataType public function Returns the data type of the data. Overrides DataDefinitionInterface::getDataType 2
DataDefinition::getDescription public function Returns a human readable description. Overrides DataDefinitionInterface::getDescription
DataDefinition::getLabel public function Returns a human readable label. Overrides DataDefinitionInterface::getLabel
DataDefinition::getSetting public function Returns the value of a given setting. Overrides DataDefinitionInterface::getSetting 2
DataDefinition::getSettings public function Returns the array of settings, as required by the used class. Overrides DataDefinitionInterface::getSettings 2
DataDefinition::isComputed public function Determines whether the data value is computed. Overrides DataDefinitionInterface::isComputed
DataDefinition::isInternal public function Determines whether the data value is internal. Overrides DataDefinitionInterface::isInternal 1
DataDefinition::isReadOnly public function Determines whether the data is read-only. Overrides DataDefinitionInterface::isReadOnly
DataDefinition::isRequired public function Determines whether a data value is required. Overrides DataDefinitionInterface::isRequired
DataDefinition::offsetExists public function This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868.
DataDefinition::offsetGet public function This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868.
DataDefinition::offsetSet public function This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868.
DataDefinition::offsetUnset public function This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868.
DataDefinition::setClass public function Sets the class used for creating the typed data object.
DataDefinition::setComputed public function Sets whether the data is computed.
DataDefinition::setConstraints public function Sets an array of validation constraints.
DataDefinition::setDataType public function Sets the data type. 1
DataDefinition::setDescription public function Sets the human-readable description.
DataDefinition::setInternal public function Sets the whether the data value should be internal.
DataDefinition::setLabel public function Sets the human-readable label.
DataDefinition::setReadOnly public function Sets whether the data is read-only.
DataDefinition::setRequired public function Sets whether the data is required.
DataDefinition::setSetting public function Sets a definition setting. 2
DataDefinition::setSettings public function Sets the array of settings, as required by the used class. 2
DataDefinition::toArray public function Returns all definition values as array.
DataDefinition::__construct public function Constructs a new data definition object. 1
DataDefinition::__sleep public function 2
ProcessorProperty::getProcessorId public function Retrieves the ID of the processor which defines this property. Overrides ProcessorPropertyInterface::getProcessorId
ProcessorProperty::isHidden public function Determines whether this property should be hidden from the UI. Overrides ProcessorPropertyInterface::isHidden
ProcessorProperty::isList public function Returns whether the data is multi-valued, i.e. a list of data items. Overrides DataDefinition::isList
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2