You are here

public function SiteSettingEntityTypeForm::save in Site Settings and Labels 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/SiteSettingEntityTypeForm.php, line 172

Class

SiteSettingEntityTypeForm
Class SiteSettingEntityTypeForm.

Namespace

Drupal\site_settings\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  if (!isset($values['existing_fieldset']) || $values['existing_fieldset'] == $this
    ->getCreateNewLabel()) {
    $this->entity->fieldset = $values['new_fieldset'];
  }
  else {
    $this->entity->fieldset = $values['existing_fieldset'];
  }
  $this->entity->multiple = $values['multiple'];

  /** @var \Drupal\site_settings\Entity\SiteSettingEntityType $site_setting_entity_type */
  $site_setting_entity_type = $this->entity;
  $status = $site_setting_entity_type
    ->save();
  switch ($status) {
    case SAVED_NEW:
      $this
        ->messenger()
        ->addMessage($this
        ->t('Created the %label Site Setting type.', [
        '%label' => $site_setting_entity_type
          ->label(),
      ]));
      break;
    default:
      $this
        ->messenger()
        ->addMessage($this
        ->t('Saved the %label Site Setting type.', [
        '%label' => $site_setting_entity_type
          ->label(),
      ]));
  }

  // Rebuild the site settings cache.
  $this->siteSettingsLoader
    ->clearCache();
  $route_name = 'entity.site_setting_entity_type.collection';
  $route_parameters = [];
  if ($this->moduleHandler
    ->moduleExists('field_ui')) {

    // Redirect the user to the add fields screen for this new entity type.
    $route_name = 'entity.site_setting_entity.field_ui_fields';
    $route_parameters = [
      'site_setting_entity_type' => $site_setting_entity_type
        ->id(),
    ];
  }
  $form_state
    ->setRedirect($route_name, $route_parameters);
}