You are here

public function ConfigContentTypes::submitForm in Allow a content type only once (Only One) 8

Form submission handler.

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 ConfigFormBase::submitForm

File

src/Form/ConfigContentTypes.php, line 149

Class

ConfigContentTypes
Class ConfigContentTypes.

Namespace

Drupal\onlyone\Form

Code

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

  // Cleaning the not checked content types from the selected checkboxes.
  $content_types_checked = array_filter($form_state
    ->getValue('onlyone_node_types'));

  // Getting the configured content types.
  $onlyone_content_types = $this
    ->config('onlyone.settings')
    ->get('onlyone_node_types');

  // Checking if we have any change in the configured content types.
  if ($content_types_checked == array_values($onlyone_content_types)) {
    $this
      ->messenger()
      ->addWarning($this
      ->t("You don't have changed the configured content types."));
  }
  else {

    // Saving the configuration.
    $this
      ->config('onlyone.settings')
      ->set('onlyone_node_types', $content_types_checked)
      ->save();

    // Calling parent method.
    parent::submitForm($form, $form_state);

    // Dispatching the event related to a change in the configured content
    // types.
    $this->eventDispacher
      ->dispatch(OnlyOneEvents::CONTENT_TYPES_UPDATED);
  }
}