You are here

public function EmbedButtonForm::save in Embed 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/EmbedButtonForm.php, line 194

Class

EmbedButtonForm
Form controller for embed button forms.

Namespace

Drupal\embed\Form

Code

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

  /** @var \Drupal\embed\EmbedButtonInterface $button */
  $button = $this->entity;

  // Run embed type plugin submission.
  $plugin = $button
    ->getTypePlugin();
  $plugin_form_state = clone $form_state;
  $plugin_form_state
    ->setValues($button
    ->getTypeSettings());
  $plugin
    ->submitConfigurationForm($form['type_settings'], $plugin_form_state);
  $form_state
    ->setValue('type_settings', $plugin
    ->getConfiguration());
  $button
    ->set('type_settings', $plugin
    ->getConfiguration());

  // If a file was uploaded to be used as the icon, get an encoded URL to be
  // stored in the config entity.
  $icon_fid = $form_state
    ->getValue([
    'icon_file',
    '0',
  ]);
  if (!empty($icon_fid) && ($file = $this->entityTypeManager
    ->getStorage('file')
    ->load($icon_fid))) {
    $file
      ->setPermanent();
    $file
      ->save();
    $button
      ->set('icon', EmbedButton::convertImageToEncodedData($file
      ->getFileUri()));
  }
  elseif ($form_state
    ->getValue('icon_reset')) {
    $button
      ->set('icon', NULL);
  }
  $status = $button
    ->save();
  $t_args = [
    '%label' => $button
      ->label(),
  ];
  if ($status === SAVED_UPDATED) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The embed button %label has been updated.', $t_args));
    $this
      ->logger('embed')
      ->info('Updated embed button %label.', $t_args);
  }
  elseif ($status === SAVED_NEW) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The embed button %label has been added.', $t_args));
    $this
      ->logger('embed')
      ->info('Added embed button %label.', $t_args);
  }
  $form_state
    ->setRedirectUrl($button
    ->toUrl());
}