You are here

public function QuicklinkConfigForm::buildForm in Quicklink 8

Same name and namespace in other branches
  1. 2.0.x 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 65

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. One per line.'),
    '#default_value' => $config
      ->get('url_patterns_to_ignore'),
    '#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;',
    ],
  ];
  $form['overrides']['prefetch_only_paths'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Prefetch these paths only (overrides everything else)'),
    '#description' => $this
      ->t('If enabled, will override other 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;',
    ],
  ];

  // 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'),
  ];

  // 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);
}