You are here

function cleantalk_page_attachments_alter in Anti Spam by CleanTalk 9.1.x

Same name and namespace in other branches
  1. 8.4 cleantalk.module \cleantalk_page_attachments_alter()
  2. 8.3 cleantalk.module \cleantalk_page_attachments_alter()

Implements hook_page_attachments_alter()

File

./cleantalk.module, line 18

Code

function cleantalk_page_attachments_alter(array &$page) {
  if (\Drupal::request()->query
    ->get('search')) {
    if (\Drupal::config('cleantalk.settings')
      ->get('cleantalk_search_noindex')) {
      $skip_index_results = [
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'robots',
          'content' => 'noindex',
        ],
      ];
      $page['#attached']['html_head'][] = [
        $skip_index_results,
        'skip_index_results',
      ];
    }
  }
  if (!\Drupal::request()->query
    ->has('amp') && !\Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    $js_template = "var ct_check_js_val = '%s';";
    $js_addon_body = sprintf($js_template, CleantalkFuncs::_cleantalk_get_checkjs_value());

    # set AntiCrawler cookie
    $drupal_ac_antibot_cookie_value = "var drupal_ac_antibot_cookie_value = '%s';";
    $drupal_ac_antibot_js_body = sprintf($drupal_ac_antibot_cookie_value, CleantalkFuncs::create_ac_cookie_value());

    # used alt cookies
    $use_alt_cookies = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_alternative_cookies_session') ? 1 : 0;
    $use_alt_cookies_marker = "var ct_use_alt_cookies = " . $use_alt_cookies . ";";
    $page['#attached']['html_head'][] = array(
      array(
        '#tag' => 'script',
        '#value' => $js_addon_body . $drupal_ac_antibot_js_body . $use_alt_cookies_marker,
      ),
      'ga_scripts',
    );
    $page['#attached']['library'][] = 'cleantalk/apbct-public';
  }

  // If external checking is enabled, add the external JS library to the page.
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_external')) {
    $page['#attached']['library'][] = 'cleantalk/apbct-external';
  }
}