You are here

automatic_updates.install in Automatic Updates 8

Same filename and directory in other branches
  1. 8.2 automatic_updates.install
  2. 7 automatic_updates.install

Automatic updates install file.

File

automatic_updates.install
View source
<?php

/**
 * @file
 * Automatic updates install file.
 */
use Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\Url;

/**
 * Implements hook_requirements().
 */
function automatic_updates_requirements($phase) {

  // Mimic the functionality of the vendor checker procedurally since class
  // loading isn't available pre module install.
  $vendor_autoloader = [
    DRUPAL_ROOT,
    'vendor',
    'autoload.php',
  ];
  if (!file_exists(implode(DIRECTORY_SEPARATOR, $vendor_autoloader))) {
    return [
      'not installable' => [
        'title' => t('Automatic Updates'),
        'severity' => REQUIREMENT_ERROR,
        'value' => '1.x',
        'description' => t('This module does not currently support relocated vendor folder and composer-based workflows.'),
      ],
    ];
  }
  if ($phase !== 'runtime') {
    return NULL;
  }
  $requirements = [];
  _automatic_updates_checker_requirements($requirements);
  _automatic_updates_psa_requirements($requirements);
  return $requirements;
}

/**
 * Display requirements from results of readiness checker.
 *
 * @param array $requirements
 *   The requirements array.
 */
function _automatic_updates_checker_requirements(array &$requirements) {

  /** @var \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface $checker */
  $checker = \Drupal::service('automatic_updates.readiness_checker');
  if (!$checker
    ->isEnabled()) {
    return;
  }
  $last_check_timestamp = $checker
    ->timestamp();
  $requirements['automatic_updates_readiness'] = [
    'title' => t('Update readiness checks'),
    'severity' => REQUIREMENT_OK,
    'value' => t('Your site is ready to for <a href="@readiness_checks">automatic updates</a>.', [
      '@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks',
    ]),
  ];
  $error_results = $checker
    ->getResults(ReadinessCheckerManagerInterface::ERROR);
  $warning_results = $checker
    ->getResults(ReadinessCheckerManagerInterface::WARNING);
  $checker_results = array_merge($error_results, $warning_results);
  if (!empty($checker_results)) {
    $requirements['automatic_updates_readiness']['severity'] = $error_results ? REQUIREMENT_ERROR : REQUIREMENT_WARNING;
    $requirements['automatic_updates_readiness']['value'] = new PluralTranslatableMarkup(count($checker_results), '@count check failed:', '@count checks failed:');
    $requirements['automatic_updates_readiness']['description'] = [
      '#theme' => 'item_list',
      '#items' => $checker_results,
    ];
  }
  if (\Drupal::time()
    ->getRequestTime() > $last_check_timestamp + ReadinessCheckerManagerInterface::LAST_CHECKED_WARNING) {
    $requirements['automatic_updates_readiness']['severity'] = REQUIREMENT_ERROR;
    $requirements['automatic_updates_readiness']['value'] = t('Your site has not recently checked if it is ready to apply <a href="@readiness_checks">automatic updates</a>.', [
      '@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks',
    ]);
    $readiness_check = Url::fromRoute('automatic_updates.update_readiness');
    $time_ago = \Drupal::service('date.formatter')
      ->formatTimeDiffSince($last_check_timestamp);
    if ($last_check_timestamp === 0) {
      $requirements['automatic_updates_readiness']['description'] = t('<a href="@link">Run readiness checks</a> manually.', [
        '@link' => $readiness_check
          ->toString(),
      ]);
    }
    elseif ($readiness_check
      ->access()) {
      $requirements['automatic_updates_readiness']['description'] = t('Last run @time ago. <a href="@link">Run readiness checks</a> manually.', [
        '@time' => $time_ago,
        '@link' => $readiness_check
          ->toString(),
      ]);
    }
    else {
      $requirements['automatic_updates_readiness']['description'] = t('Readiness checks were last run @time ago.', [
        '@time' => $time_ago,
      ]);
    }
  }
}

/**
 * Display requirements from Public service announcements.
 *
 * @param array $requirements
 *   The requirements array.
 */
function _automatic_updates_psa_requirements(array &$requirements) {
  if (!\Drupal::config('automatic_updates.settings')
    ->get('enable_psa')) {
    return;
  }

  /** @var \Drupal\automatic_updates\Services\AutomaticUpdatesPsa $psa */
  $psa = \Drupal::service('automatic_updates.psa');
  $messages = $psa
    ->getPublicServiceMessages();
  $requirements['automatic_updates_psa'] = [
    'title' => t('<a href="@link">Public service announcements</a>', [
      '@link' => 'https://www.drupal.org/docs/8/update/automatic-updates#psas',
    ]),
    'severity' => REQUIREMENT_OK,
    'value' => t('No announcements requiring attention.'),
  ];
  if (!empty($messages)) {
    $requirements['automatic_updates_psa']['severity'] = REQUIREMENT_ERROR;
    $requirements['automatic_updates_psa']['value'] = new PluralTranslatableMarkup(count($messages), '@count urgent announcement requires your attention:', '@count urgent announcements require your attention:');
    $requirements['automatic_updates_psa']['description'] = [
      '#theme' => 'item_list',
      '#items' => $messages,
    ];
  }
}

Functions

Namesort descending Description
automatic_updates_requirements Implements hook_requirements().
_automatic_updates_checker_requirements Display requirements from results of readiness checker.
_automatic_updates_psa_requirements Display requirements from Public service announcements.