You are here

public function FacetsSummarySettingsForm::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

modules/facets_summary/src/Form/FacetsSummarySettingsForm.php, line 186

Class

FacetsSummarySettingsForm
Provides a form for configuring the processors of a facet.

Namespace

Drupal\facets_summary\Form

Code

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

  /** @var \Drupal\facets_summary\FacetsSummaryInterface $facets_summary */
  $facets_summary = $this
    ->getEntity();
  $is_new = $facets_summary
    ->isNew();
  $facets_summary
    ->save();
  if ($is_new) {
    if ($this->moduleHandler
      ->moduleExists('block')) {
      $message = $this
        ->t('Facet Summary %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' => $facets_summary
          ->getName(),
        ':block_overview' => $this->urlGenerator
          ->generateFromRoute('block.admin_display'),
      ]);
      $this
        ->messenger()
        ->addMessage($message);
      $form_state
        ->setRedirect('entity.facets_summary.edit_form', [
        'facets_summary' => $facets_summary
          ->id(),
      ]);
    }

    // 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];
          }
        }
        $facets_summary
          ->addProcessor([
          'processor_id' => $processor_id,
          'weights' => $weights,
          'settings' => [],
        ]);
      }
    }
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Facet %name has been updated.', [
      '%name' => $facets_summary
        ->getName(),
    ]));
  }

  // Clear Drupal cache for blocks to reflect recent changes.
  $this->blockManager
    ->clearCachedDefinitions();
  $facet_source_id = $form_state
    ->getValue('facet_source_id');
  list($type, ) = explode(':', $facet_source_id);
  if ($type !== 'search_api') {
    return $facets_summary;
  }

  // Ensure that the caching of the view display is disabled, so the search
  // correctly returns the facets.
  $facet_source = $this->facetSourcePluginManager
    ->createInstance($facet_source_id, [
    'facet' => $this
      ->getEntity(),
  ]);
  if (isset($facet_source) && $facet_source instanceof SearchApiFacetSourceInterface) {
    $view = $facet_source
      ->getViewsDisplay();
    if ($view !== NULL) {
      $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 $facets_summary;
}