You are here

public function DependentFacetProcessor::buildConfigurationForm in Facets 8

Adds a configuration form for this processor.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

\Drupal\facets\FacetInterface $facet: The facet this processor is being added to.

Overrides ProcessorPluginBase::buildConfigurationForm

File

src/Plugin/facets/processor/DependentFacetProcessor.php, line 79

Class

DependentFacetProcessor
Provides a processor that makes a facet depend on the state of another facet.

Namespace

Drupal\facets\Plugin\facets\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $current_facet) {
  $build = [];
  $config = $this
    ->getConfiguration();

  // Loop over all defined blocks and filter them by provider, this builds an
  // array of blocks that are provided by the facets module.

  /** @var \Drupal\facets\Entity\Facet[] $facets */
  $facets = $this->facetStorage
    ->loadMultiple();
  foreach ($facets as $facet) {
    if ($facet
      ->getFacetSourceId() !== $current_facet
      ->getFacetSourceId()) {
      continue;
    }
    if ($facet
      ->id() === $current_facet
      ->id()) {
      continue;
    }
    $build[$facet
      ->id()]['label'] = [
      '#title' => $facet
        ->getName(),
      '#type' => 'label',
    ];
    $build[$facet
      ->id()]['enable'] = [
      '#title' => $this
        ->t('Enable condition'),
      '#type' => 'checkbox',
      '#default_value' => !empty($config[$facet
        ->id()]['enable']),
    ];
    $build[$facet
      ->id()]['condition'] = [
      '#title' => $this
        ->t('Condition mode'),
      '#type' => 'radios',
      '#options' => [
        'presence' => $this
          ->t('Check whether the facet is present.'),
        'not_empty' => $this
          ->t('Check whether the facet is selected / not empty.'),
        'values' => $this
          ->t('Check whether the facet is set to specific values.'),
      ],
      '#default_value' => empty($config[$facet
        ->id()]['condition']) ? NULL : $config[$facet
        ->id()]['condition'],
      '#states' => [
        'visible' => [
          ':input[name="facet_settings[' . $this
            ->getPluginId() . '][settings][' . $facet
            ->id() . '][enable]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $build[$facet
      ->id()]['values'] = [
      '#title' => $this
        ->t('Values'),
      '#type' => 'textfield',
      '#default_value' => empty($config[$facet
        ->id()]['values']) ? '' : $config[$facet
        ->id()]['values'],
      '#states' => [
        'visible' => [
          ':input[name="facet_settings[' . $this
            ->getPluginId() . '][settings][' . $facet
            ->id() . '][enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="facet_settings[' . $this
            ->getPluginId() . '][settings][' . $facet
            ->id() . '][condition]"]' => [
            'value' => 'values',
          ],
        ],
      ],
    ];
    $build[$facet
      ->id()]['negate'] = [
      '#title' => $this
        ->t('Negate condition'),
      '#type' => 'checkbox',
      '#default_value' => !empty($config[$facet
        ->id()]['negate']),
      '#states' => [
        'visible' => [
          ':input[name="facet_settings[' . $this
            ->getPluginId() . '][settings][' . $facet
            ->id() . '][enable]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  return parent::buildConfigurationForm($form, $form_state, $current_facet) + $build;
}