You are here

public function LocalFontConfigEntityForm::save in @font-your-face 8.3

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/local_fonts/src/Form/LocalFontConfigEntityForm.php, line 124

Class

LocalFontConfigEntityForm
Config form to set the local fonts.

Namespace

Drupal\local_fonts\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  // Save Custom Font Config Entity.
  $local_font_config_entity = $this->entity;
  if (!empty($values['font_file'])) {

    // Get contents of Font File.
    $font_file = File::load($values['font_file'][0]);
    $font_file_data = base64_encode(file_get_contents($font_file
      ->getFileUri()));
    $local_font_config_entity
      ->setFontWoffData($font_file_data);
  }
  $status = $local_font_config_entity
    ->save();
  switch ($status) {
    case SAVED_NEW:
      \Drupal::messenger()
        ->addMessage($this
        ->t('Created the %label Custom Font.', [
        '%label' => $local_font_config_entity
          ->label(),
      ]));
      break;
    default:
      \Drupal::messenger()
        ->addMessage($this
        ->t('Saved the %label Custom Font.', [
        '%label' => $local_font_config_entity
          ->label(),
      ]));
  }
  $form_state
    ->setRedirectUrl($local_font_config_entity
    ->toUrl('collection'));
}