You are here

public function NumberFieldBoost::buildConfigurationForm in Search API 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/search_api/processor/NumberFieldBoost.php, line 39

Class

NumberFieldBoost
Adds a boost based on a number field value.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
  $config = $this->configuration['boosts'];
  $additional_factors = [];
  foreach ($config as $field_configs) {
    if (isset($field_configs['boost_factor'])) {
      $additional_factors[] = $field_configs['boost_factor'];
    }
  }
  $boost_factors = Utility::getBoostFactors($additional_factors);
  $boost_factors[Utility::formatBoostFactor(0)] = $this
    ->t('Ignore');
  foreach ($this->index
    ->getFields(TRUE) as $field_id => $field) {
    if (in_array($field
      ->getType(), [
      'integer',
      'decimal',
    ])) {
      $form['boosts'][$field_id] = [
        '#type' => 'details',
        '#title' => $field
          ->getLabel(),
      ];
      $form['boosts'][$field_id]['boost_factor'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Boost factor'),
        '#options' => $boost_factors,
        '#description' => $this
          ->t('The boost factor the field value gets multiplied with. Setting it to 1.00 means using the field value as a boost as it is.'),
        '#default_value' => Utility::formatBoostFactor($config[$field_id]['boost_factor'] ?? 0),
      ];
      $form['boosts'][$field_id]['aggregation'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Aggregation'),
        '#options' => [
          'max' => $this
            ->t('maximum'),
          'min' => $this
            ->t('minimum'),
          'avg' => $this
            ->t('average'),
          'sum' => $this
            ->t('sum'),
          'mul' => $this
            ->t('product'),
          'first' => $this
            ->t('use first value'),
        ],
        '#description' => $this
          ->t('Select the method of aggregation to use in case the field has multiple values.'),
        '#default_value' => $config[$field_id]['aggregation'] ?? 'max',
        // @todo This shouldn't be dependent on the form array structure.
        //   Use the '#process' trick instead.
        '#states' => [
          'invisible' => [
            ":input[name=\"processors[number_field_boost][settings][boosts][{$field_id}][boost_factor]\"]" => [
              'value' => '',
            ],
          ],
        ],
      ];
    }
  }
  return $form;
}