You are here

class ReadonlymodeManager in Read only mode 2.0.x

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.

Hierarchy

  • class \Drupal\readonlymode\ReadonlymodeManager implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ReadonlymodeManager

1 file declares its use of ReadonlymodeManager
ReadonlymodeCommands.php in src/Commands/ReadonlymodeCommands.php
1 string reference to 'ReadonlymodeManager'
readonlymode.services.yml in ./readonlymode.services.yml
readonlymode.services.yml
1 service uses ReadonlymodeManager
readonlymode.manager in ./readonlymode.services.yml
Drupal\readonlymode\ReadonlymodeManager

File

src/ReadonlymodeManager.php, line 24

Namespace

Drupal\readonlymode
View source
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());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReadonlymodeManager::$account protected property The current user.
ReadonlymodeManager::$config protected property The modules configuration.
ReadonlymodeManager::$messenger protected property The messenger service.
ReadonlymodeManager::$state protected property The drupal state.
ReadonlymodeManager::$token protected property The token replacement service.
ReadonlymodeManager::getErrorMessage public function Get the error message.
ReadonlymodeManager::getSubscribedEvents public static function
ReadonlymodeManager::getWarningMessage public function Get the warning message.
ReadonlymodeManager::isReadonly public function Is the read-only mode active.
ReadonlymodeManager::onRequest public function Set the readonly mode message on every request.
ReadonlymodeManager::setReadonly public function Set the new read-only mode.
ReadonlymodeManager::__construct public function ReadonlymodeManager constructor.