ReadonlymodeManager.php in Read only mode 2.0.x
Namespace
Drupal\readonlymodeFile
src/ReadonlymodeManager.phpView source
<?php
namespace Drupal\readonlymode;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Utility\Token;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* The manager class for the read only mode.
*
* This is the central access to this modules functionality. But this is not an
* API yet. We reserve the right to change method signatures, constructor
* arguments and everything else. That said if other modules would like to
* interact with this, please open a issue on drupal.org and we can consider
* adding an interface and make this the API for this module.
*
* @internal this is not an API yet, open an issue on drupal.org.
*/
class ReadonlymodeManager implements EventSubscriberInterface {
/**
* The drupal state.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The token replacement service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* The modules configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* ReadonlymodeManager constructor.
*
* @param \Drupal\Core\State\StateInterface $state
* The drupal state.
* @param \Drupal\Core\Utility\Token $token
* The token replacement service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
*/
public function __construct(StateInterface $state, Token $token, ConfigFactoryInterface $configFactory, MessengerInterface $messenger, AccountInterface $account) {
$this->state = $state;
$this->token = $token;
$this->config = $configFactory
->get('readonlymode.settings');
$this->messenger = $messenger;
$this->account = $account;
}
/**
* Is the read-only mode active.
*
* @return bool
* True if active.
*/
public function isReadonly() : bool {
return (bool) $this->state
->get('readonlymode_active', FALSE);
}
/**
* Set the new read-only mode.
*
* @param bool $active
* The new read-only maintenance mode state.
*/
public function setReadonly(bool $active) : void {
$this->state
->set('readonlymode_active', $active);
}
/**
* Get the warning message.
*
* @return string
* The message.
*/
public function getWarningMessage() : string {
return (string) $this->token
->replace($this->config
->get('messages.default'));
}
/**
* Get the error message.
*
* @return string
* The error message
*/
public function getErrorMessage() : string {
// We could take the entity as an argument and pass it on.
return (string) $this->token
->replace($this->config
->get('messages.not_saved'));
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => 'onRequest',
];
}
/**
* Set the readonly mode message on every request.
*/
public function onRequest() {
if ($this
->isReadonly() && $this->account
->hasPermission('see readonlymode warning')) {
$this->messenger
->addWarning($this
->getWarningMessage());
}
}
}
Classes
Name![]() |
Description |
---|---|
ReadonlymodeManager | The manager class for the read only mode. |