You are here

public function FlagFormBase::save in Flag 8.4

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/FlagFormBase.php, line 312

Class

FlagFormBase
Provides the base flag add/edit form.

Namespace

Drupal\flag\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $flag = $this->entity;
  $flag
    ->getFlagTypePlugin()
    ->submitConfigurationForm($form, $form_state);
  $flag
    ->getLinkTypePlugin()
    ->submitConfigurationForm($form, $form_state);
  $status = $flag
    ->save();
  $message_params = [
    '%label' => $flag
      ->label(),
  ];
  $logger_params = [
    '%label' => $flag
      ->label(),
    'link' => $flag
      ->toLink($this
      ->t('Edit'), 'edit-form')
      ->toString(),
  ];
  if ($status == SAVED_UPDATED) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Flag %label has been updated.', $message_params));
    $this
      ->logger('flag')
      ->notice('Flag %label has been updated.', $logger_params);
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Flag %label has been added.', $message_params));
    $this
      ->logger('flag')
      ->notice('Flag %label has been added.', $logger_params);
  }

  // We clear caches more vigorously if the flag was new.
  // _flag_clear_cache($flag->entity_type, !empty($flag->is_new));
  // Save permissions.
  // This needs to be done after the flag cache has been cleared, so that
  // the new permissions are picked up by hook_permission().
  // This may need to move to the flag class when we implement extra
  // permissions for different flag types: http://drupal.org/node/879988
  // If the flag ID has changed, clean up all the obsolete permissions.
  if ($flag
    ->id() != $form['#flag_name']) {
    $old_name = $form['#flag_name'];
    $permissions = [
      "flag {$old_name}",
      "unflag {$old_name}",
    ];
    foreach (array_keys(user_roles()) as $rid) {
      user_role_revoke_permissions($rid, $permissions);
    }
  }

  /*
      foreach (array_keys(user_roles(!\Drupal::moduleHandler()->moduleExists('session_api'))) as $rid) {
        // Create an array of permissions.
        $permissions = array(
          "flag $flag->name" => $flag->roles['flag'][$rid],
          "unflag $flag->name" => $flag->roles['unflag'][$rid],
        );
        user_role_change_permissions($rid, $permissions);
      }
  */

  // @todo: when we add database caching for flags we'll have to clear the
  // cache again here.
  $form_state
    ->setRedirect('entity.flag.collection');
}