You are here

function extlink_extra_preprocess_extlink_extra_leaving in External Links Extra 8

Implements hook_preprocess_HOOK for extlink_extra_leaving.

Adds variables for the template.

File

./extlink_extra.module, line 105

Code

function extlink_extra_preprocess_extlink_extra_leaving(&$variables) {

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

  // Prepare token replacement values.
  $cache_fix = $config
    ->get('extlink_cache_fix') ?: 0;
  $request = \Drupal::request();
  $external_url = $request->query
    ->get('external_url');

  // UrlHelper::isValid() considers spaces in URLs invalid if they are not
  // encoded so we make sure to encode them before checking validity.
  if ($external_url) {
    $external_url = _extlink_extra_encode_url_parts($external_url);
  }
  if (!$external_url || !UrlHelper::isValid($external_url, TRUE)) {
    $external_url = $cache_fix ? 'external-url-placeholder' : $_COOKIE['external_url'];
  }
  $variables['external_url'] = Url::fromUri($external_url)
    ->setAbsolute()
    ->toString();
  $back_url = $request->query
    ->get('back_url');

  // UrlHelper::isValid() considers spaces in URLs invalid if they are not
  // encoded so we make sure to encode them before checking validity.
  if ($back_url) {
    $back_url = _extlink_extra_encode_url_parts($back_url);
  }
  if (!$back_url || !UrlHelper::isValid($back_url, TRUE)) {
    $back_url = $cache_fix ? 'back-url-placeholder' : $_COOKIE['back_url'];
  }
  $variables['back_url'] = Url::fromUri($back_url)
    ->setAbsolute()
    ->toString();
  $extlink_token_data = [
    'extlink' => [
      'external_url' => $variables['external_url'],
      'back_url' => $variables['back_url'],
    ],
  ];

  // Fetch the alert text and replace tokens.
  $token = \Drupal::token();
  $eat_default = [
    'value' => 'This link will take you to an external web site. We are not responsible for their content.',
    'format' => filter_default_format(),
  ];
  $alert_text = $config
    ->get('extlink_alert_text') ?: $eat_default;
  $variables['alert_text'] = check_markup($token
    ->replace($alert_text['value'], $extlink_token_data), $alert_text['format']);

  // Fetch the timer markup.
  $variables['timer'] = extlink_extra_timer_markup();

  // Fetch the site name.
  $config = \Drupal::config('system.site');
  $variables['site_name'] = $config
    ->get('name') ?: '';
}