You are here

public function ConfigurationForm::submitForm in Progressive Web App 8

Same name and namespace in other branches
  1. 2.x src/Form/ConfigurationForm.php \Drupal\pwa\Form\ConfigurationForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/ConfigurationForm.php, line 351

Class

ConfigurationForm
Class ConfigurationForm.

Namespace

Drupal\pwa\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('pwa.config');
  $display = $this
    ->getDisplayValue($form_state
    ->getValue('display'), FALSE);
  $fid = $form_state
    ->getValue([
    'image',
    0,
  ]);
  $default_image = $form_state
    ->getValue('default_image');
  if ($config
    ->get('default_image') == 0) {
    if (isset($fid) || $default_image == 1) {
      $this->manifest
        ->deleteImage();
    }
  }
  $configTheme = $this
    ->config('system.theme');
  $nameOfDefaultTheme = $configTheme
    ->get('default');

  // Get image from theme
  if ($default_image) {
    $theme_image = theme_get_setting('logo.path', $nameOfDefaultTheme);
    if (substr($theme_image, strlen($theme_image) - 3, 3) != 'png') {
      $this
        ->messenger()
        ->addWarning($this
        ->t('The theme image is not a .png file, your users may not be able to add this website to the homescreen.'));
    }
    $image_size = getimagesize(getcwd() . $theme_image);
    if ($image_size[0] == $image_size[1]) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('The theme image is not a square, your application image maybe altered (recommended size: 512x512).'));
    }
  }

  // Save new config data
  $config
    ->set('site_name', $form_state
    ->getValue('name'))
    ->set('short_name', $form_state
    ->getValue('short_name'))
    ->set('theme_color', $form_state
    ->getValue('theme_color'))
    ->set('background_color', $form_state
    ->getValue('background_color'))
    ->set('description', $form_state
    ->getValue('description'))
    ->set('lang', $form_state
    ->getValue('lang'))
    ->set('display', $display)
    ->set('default_image', $default_image)
    ->set('start_url', $form_state
    ->getValue('start_url'))
    ->set('urls_to_cache', $form_state
    ->getValue('urls_to_cache'))
    ->set('urls_to_exclude', $form_state
    ->getValue('urls_to_exclude'))
    ->set('offline_page', $form_state
    ->getValue('offline_page'))
    ->set('cache_version', $form_state
    ->getValue('cache_version'))
    ->set('cross_origin', $form_state
    ->getValue('cross_origin'))
    ->set('skip_waiting', $form_state
    ->getValue('skip_waiting'))
    ->save();

  // Save image if exists
  if (!empty($fid)) {
    $file = File::load($fid);
    $file_usage = \Drupal::service('file.usage');
    $file
      ->setPermanent();
    $file
      ->save();
    $file_usage
      ->add($file, 'PWA', 'PWA', $this
      ->currentUser()
      ->id());

    // Save new image.
    $wrapper = \Drupal::service('stream_wrapper_manager')
      ->getViaScheme(\Drupal::config('system.file')
      ->get('default_scheme'));
    $files_path = '/' . $wrapper
      ->basePath() . '/pwa/';
    $file_uri = $files_path . $file
      ->getFilename();
    $file_path = $wrapper
      ->realpath() . '/pwa/' . $file
      ->getFilename();
    $config
      ->set('image', $file_uri)
      ->save();

    // for image_small
    $newSize = 192;
    $oldSize = 512;
    $src = imagecreatefrompng($file_path);
    $dst = imagecreatetruecolor($newSize, $newSize);

    // Make transparent background.
    $color = imagecolorallocatealpha($dst, 0, 0, 0, 127);
    imagefill($dst, 0, 0, $color);
    imagesavealpha($dst, TRUE);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newSize, $newSize, $oldSize, $oldSize);
    $path_to_copy = $file_path . 'copy.png';
    $stream = fopen($path_to_copy, 'w+');
    if ($stream == TRUE) {
      imagepng($dst, $stream);
      $config
        ->set('image_small', $file_uri . 'copy.png')
        ->save();
    }

    // for image_very_small
    $newSize = 144;
    $oldSize = 512;
    $src = imagecreatefrompng($file_path);
    $dst = imagecreatetruecolor($newSize, $newSize);

    // Make transparent background.
    $color = imagecolorallocatealpha($dst, 0, 0, 0, 127);
    imagefill($dst, 0, 0, $color);
    imagesavealpha($dst, TRUE);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newSize, $newSize, $oldSize, $oldSize);
    $path_to_copy = $file_path . 'copy2.png';
    if ($stream = fopen($path_to_copy, 'w+')) {
      imagepng($dst, $stream);
      $config
        ->set('image_very_small', $file_uri . 'copy2.png')
        ->save();
    }
  }
  Cache::invalidateTags([
    'manifestjson',
  ]);
  parent::submitForm($form, $form_state);
}