You are here

public function CkeditorTemplateForm::save in Ckeditor templates user interface 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/CkeditorTemplateForm.php, line 130

Class

CkeditorTemplateForm
Implement config form for Ckeditor template.

Namespace

Drupal\ckeditor_templates_ui\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $template = $this->entity;
  $file = $form_state
    ->getValue('image_upload');
  if ($file) {
    $file_dir = 'public://ckeditor-templates';
    if (!is_dir($file_dir)) {
      $this->fileSystem
        ->prepareDirectory($file_dir, FileSystemInterface::CREATE_DIRECTORY);
    }
    $file_destination = 'public://ckeditor-templates/' . $file
      ->getFilename();
    $filename = $this->fileSystem
      ->copy($file
      ->getFileUri(), $file_destination);
    if ($filename) {
      $template
        ->set('image', $filename);
    }
  }
  $status = $template
    ->save();
  if ($status) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Saved the %label Ckeditor Template.', [
      '%label' => $template
        ->label(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('The %label Ckeditor Template was not saved.', [
      '%label' => $template
        ->label(),
    ]));
  }
  $form_state
    ->setRedirect('entity.ckeditor_template.collection');
}