You are here

adstxt.install in AdsTxt 8

Same filename and directory in other branches
  1. 7 adstxt.install

Install, update and uninstall functions for the adstxt module.

File

adstxt.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the adstxt module.
 */
use Drupal\Core\Routing\RequestHelper;

/**
 * Implements hook_install().
 */
function adstxt_install() {
  $content = '';

  // List of candidates for import.
  $files = [
    DRUPAL_ROOT . '/ads.txt',
    DRUPAL_ROOT . '/sites/default/default.ads.txt',
    drupal_get_path('module', 'adstxt') . '/ads.txt',
  ];
  foreach ($files as $file) {
    if (file_exists($file) && is_readable($file)) {
      $content = file_get_contents($file);
      break;
    }
  }
  \Drupal::configFactory()
    ->getEditable('adstxt.settings')
    ->set('content', $content)
    ->save();
  $app_content = '';

  // List of candidates for import.
  $files = [
    DRUPAL_ROOT . '/app-ads.txt',
    DRUPAL_ROOT . '/sites/default/default.app-ads.txt',
    drupal_get_path('module', 'adstxt') . '/app-ads.txt',
  ];
  foreach ($files as $file) {
    if (file_exists($file) && is_readable($file)) {
      $app_content = file_get_contents($file);
      break;
    }
  }
  \Drupal::configFactory()
    ->getEditable('adstxt.settings')
    ->set('app_content', $app_content)
    ->save();
}

/**
 * Implements hook_requirements().
 */
function adstxt_requirements($phase) {
  $requirements = [];
  switch ($phase) {
    case 'runtime':

      // Module cannot work without Clean URLs.
      $request = \Drupal::request();
      if (!RequestHelper::isCleanUrl($request)) {
        $requirements['adstxt_cleanurl'] = [
          'title' => t('Ads.txt'),
          'severity' => REQUIREMENT_ERROR,
          'value' => t('Not enabled.'),
          'description' => t('Clean URLs are mandatory for this module.'),
        ];
      }

      // Webservers prefer the ads.txt file on disk and does not allow menu
      // path overwrite.
      if (file_exists(DRUPAL_ROOT . '/ads.txt')) {
        $requirements['adstxt_file'] = [
          'title' => t('Ads.txt'),
          'severity' => REQUIREMENT_WARNING,
          'value' => t('ads.txt exists.'),
          'description' => t('Please remove the existing ads.txt file in the docroot.'),
        ];
      }
  }
  return $requirements;
}

Functions

Namesort descending Description
adstxt_install Implements hook_install().
adstxt_requirements Implements hook_requirements().