You are here

public function ChannelForm::save in Entity Share 8

Same name and namespace in other branches
  1. 8.3 modules/entity_share_server/src/Form/ChannelForm.php \Drupal\entity_share_server\Form\ChannelForm::save()
  2. 8.2 modules/entity_share_server/src/Form/ChannelForm.php \Drupal\entity_share_server\Form\ChannelForm::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

modules/entity_share_server/src/Form/ChannelForm.php, line 135

Class

ChannelForm
Class ChannelForm.

Namespace

Drupal\entity_share_server\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\entity_share_server\Entity\ChannelInterface $channel */
  $channel = $this->entity;
  $authorized_users = array_filter($form_state
    ->getValue('authorized_users'));
  $channel
    ->set('authorized_users', $authorized_users);

  // Sorts order.
  $channel_sorts = $channel
    ->get('channel_sorts');
  if (is_null($channel_sorts)) {
    $channel_sorts = [];
  }
  $sorts = $form_state
    ->getValue('sort_table');
  if (!is_null($sorts) && is_array($sorts)) {
    foreach ($sorts as $sort_id => $sort) {
      $channel_sorts[$sort_id]['weight'] = $sort['weight'];
    }
  }
  $channel
    ->set('channel_sorts', $channel_sorts);
  $status = $channel
    ->save();
  switch ($status) {
    case SAVED_NEW:
      drupal_set_message($this
        ->t('Created the %label Channel.', [
        '%label' => $channel
          ->label(),
      ]));
      break;
    default:
      drupal_set_message($this
        ->t('Saved the %label Channel.', [
        '%label' => $channel
          ->label(),
      ]));
  }
  $form_state
    ->setRedirectUrl($channel
    ->toUrl('collection'));
}