You are here

public function ConfigPagesForm::save in Config Pages 8

Same name and namespace in other branches
  1. 8.3 src/ConfigPagesForm.php \Drupal\config_pages\ConfigPagesForm::save()
  2. 8.2 src/ConfigPagesForm.php \Drupal\config_pages\ConfigPagesForm::save()

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/ConfigPagesForm.php, line 223

Class

ConfigPagesForm
Form controller for the custom config page edit forms.

Namespace

Drupal\config_pages

Code

public function save(array $form, FormStateInterface $form_state) {
  $config_pages = $this->entity;
  $type = ConfigPagesType::load($config_pages
    ->bundle());
  if (!$config_pages
    ->label()) {
    $config_pages
      ->setLabel($type
      ->label());
  }
  $config_pages->context = $type
    ->getContextData();

  // Save as a new revision if requested to do so.
  if (!$form_state
    ->isValueEmpty('revision')) {
    $config_pages
      ->setNewRevision();
  }
  $insert = $config_pages
    ->isNew();
  $config_pages
    ->save();
  $context = [
    '@type' => $config_pages
      ->bundle(),
    '%info' => $config_pages
      ->label(),
  ];
  $logger = $this
    ->logger('config_pages');
  $config_pages_type = $this->ConfigPagesTypeStorage
    ->load($config_pages
    ->bundle());
  $t_args = [
    '@type' => $config_pages_type
      ->label(),
    '%info' => $config_pages
      ->label(),
  ];
  if ($insert) {
    $logger
      ->notice('@type: added %info.', $context);
    drupal_set_message($this
      ->t('@type %info has been created.', $t_args));
  }
  else {
    $logger
      ->notice('@type: updated %info.', $context);
    drupal_set_message($this
      ->t('@type %info has been updated.', $t_args));
  }
  if ($config_pages
    ->id()) {
    $form_state
      ->setValue('id', $config_pages
      ->id());
    $form_state
      ->set('id', $config_pages
      ->id());
  }
  else {

    // In the unlikely case something went wrong on save, the config page will be
    // rebuilt and config page form redisplayed.
    drupal_set_message($this
      ->t('The config page could not be saved.'), 'error');
    $form_state
      ->setRebuild();
  }
}