You are here

function extlink_extra_form_extlink_admin_settings_alter in External Links Extra 8

Same name and namespace in other branches
  1. 7 extlink_extra.module \extlink_extra_form_extlink_admin_settings_alter()

Implements hook_form_FORM_ID_alter() for extlink_admin_settings.

File

./extlink_extra.module, line 239

Code

function extlink_extra_form_extlink_admin_settings_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Get configuration.
  $config = \Drupal::config('extlink_extra.settings');

  // Add a select field for the type of alert.
  $form['extlink_alert_type'] = [
    '#type' => 'select',
    '#title' => t('External link click reaction'),
    '#default_value' => $config
      ->get('extlink_alert_type') ?: 'confirm',
    '#description' => t('Choose the way you would like external links to be handled.'),
    '#options' => [
      'confirm' => t('A standard javascript confirm form will popup with the alert text'),
      'page' => t('The user will be taken to an intermediate warning page which will display the alert text'),
    ],
  ];

  // Add an extra option when the Colorbox module is enabled.
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('colorbox')) {
    $form['extlink_alert_type']['#options']['colorbox'] = t('A jQuery colorbox will be used for the alert (allows for HTML inside)');
    $form['extlink_alert_type']['#default_value'] = $config
      ->get('extlink_alert_type') ?: 'colorbox';
  }

  // Add a textfield for the modal width.
  $form['extlink_modal_width'] = [
    '#type' => 'textfield',
    '#title' => t('Modal width'),
    '#description' => t('The width of the modal. The height will be determined by the contents of the modal. Use 0 to let Drupal determine the width too.'),
    '#default_value' => $config
      ->get('extlink_modal_width') ?: 0,
    '#states' => [
      'visible' => [
        ':input[name=extlink_alert_type]' => [
          [
            'value' => 'modal',
          ],
        ],
      ],
    ],
  ];

  // Add a collapsible wrapper.
  $form['extlink_alert_text_fieldset'] = [
    '#type' => 'details',
    '#title' => t('Warning Text'),
    '#open' => TRUE,
    '#states' => [
      'invisible' => [
        ':input[name=extlink_alert_type]' => [
          'value' => '',
        ],
      ],
    ],
  ];

  // Add a textfield for the page title.
  $form['extlink_alert_text_fieldset']['extlink_page_title'] = [
    '#type' => 'textfield',
    '#title' => t('Warning Page Title'),
    '#description' => t('If you are using an intermediate page to display the leaving alert, you can specify it\'s the page title here.  You may also use the tokens indicated below.'),
    '#default_value' => $config
      ->get('extlink_page_title') ?: NULL,
    '#states' => [
      'visible' => [
        ':input[name=extlink_alert_type]' => [
          [
            'value' => 'modal',
          ],
          [
            'value' => 'page',
          ],
        ],
      ],
    ],
  ];

  // Convert the field for the text to a formatted text field with token support.
  $eat_default = [
    'value' => extlink_extra_alert_default(),
    'format' => filter_default_format(),
  ];
  $alert_text = $config
    ->get('extlink_alert_text') ?: $eat_default;
  $form['extlink_alert_text']['#type'] = 'text_format';
  $form['extlink_alert_text']['#default_value'] = $alert_text['value'];
  $form['extlink_alert_text']['#format'] = $alert_text['format'];
  $form['extlink_alert_text_fieldset']['token_tree'] = [
    '#theme' => 'token_tree_link',
    '#global_types' => TRUE,
    '#click_insert' => TRUE,
    '#weight' => 20,
    '#token_types' => [
      'extlink',
    ],
  ];

  // Move the original alert text textfield into the wrapper.
  $form['extlink_alert_text_fieldset']['extlink_alert_text'] = $form['extlink_alert_text'];
  unset($form['extlink_alert_text']);

  // Remove the 'Display pop-up warnings' checkbox that extlink.module provides.
  $form['extlink_alert']['#access'] = FALSE;

  // Add a select field for the type of alert.
  $form['extlink_alert_type'] = [
    '#type' => 'select',
    '#title' => t('External link click reaction'),
    '#default_value' => $config
      ->get('extlink_alert_type') ?: 'modal',
    '#description' => t('Choose the way you would like external links to be handled.'),
    '#options' => [
      'modal' => t('Drupal 8 core Modal Dialog (jQuery UI Dialog)'),
      'confirm' => t('A standard javascript confirm form will popup with the alert text'),
      'page' => t('The user will be taken to an intermediate warning page which will display the alert text'),
    ],
  ];

  // Add an extra option when the Colorbox module is enabled.
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('colorbox')) {
    $form['extlink_alert_type']['#options']['colorbox'] = t('A jQuery colorbox will be used for the alert (allows for HTML inside)');
  }

  // Add a number field for setting the interval for the redirect timer.
  $form['extlink_alert_timer'] = [
    '#type' => 'number',
    '#title' => t('Use automatic redirect timer'),
    '#default_value' => $config
      ->get('extlink_alert_timer') ?: 0,
    '#description' => t('If you would like the colorbox popup (if enabled) to automatically redirect the user after clicking clicking an external link, choose the number of seconds on the timer before it will happen.  Enter 0 for no automatic redirection.  Using this feature will not allow the link to open in a new window.'),
  ];

  // Handle caching.
  $form['extlink_cache_fix'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable aggressive caching compatibility'),
    '#description' => t('If you\'re running an aggressive caching system like varnish or memcached, you may find that the \'now-leaving\' page or colorbox popup gets cached
       and shows the same redirect tokens for all users.  Enabling this option will cause the module to overcome this by using client side (javascript) code to dynamically
       replace the values when the page is loaded.  <br/>
       <span class="error">Note</span> that this depends on your links being wrapped in the default classes: extlink-extra-back-action and extlink-extra-go-action.
       See extlink-extra-leaving.html.example.twig for an example.'),
    '#default_value' => $config
      ->get('extlink_cache_fix') ?: 0,
  ];

  // Handle accessibility.
  $form['extlink_508'] = [
    '#type' => 'details',
    '#title' => t('Section 508 Accessibility'),
    '#open' => FALSE,
  ];
  $form['extlink_508']['extlink_508_fix'] = [
    '#type' => 'checkbox',
    '#title' => t('Section 508 improvement for link indicators'),
    '#description' => t('Improves usability for screen readers by adding offscreen text to the span tags created by the External Link module.'),
    '#default_value' => $config
      ->get('extlink_508_fix') ?: 0,
  ];
  $form['extlink_508']['extlink_508_text'] = [
    '#type' => 'textfield',
    '#title' => t('Section 508 text'),
    '#description' => t('Screenreader text used when 508 fix is applied'),
    '#default_value' => $config
      ->get('extlink_508_text') ?: EXTLINK_EXTRA_508_TEXT,
    '#states' => [
      'invisible' => [
        ':input[name=extlink_508_fix]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];

  // Override options.
  $form['extlink_url_override'] = [
    '#type' => 'checkbox',
    '#title' => t('Allow query string parameters to set destination and back links'),
    '#description' => t('If you have advertisements and require a bumper for leaving the site, some advertisers use url parameters to set the destination.
     Select this checkbox to allow url parameters to set the destination and back links. Links must be prepended with http://.<br/>
     Eg. example.com/now-leaving?external_url=http://newurl.com&back_url=http://example.com/old-path.'),
    '#default_value' => $config
      ->get('extlink_url_override') ?: 0,
  ];

  // Add exclude option for alerts.
  $form['patterns']['#weight'] = 1;
  $form['patterns']['extlink_exclude_warning'] = array(
    '#title' => t('Don\'t warn for links matching the pattern'),
    '#description' => t('Enter a regular expression for external links that you wish <strong>not</strong> to display a warning when clicked'),
    '#type' => 'textfield',
    '#default_value' => $config
      ->get('extlink_exclude_warning') ?: '',
  );

  // Add a custom submit handler to save the settings in config.
  $form['#submit'][] = 'extlink_extra_admin_settings_submit';
}