You are here

site_verify.module in Site verification 8

Same filename and directory in other branches
  1. 6 site_verify.module
  2. 7.2 site_verify.module
  3. 7 site_verify.module

Used to demonstrate the possibilities of routing in Drupal 8.

File

site_verify.module
View source
<?php

/**
 * @file
 * Used to demonstrate the possibilities of routing in Drupal 8.
 */

/**
 * Implements hook_site_verify_engine_info().
 */
function site_verify_site_verify_engine_info() {
  $engines['google'] = [
    'name' => t('Google'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'file_example' => 'google1234567890abcdef.html',
    'meta' => TRUE,
    'meta_example' => '<meta name="google-site-verification" content="NbwaW3WIDp_SPsSsfl78Ive7F34-znm9lxLJXjuWNGE" />',
  ];
  $engines['yahoo'] = [
    'name' => t('Yahoo!'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'meta' => TRUE,
  ];
  $engines['bing'] = [
    'name' => t('Bing'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'meta' => TRUE,
  ];
  $engines['yandex'] = [
    'name' => t('Yandex'),
    'file' => TRUE,
    'file_example' => 'yandex_b5741169901f6c20.txt',
    'meta' => TRUE,
    'meta_example' => '<meta name="yandex-verification" content="b5741169901f6c20" />',
  ];
  $engines['custom'] = [
    'name' => t('Custom verification'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'meta' => TRUE,
  ];
  return $engines;
}

/**
 * Implements hook_page_attachments().
 *
 * Add a meta tag to the HEAD.
 */
function site_verify_page_attachments(array &$page) {
  if (\Drupal::service('path.matcher')
    ->isFrontPage()) {
    $meta_tags = \Drupal::database()
      ->select('site_verify', 'site_verify')
      ->fields('site_verify', [
      'svid',
      'meta',
    ])
      ->condition('meta', '', '<>')
      ->execute()
      ->fetchAllKeyed();
    foreach ($meta_tags as $svid => $meta_tag) {
      preg_match('/name="(.*)" content/', $meta_tag, $name);
      preg_match('/content="(.*)"/', $meta_tag, $content);
      $data = [
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => [
          'name' => $name[1],
          'content' => $content[1],
        ],
      ];
      $page['#attached']['html_head'][] = [
        $data,
        'site_verify:' . $svid,
      ];
    }
  }
}

Functions

Namesort descending Description
site_verify_page_attachments Implements hook_page_attachments().
site_verify_site_verify_engine_info Implements hook_site_verify_engine_info().