You are here

public function QuicklinkConfigForm::buildForm in Quicklink 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/QuicklinkConfigForm.php \Drupal\quicklink\Form\QuicklinkConfigForm::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/QuicklinkConfigForm.php, line 83

Class

QuicklinkConfigForm
Class QuicklinkConfig.

Namespace

Drupal\quicklink\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('quicklink.settings');

  // Build form elements.
  $form['settings'] = [
    '#type' => 'vertical_tabs',
    '#attributes' => [
      'class' => [
        'quicklink',
      ],
    ],
  ];

  // Ignore tab.
  $form['ignore'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Prefetch Ignore Settings'),
    '#description' => $this
      ->t('On this tab, specify what Quicklink should not prefetch.'),
    '#group' => 'settings',
  ];
  $form['ignore']['ignore_admin_paths'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Do not prefetch admin paths'),
    '#description' => $this
      ->t('Highly recommended. Ignore administrative paths.'),
    '#default_value' => $config
      ->get('ignore_admin_paths'),
  ];
  $form['ignore']['ignore_ajax_links'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Do not prefetch AJAX links'),
    '#description' => $this
      ->t('Highly recommended. Ignore links that trigger AJAX behavior.'),
    '#default_value' => $config
      ->get('ignore_ajax_links'),
  ];
  $form['ignore']['ignore_hashes'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Ignore paths with hashes (#) in them'),
    '#description' => $this
      ->t('Recommended. Prevents multiple prefetches of the same page.'),
    '#default_value' => $config
      ->get('ignore_hashes'),
  ];
  $form['ignore']['ignore_file_ext'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Ignore paths with file extensions'),
    '#description' => $this
      ->t('Recommended. This will ignore links that end with a file extension.
        It will match paths ending with a period followed by 1-5 characters. Querystrings are supported.'),
    '#default_value' => $config
      ->get('ignore_file_ext'),
  ];
  $form['ignore']['url_patterns_to_ignore'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('URL patterns to ignore (optional)'),
    '#description' => $this
      ->t('Quicklink will not fetch data if the URL contains any of these patterns. Enter one value per line.'),
    '#default_value' => $config
      ->get('url_patterns_to_ignore'),
    '#attributes' => [
      'style' => 'max-width: 600px;',
    ],
  ];
  $form['ignore']['ignore_selectors'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Ignore these selectors (optional)'),
    '#description' => $this
      ->t('Selectors that will not be prefected. Enter one value per line. Example: <code>.footer a</code>'),
    '#default_value' => $config
      ->get('ignore_selectors'),
    '#attributes' => [
      'style' => 'max-width: 600px;',
    ],
  ];

  // Overrides tab.
  $form['overrides'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Optional Overrides'),
    '#description' => $this
      ->t('On this tab, specify various overrides.'),
    '#group' => 'settings',
  ];
  $form['overrides']['selector'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Override parent selector (optional)'),
    '#description' => $this
      ->t('Quicklink will search this CSS selector for URLs to prefetch (ex. <code>.body-inner</code>). Defaults to the whole document.'),
    '#maxlength' => 128,
    '#size' => 128,
    '#default_value' => $config
      ->get('selector'),
    '#attributes' => [
      'style' => 'max-width: 600px;',
    ],
  ];
  $form['overrides']['allowed_domains'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Override allowed domains (optional)'),
    '#description' => $this
      ->t('List of domains to prefetch from. If empty, Quicklink will only prefetch links from the origin domain.
        If you configure this, be sure to input the origin domain. Add <code>true</code> here to allow <em>every</em> origin. Enter one value per line.'),
    '#default_value' => $config
      ->get('allowed_domains'),
    '#attributes' => [
      'style' => 'max-width: 600px;',
    ],
  ];

  // When to Prefetch tab.
  $form['when_load_library'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('When to Load Library'),
    '#description' => $this
      ->t('On this tab, specify when the Quicklink library will be loaded.'),
    '#group' => 'settings',
  ];
  $form['when_load_library']['no_load_when_authenticated'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Prefetch for anonymous users only'),
    '#description' => $this
      ->t('Highly recommended. Quicklink library will not be loaded for authenticated users.'),
    '#default_value' => $config
      ->get('no_load_when_authenticated'),
  ];
  $form['when_load_library']['no_load_when_session'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Do not prefetch during sessions'),
    '#description' => $this
      ->t('Recommended. Disables loading of the Quicklink library when a PHP session has been started. Useful for modules that use sessions (e.g. Drupal Commerce shopping carts).'),
    '#default_value' => $config
      ->get('no_load_when_session'),
  ];
  $options = [];
  $types = $this->entityTypeManager
    ->getStorage('node_type')
    ->loadMultiple();
  foreach ($types as $type) {
    $options[$type
      ->id()] = $type
      ->label();
  }
  $form['when_load_library']['no_load_content_types'] = [
    '#title' => $this
      ->t('Do not load library on these content types:'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $config
      ->get('no_load_content_types'),
  ];

  // Parameters tab.
  $form['throttle_options'] = [
    '#type' => 'details',
    '#title' => t('Throttle Options'),
    '#description' => t('On this tab, set parameters to adjust how Quicklink loads content.'),
    '#group' => 'settings',
  ];
  $form['throttle_options']['total_request_limit'] = [
    '#type' => 'number',
    '#title' => t('Set request limit'),
    '#description' => t('Enter the total number of requests that can be
        prefetched for a given page. Set to 0 for unlimited. <br><em>Warning</em>: this may not
        work when Concurrency Throttle is also enabled (see
        <a href="https://github.com/GoogleChromeLabs/quicklink/issues/235">https://github.com/GoogleChromeLabs/quicklink/issues/235</a>.'),
    '#maxlength' => 10,
    '#size' => 10,
    '#default_value' => $config
      ->get('total_request_limit', 0),
  ];
  $form['throttle_options']['concurrency_throttle_limit'] = [
    '#type' => 'number',
    '#title' => t('Set concurrency throttle'),
    '#description' => t('Enter a limit for the simultaneous requests that can be made while prefetching. Set to 0 for unlimited.'),
    '#maxlength' => 10,
    '#size' => 10,
    '#default_value' => $config
      ->get('concurrency_throttle_limit', 0),
  ];
  $form['throttle_options']['viewport_delay'] = [
    '#type' => 'number',
    '#title' => t('Viewport Delay'),
    '#field_suffix' => t('ms'),
    '#description' => t('Amount of time each link needs to stay inside the viewport before being prefetched. Default is 0 ms.'),
    '#maxlength' => 10,
    '#size' => 10,
    '#default_value' => $config
      ->get('viewport_delay', 0),
  ];
  $form['throttle_options']['idle_wait_timeout'] = [
    '#type' => 'number',
    '#title' => t('Set idle timeout value'),
    '#field_suffix' => t('ms'),
    '#description' => t('Amount of time the browser must be idle before prefetching, in milliseconds. Default is 2000 ms.'),
    '#maxlength' => 10,
    '#size' => 10,
    '#default_value' => $config
      ->get('idle_wait_timeout', 2000),
  ];

  // Prefetch Paths Only Tab
  $form['paths_only'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Prefetch Paths Only'),
    '#description' => $this
      ->t('On this tab, prefetch paths setting that will override all other settings.'),
    '#group' => 'settings',
  ];
  $form['paths_only']['warning_message'] = [
    '#type' => 'html_tag',
    '#tag' => 'p',
    '#value' => $this
      ->t('Warning: This setting negates all other settings.'),
    '#attributes' => [
      'class' => 'color-warning',
      'style' => 'max-width: 600px; padding: 1rem; font-weight: bold;',
    ],
  ];
  $form['paths_only']['prefetch_only_paths'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Prefetch these paths only (overrides everything else)'),
    '#description' => $this
      ->t('If enabled, this setting will override all other prefetch settings. <strong>Only these paths will be prefetched.</strong> Include the forward slash at the beginning of the path. Enter one value per line.'),
    '#default_value' => $config
      ->get('prefetch_only_paths'),
    '#attributes' => [
      'style' => 'max-width: 600px;',
    ],
  ];

  // Polyfill tab.
  $form['polyfill'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Extended Browser Support'),
    '#description' => $this
      ->t('On this tab, include support of additional browsers via polyfill.'),
    '#group' => 'settings',
  ];
  $form['polyfill']['load_polyfill'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Load <em>Intersection Observer</em> polyfill'),
    '#description' => $this
      ->t('This checkbox will enable loading of necessary polyfills from <a href="https://polyfill.io" target="_blank">polyfill.io</a>. This will enable usage of Quicklink in IE11 and older versions modern browsers.'),
    '#default_value' => $config
      ->get('load_polyfill'),
  ];

  // Debug tab.
  $form['debug'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Debug'),
    '#description' => $this
      ->t('On this tab, enable debug logging.'),
    '#group' => 'settings',
  ];
  $form['debug']['enable_debug_mode'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable debug mode'),
    '#description' => $this
      ->t("Log Quicklink development information to the HTML and JavaScript console."),
    '#default_value' => $config
      ->get('enable_debug_mode'),
  ];
  if ($this
    ->config('quicklink.settings')
    ->get('enable_debug_mode')) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Quicklink debug mode enabled. Be sure to disable this on production.'));
  }
  return parent::buildForm($form, $form_state);
}