You are here

public function FacetSettingsForm::submitForm in Facets 8

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

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

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

Overrides EntityForm::submitForm

File

src/Form/FacetSettingsForm.php, line 241

Class

FacetSettingsForm
Provides a form for creating and editing facets.

Namespace

Drupal\facets\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  /** @var \Drupal\facets\FacetInterface $facet */
  $facet = $this
    ->getEntity();
  $is_new = $facet
    ->isNew();
  if ($is_new) {

    // On facet creation, enable all locked processors by default, using their
    // default settings.
    $stages = $this->processorPluginManager
      ->getProcessingStages();
    $processors_definitions = $this->processorPluginManager
      ->getDefinitions();
    foreach ($processors_definitions as $processor_id => $processor) {
      $is_locked = isset($processor['locked']) && $processor['locked'] == TRUE;
      $is_default_enabled = isset($processor['default_enabled']) && $processor['default_enabled'] == TRUE;
      if ($is_locked || $is_default_enabled) {
        $weights = [];
        foreach ($stages as $stage_id => $stage) {
          if (isset($processor['stages'][$stage_id])) {
            $weights[$stage_id] = $processor['stages'][$stage_id];
          }
        }
        $facet
          ->addProcessor([
          'processor_id' => $processor_id,
          'weights' => $weights,
          'settings' => [],
        ]);
      }
    }

    // Set a default widget for new facets.
    $facet
      ->setWidget('links');
    $facet
      ->setUrlAlias($form_state
      ->getValue('id'));
    $facet
      ->setWeight(0);

    // Set default empty behaviour.
    $facet
      ->setEmptyBehavior([
      'behavior' => 'none',
    ]);
    $facet
      ->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
  }
  $facet_source_id = $form_state
    ->getValue('facet_source_id');
  if (!is_null($facet_source_id) && $facet_source_id !== '') {

    /** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
    $facet_source = $this->facetSourcePluginManager
      ->createInstance($facet_source_id, [
      'facet' => $this
        ->getEntity(),
    ]);
    $facet_source
      ->submitConfigurationForm($form, $form_state);
  }
  $facet
    ->save();
  if ($is_new) {
    if ($this->moduleHandler
      ->moduleExists('block')) {
      $message = $this
        ->t('Facet %name has been created. Go to the <a href=":block_overview">Block overview page</a> to place the new block in the desired region.', [
        '%name' => $facet
          ->getName(),
        ':block_overview' => $this->urlGenerator
          ->generateFromRoute('block.admin_display'),
      ]);
      $this
        ->messenger()
        ->addMessage($message);
      $form_state
        ->setRedirect('entity.facets_facet.edit_form', [
        'facets_facet' => $facet
          ->id(),
      ]);
    }
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Facet %name has been updated.', [
      '%name' => $facet
        ->getName(),
    ]));
  }
  list($type, ) = explode(':', $facet_source_id);
  if ($type !== 'search_api') {
    return $facet;
  }

  // Ensure that the caching of the view display is disabled, so the search
  // correctly returns the facets.
  if (isset($facet_source) && $facet_source instanceof SearchApiFacetSourceInterface) {
    $view = $facet_source
      ->getViewsDisplay();
    if ($view !== NULL) {
      if ($view->display_handler instanceof Block) {
        $facet
          ->setOnlyVisibleWhenFacetSourceIsVisible(FALSE);
      }
      $view->display_handler
        ->overrideOption('cache', [
        'type' => 'none',
      ]);
      $view
        ->save();
      $this
        ->messenger()
        ->addMessage($this
        ->t('Caching of view %view has been disabled.', [
        '%view' => $view->storage
          ->label(),
      ]));
    }
  }
  return $facet;
}