You are here

public function EntityDisplayFormBase::submitForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_ui/src/Form/EntityDisplayFormBase.php \Drupal\field_ui\Form\EntityDisplayFormBase::submitForm()

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

core/modules/field_ui/src/Form/EntityDisplayFormBase.php, line 541

Class

EntityDisplayFormBase
Base class for EntityDisplay edit forms.

Namespace

Drupal\field_ui\Form

Code

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

  // If the main "Save" button was submitted while a field settings subform
  // was being edited, update the new incoming settings when rebuilding the
  // entity, just as if the subform's "Update" button had been submitted.
  if ($edit_field = $form_state
    ->get('plugin_settings_edit')) {
    $form_state
      ->set('plugin_settings_update', $edit_field);
  }
  parent::submitForm($form, $form_state);
  $form_values = $form_state
    ->getValues();

  // Handle the 'display modes' checkboxes if present.
  if ($this->entity
    ->getMode() == 'default' && !empty($form_values['display_modes_custom'])) {
    $display_modes = $this
      ->getDisplayModes();
    $current_statuses = $this
      ->getDisplayStatuses();
    $statuses = [];
    foreach ($form_values['display_modes_custom'] as $mode => $value) {
      if (!empty($value) && empty($current_statuses[$mode])) {

        // If no display exists for the newly enabled view mode, initialize
        // it with those from the 'default' view mode, which were used so
        // far.
        if (!$this->entityTypeManager
          ->getStorage($this->entity
          ->getEntityTypeId())
          ->load($this->entity
          ->getTargetEntityTypeId() . '.' . $this->entity
          ->getTargetBundle() . '.' . $mode)) {
          $display = $this
            ->getEntityDisplay($this->entity
            ->getTargetEntityTypeId(), $this->entity
            ->getTargetBundle(), 'default')
            ->createCopy($mode);
          $display
            ->save();
        }
        $display_mode_label = $display_modes[$mode]['label'];
        $url = $this
          ->getOverviewUrl($mode);
        $this
          ->messenger()
          ->addStatus($this
          ->t('The %display_mode mode now uses custom display settings. You might want to <a href=":url">configure them</a>.', [
          '%display_mode' => $display_mode_label,
          ':url' => $url
            ->toString(),
        ]));
      }
      $statuses[$mode] = !empty($value);
    }
    $this
      ->saveDisplayStatuses($statuses);
  }
  $this
    ->messenger()
    ->addStatus($this
    ->t('Your settings have been saved.'));
}