You are here

public function ConfigurationForm::buildForm in Progressive Web App 8

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

Form constructor.

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

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/ConfigurationForm.php, line 69

Class

ConfigurationForm
Class ConfigurationForm.

Namespace

Drupal\pwa\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $host = $this
    ->getRequest()->server
    ->get('HTTP_HOST');
  $files_path = file_create_url("public://pwa") . '/';
  if (substr($files_path, 0, 7) == 'http://') {
    $files_path = str_replace('http://', '', $files_path);
  }
  elseif (substr($files_path, 0, 8) == 'https://') {
    $files_path = str_replace('https://', '', $files_path);
  }
  if (substr($files_path, 0, 4) == 'www.') {
    $files_path = str_replace('www.', '', $files_path);
  }
  $host = $this
    ->getRequest()->server
    ->get('HTTP_HOST');
  if (substr($files_path, 0, strlen($host)) == $host) {
    $files_path = str_replace($host, '', $files_path);
  }
  $wrapper = \Drupal::service('stream_wrapper_manager')
    ->getViaScheme(\Drupal::config('system.file')
    ->get('default_scheme'));
  $realpath = \Drupal::service('file_system')
    ->realpath(\Drupal::config('system.file')
    ->get('default_scheme') . "://");
  $config = $this
    ->config('pwa.config');
  $form['manifest'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Manifest'),
    '#open' => TRUE,
  ];
  $form['manifest']['name'] = [
    "#type" => 'textfield',
    '#title' => $this
      ->t('Web app name'),
    '#description' => $this
      ->t("The name for the application that needs to be displayed to the user."),
    '#default_value' => $config
      ->get('site_name'),
    '#required' => TRUE,
    "#maxlength" => 55,
    '#size' => 60,
  ];
  $form['manifest']['short_name'] = [
    "#type" => 'textfield',
    "#title" => $this
      ->t('Short name'),
    "#description" => $this
      ->t("A short application name, this one gets displayed on the user's homescreen."),
    '#default_value' => $config
      ->get('short_name'),
    '#required' => TRUE,
    '#maxlength' => 25,
    '#size' => 30,
  ];
  $form['manifest']['lang'] = [
    "#type" => 'textfield',
    "#title" => $this
      ->t('Lang'),
    "#description" => $this
      ->t('The default language of the manifest.'),
    '#default_value' => $config
      ->get('lang'),
    '#required' => TRUE,
    '#maxlength' => 25,
    '#size' => 30,
  ];
  $form['manifest']['description'] = [
    "#type" => 'textfield',
    "#title" => $this
      ->t('Description'),
    "#description" => $this
      ->t('The description of your PWA.'),
    '#default_value' => $config
      ->get('description'),
    '#maxlength' => 255,
    '#size' => 60,
  ];
  $form['manifest']['start_url'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Start URL'),
    '#description' => $this
      ->t('Start URL.'),
    '#default_value' => $config
      ->get('start_url'),
    '#rows' => 1,
  ];
  $form['manifest']['theme_color'] = [
    "#type" => 'color',
    "#title" => $this
      ->t('Theme color'),
    "#description" => $this
      ->t('This color sometimes affects how the application is displayed by the OS.'),
    '#default_value' => $config
      ->get('theme_color'),
    '#required' => TRUE,
  ];
  $form['manifest']['background_color'] = [
    "#type" => 'color',
    "#title" => $this
      ->t('Background color'),
    "#description" => $this
      ->t('This color gets shown as the background when the application is launched'),
    '#default_value' => $config
      ->get('background_color'),
    '#required' => TRUE,
  ];
  $id = $this
    ->getDisplayValue($config
    ->get('display'), TRUE);
  $form['manifest']['display'] = [
    "#type" => 'select',
    "#title" => $this
      ->t('Display type'),
    "#description" => $this
      ->t('This determines which UI elements from the OS are displayed.'),
    "#options" => [
      '1' => $this
        ->t('fullscreen'),
      '2' => $this
        ->t('standalone'),
      '3' => $this
        ->t('minimal-ui'),
      '4' => $this
        ->t('browser'),
    ],
    '#default_value' => $id,
    '#required' => TRUE,
  ];
  $form['manifest']['cross_origin'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('The site is behind HTTP basic authentication'),
    '#description' => $this
      ->t('This will ensure any login credentials are passed to the manifest.'),
    '#title_display' => 'after',
    '#default_value' => $config
      ->get('cross_origin'),
  ];
  $validators = [
    'file_validate_extensions' => [
      'png',
    ],
    'file_validate_image_resolution' => [
      '512x512',
      '512x512',
    ],
  ];
  $form['manifest']['default_image'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use the theme image'),
    "#description" => $this
      ->t('This depends on the logo that the theme generates'),
    "#default_value" => $config
      ->get('default_image'),
  ];
  $form['manifest']['images'] = [
    '#type' => 'fieldset',
    '#states' => [
      'invisible' => [
        ':input[name="default_image"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['manifest']['images']['image'] = [
    '#type' => 'managed_file',
    '#name' => 'image',
    '#title' => $this
      ->t('Image'),
    '#size' => 20,
    '#description' => $this
      ->t('This image is your application icon. (png files only, format: (512x512)'),
    '#upload_validators' => $validators,
    '#upload_location' => 'public://pwa/',
  ];
  $bobTheHTMLBuilder = '<label>Current Image:</label> <br/> <img src="' . $config
    ->get('image') . '" width="200"/>';
  if ($config
    ->get('default_image') == 0) {
    $form['manifest']['images']['current_image'] = [
      '#markup' => $bobTheHTMLBuilder,
      '#name' => 'current image',
      '#id' => 'current_image',
    ];
  }
  $form['service_worker'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Service worker'),
    '#open' => TRUE,
  ];
  $form['service_worker']['urls_to_cache'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('URLs to cache on install'),
    '#description' => $this
      ->t('These will serve the page offline even if they have not been visited. Make sure the URL is not a 404. Make sure are these are relative URLs, tokens or regex are not supported. Because we cache these, you may need to flush your cache when changing this value.'),
    '#default_value' => $config
      ->get('urls_to_cache'),
    '#rows' => 7,
  ];
  $form['service_worker']['urls_to_exclude'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('URLs to exclude'),
    '#description' => $this
      ->t('Takes a regex, these URLs will use network-only, default config should be, admin/.* and user/reset/.*.'),
    '#default_value' => $config
      ->get('urls_to_exclude'),
    '#rows' => 7,
  ];
  $form['service_worker']['offline_page'] = [
    '#type' => 'textfield',
    '#title' => t('Offline page'),
    '#default_value' => $config
      ->get('offline_page') ?: '/offline',
    '#size' => 40,
    '#description' => t('This page is displayed when the user is offline and the requested page is not cached. It is automatically added to the "URLs to cache". Use <code>/offline</code> for a generic "You are offline" page.'),
  ];
  $form['service_worker']['cache_version'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Cache version'),
    '#description' => $this
      ->t('Changing this number will invalidate all Service Worker caches. Use it when assets have significantly changed or if you want to force a cache refresh for all clients.'),
    '#size' => 5,
    '#default_value' => $config
      ->get('cache_version') ?: 1,
  ];
  $form['service_worker']['skip_waiting'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Skip waiting'),
    '#description' => $this
      ->t("If enabled, an updated service worker will not wait, but instead activates as soon as it's finished installing"),
    '#title_display' => 'after',
    '#default_value' => $config
      ->get('skip_waiting'),
  ];
  return parent::buildForm($form, $form_state);
}