final class AdminReadinessMessages in Automatic Updates 8.2
Class for displaying readiness messages on admin pages.
@internal This class implements logic to output the messages from readiness checkers on admin pages. It should not be called directly.
Hierarchy
- class \Drupal\automatic_updates\Validation\AdminReadinessMessages implements ContainerInjectionInterface uses ReadinessTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
Expanded class hierarchy of AdminReadinessMessages
1 file declares its use of AdminReadinessMessages
- automatic_updates.module in ./
automatic_updates.module - Contains hook implementations for Automatic Updates.
File
- src/
Validation/ AdminReadinessMessages.php, line 25
Namespace
Drupal\automatic_updates\ValidationView source
final class AdminReadinessMessages implements ContainerInjectionInterface {
use MessengerTrait;
use StringTranslationTrait;
use RedirectDestinationTrait;
use ReadinessTrait;
/**
* The readiness checker manager.
*
* @var \Drupal\automatic_updates\Validation\ReadinessValidationManager
*/
protected $readinessCheckerManager;
/**
* The admin context service.
*
* @var \Drupal\Core\Routing\AdminContext
*/
protected $adminContext;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected $currentRouteMatch;
/**
* Constructs a ReadinessRequirement object.
*
* @param \Drupal\automatic_updates\Validation\ReadinessValidationManager $readiness_checker_manager
* The readiness checker manager service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Routing\AdminContext $admin_context
* The admin context service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The translation service.
* @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
* The current route match.
*/
public function __construct(ReadinessValidationManager $readiness_checker_manager, MessengerInterface $messenger, AdminContext $admin_context, AccountProxyInterface $current_user, TranslationInterface $translation, CurrentRouteMatch $current_route_match) {
$this->readinessCheckerManager = $readiness_checker_manager;
$this
->setMessenger($messenger);
$this->adminContext = $admin_context;
$this->currentUser = $current_user;
$this
->setStringTranslation($translation);
$this->currentRouteMatch = $current_route_match;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) : self {
return new static($container
->get('automatic_updates.readiness_validation_manager'), $container
->get('messenger'), $container
->get('router.admin_context'), $container
->get('current_user'), $container
->get('string_translation'), $container
->get('current_route_match'));
}
/**
* Displays the checker results messages on admin pages.
*/
public function displayAdminPageMessages() : void {
if (!$this
->displayResultsOnCurrentPage()) {
return;
}
if ($this->readinessCheckerManager
->getResults() === NULL) {
$checker_url = Url::fromRoute('automatic_updates.update_readiness')
->setOption('query', $this
->getDestinationArray());
if ($checker_url
->access()) {
$this
->messenger()
->addError($this
->t('Your site has not recently run an update readiness check. <a href=":url">Run readiness checks now.</a>', [
':url' => $checker_url
->toString(),
]));
}
}
else {
// Display errors, if there are any. If there aren't, then display
// warnings, if there are any.
if (!$this
->displayResultsForSeverity(SystemManager::REQUIREMENT_ERROR)) {
$this
->displayResultsForSeverity(SystemManager::REQUIREMENT_WARNING);
}
}
}
/**
* Determines whether the messages should be displayed on the current page.
*
* @return bool
* Whether the messages should be displayed on the current page.
*/
protected function displayResultsOnCurrentPage() : bool {
if ($this->adminContext
->isAdminRoute() && $this->currentUser
->hasPermission('administer site configuration')) {
// These routes don't need additional nagging.
$disabled_routes = [
'update.theme_update',
'system.theme_install',
'update.module_update',
'update.module_install',
'update.status',
'update.report_update',
'update.report_install',
'update.settings',
'system.status',
'update.confirmation_page',
];
return !in_array($this->currentRouteMatch
->getRouteName(), $disabled_routes, TRUE);
}
return FALSE;
}
/**
* Displays the results for severity.
*
* @param int $severity
* The severity for the results to display. Should be one of the
* SystemManager::REQUIREMENT_* constants.
*
* @return bool
* Whether any results were displayed.
*/
protected function displayResultsForSeverity(int $severity) : bool {
$results = $this->readinessCheckerManager
->getResults($severity);
if (empty($results)) {
return FALSE;
}
$failure_message = $this
->getFailureMessageForSeverity($severity);
if ($severity === SystemManager::REQUIREMENT_ERROR) {
$this
->messenger()
->addError($failure_message);
}
else {
$this
->messenger()
->addWarning($failure_message);
}
foreach ($results as $result) {
$messages = $result
->getMessages();
$message = count($messages) === 1 ? $messages[0] : $result
->getSummary();
if ($severity === SystemManager::REQUIREMENT_ERROR) {
$this
->messenger()
->addError($message);
}
else {
$this
->messenger()
->addWarning($message);
}
}
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdminReadinessMessages:: |
protected | property | The admin context service. | |
AdminReadinessMessages:: |
protected | property | The current route match. | |
AdminReadinessMessages:: |
protected | property | The current user. | |
AdminReadinessMessages:: |
protected | property | The readiness checker manager. | |
AdminReadinessMessages:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
AdminReadinessMessages:: |
public | function | Displays the checker results messages on admin pages. | |
AdminReadinessMessages:: |
protected | function | Displays the results for severity. | |
AdminReadinessMessages:: |
protected | function | Determines whether the messages should be displayed on the current page. | |
AdminReadinessMessages:: |
public | function | Constructs a ReadinessRequirement object. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
ReadinessTrait:: |
protected | function | Gets a message, based on severity, when readiness checkers fail. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |