You are here

public function TypeBoost::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/TypeBoost.php, line 39

Class

TypeBoost
Adds a boost to indexed items based on their datasource and/or bundle.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $formState) {
  $datasource_configurations = [];
  $additional_factors = [];
  foreach ($this->index
    ->getDatasources() as $datasource_id => $datasource) {
    $datasource_configuration = $this->configuration['boosts'][$datasource_id] ?? [];
    $datasource_configuration += [
      'datasource_boost' => Utility::formatBoostFactor(1),
      'bundle_boosts' => [],
    ];
    $datasource_configurations[$datasource_id] = $datasource_configuration;
    $additional_factors = array_merge($additional_factors, [
      $datasource_configuration['datasource_boost'],
    ], $datasource_configuration['bundle_boosts']);
  }
  $boost_factors = Utility::getBoostFactors($additional_factors);
  $bundle_boost_options = [
    '' => $this
      ->t('Use datasource default'),
  ] + $boost_factors;
  foreach ($this->index
    ->getDatasources() as $datasource_id => $datasource) {
    $form['boosts'][$datasource_id] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Boost settings for %datasource', [
        '%datasource' => $datasource
          ->label(),
      ]),
      '#open' => TRUE,
      'datasource_boost' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Default boost for items from this datasource'),
        '#options' => $boost_factors,
        '#description' => $this
          ->t('A boost of 1.00 is the default. Assign a boost of 0.00 to not score the item at all.'),
        '#default_value' => $datasource_configurations[$datasource_id]['datasource_boost'],
      ],
    ];

    // Add a boost for every available bundle. Drop the "pseudo-bundle" that
    // is added when the datasource does not contain any bundles.
    $bundles = $datasource
      ->getBundles();
    if (count($bundles) === 1) {

      // Depending on the datasource, the pseudo-bundle might use the
      // datasource ID or the entity type ID.
      unset($bundles[$datasource_id], $bundles[$datasource
        ->getEntityTypeId()]);
    }
    $bundle_boosts = $datasource_configurations[$datasource_id]['bundle_boosts'];
    foreach ($bundles as $bundle => $bundle_label) {
      $bundle_boost = Utility::formatBoostFactor($bundle_boosts[$bundle] ?? 0);
      $form['boosts'][$datasource_id]['bundle_boosts'][$bundle] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Boost for the %bundle bundle', [
          '%bundle' => $bundle_label,
        ]),
        '#options' => $bundle_boost_options,
        '#default_value' => $bundle_boost,
      ];
    }
  }
  return $form;
}