UpdateMessageTrait.php

Namespace

Drupal\update

File

core/modules/update/src/UpdateMessageTrait.php

View source
<?php

namespace Drupal\update;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

/**
 * Provides helpers for update messages.
 */
trait UpdateMessageTrait {
  
  /**
   * Returns a warning message when there is no data about available updates.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   Translatable markup.
   */
  protected function noData() : TranslatableMarkup {
    $destination = \Drupal::destination()->getAsArray();
    return t('No update information available. <a href=":run_cron">Run cron</a> or <a href=":check_manually">check manually</a>.', [
      ':run_cron' => Url::fromRoute('system.run_cron', [], [
        'query' => $destination,
      ])->toString(),
      ':check_manually' => Url::fromRoute('update.manual_status', [], [
        'query' => $destination,
      ])->toString(),
    ]);
  }
  
  /**
   * Returns the appropriate message text when site is out of date or insecure.
   *
   * These error messages are shared by both update_requirements() for the
   * site-wide status report at admin/reports/status and in the body of the
   * notification email messages generated by update_cron().
   *
   * @param string $msg_type
   *   String to indicate what kind of message to generate. Can be either 'core'
   *   or 'contrib'.
   * @param int $msg_reason
   *   Integer constant specifying why message is generated.
   * @param string $langcode
   *   (optional) A language code to use. Defaults to NULL.
   * @param bool $is_email
   *   (optional) Controls if this function is used for generating mail. If
   *   TRUE, URLs will be absolute and will be included without access checks.
   *   If FALSE, links will be relative and will only be included if the
   *   current user can access them. Defaults to FALSE.
   *
   * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
   *   The properly translated error message for the given key.
   */
  protected function getText(string $msg_type, int $msg_reason, ?string $langcode = NULL, bool $is_email = FALSE) : string|TranslatableMarkup {
    $text = '';
    switch ($msg_reason) {
      case UpdateManagerInterface::NOT_SECURE:
        if ($msg_type == 'core') {
          $text = t('There is a security update available for your version of Drupal. To ensure the security of your server, you should update immediately!', [], [
            'langcode' => $langcode,
          ]);
        }
        else {
          $text = t('There are security updates available for one or more of your modules or themes. To ensure the security of your server, you should update immediately!', [], [
            'langcode' => $langcode,
          ]);
        }
        break;

      case UpdateManagerInterface::REVOKED:
        if ($msg_type == 'core') {
          $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', [], [
            'langcode' => $langcode,
          ]);
        }
        else {
          $text = t('The installed version of at least one of your modules or themes has been revoked and is no longer available for download. Upgrading or uninstalling is strongly recommended!', [], [
            'langcode' => $langcode,
          ]);
        }
        break;

      case UpdateManagerInterface::NOT_SUPPORTED:
        if ($msg_type == 'core') {
          $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', [], [
            'langcode' => $langcode,
          ]);
        }
        else {
          $text = t('The installed version of at least one of your modules or themes is no longer supported. Upgrading or uninstalling is strongly recommended. See the project homepage for more details.', [], [
            'langcode' => $langcode,
          ]);
        }
        break;

      case UpdateManagerInterface::NOT_CURRENT:
        if ($msg_type == 'core') {
          $text = t('There are updates available for your version of Drupal. To ensure the proper functioning of your site, you should update as soon as possible.', [], [
            'langcode' => $langcode,
          ]);
        }
        else {
          $text = t('There are updates available for one or more of your modules or themes. To ensure the proper functioning of your site, you should update as soon as possible.', [], [
            'langcode' => $langcode,
          ]);
        }
        break;

      case UpdateFetcherInterface::UNKNOWN:
      case UpdateFetcherInterface::NOT_CHECKED:
      case UpdateFetcherInterface::NOT_FETCHED:
      case UpdateFetcherInterface::FETCH_PENDING:
        $url = Url::fromRoute('update.status', [], [
          'absolute' => $is_email,
          'language' => $langcode,
        ]);
        if ($msg_type == 'core') {
          if ($url->access() || $is_email) {
            return t('There was a problem checking <a href=":update-report">available updates</a> for Drupal.', [
              ':update-report' => $url->toString(),
            ], [
              'langcode' => $langcode,
            ]);
          }
          return t('There was a problem checking available updates for Drupal.', [], [
            'langcode' => $langcode,
          ]);
        }
        else {
          if ($url->access() || $is_email) {
            return t('There was a problem checking <a href=":update-report">available updates</a> for your modules or themes.', [
              ':update-report' => $url->toString(),
            ], [
              'langcode' => $langcode,
            ]);
          }
          return t('There was a problem checking available updates for Drupal.', [], [
            'langcode' => $langcode,
          ]);
        }
        break;

    }
    return $text;
  }

}

Traits

Title Deprecated Summary
UpdateMessageTrait Provides helpers for update messages.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.