You are here

class Days in Message 8

Delete messages older than certain days.

Plugin annotation


@MessagePurge(
  id = "days",
  label = @Translation("Days", context = "MessagePurge"),
  description = @Translation("Delete messages older than a given amount of days."),
)

Hierarchy

Expanded class hierarchy of Days

1 file declares its use of Days
DaysTest.php in tests/src/Unit/Plugin/MessagePurge/DaysTest.php
1 string reference to 'Days'
message.schema.yml in config/schema/message.schema.yml
config/schema/message.schema.yml

File

src/Plugin/MessagePurge/Days.php, line 23

Namespace

Drupal\message\Plugin\MessagePurge
View source
class Days extends MessagePurgeBase {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Days constructor.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Entity\Query\QueryInterface $message_query
   *   The entity query service.
   * @param \Drupal\Core\Queue\QueueInterface $queue
   *   The message deletion queue.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack used to determine the current time.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryInterface $message_query, QueueInterface $queue, RequestStack $request_stack) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $message_query, $queue);
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager')
      ->getStorage('message')
      ->getQuery(), $container
      ->get('queue')
      ->get('message_delete'), $container
      ->get('request_stack'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['days'] = [
      '#type' => 'number',
      '#min' => 1,
      '#title' => $this
        ->t('Messages older than'),
      '#description' => $this
        ->t('Maximal message age in days.'),
      '#default_value' => $this->configuration['days'],
      '#tree' => FALSE,
    ];
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['days'] = $form_state
      ->getValue('days');
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'days' => 30,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function fetch(MessageTemplateInterface $template) {
    $query = $this
      ->baseQuery($template);

    // Find messages older than the current time minus the maximum age.
    $earlier_than = $this->requestStack
      ->getCurrentRequest()->server
      ->get('REQUEST_TIME') - $this->configuration['days'] * 86400;
    $result = $query
      ->condition('created', $earlier_than, '<')
      ->execute();
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Days::$requestStack protected property The request stack.
Days::buildConfigurationForm public function Form constructor. Overrides MessagePurgeBase::buildConfigurationForm
Days::create public static function Creates an instance of the plugin. Overrides MessagePurgeBase::create
Days::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
Days::fetch public function Fetch the messages that need to be purged for a given template. Overrides MessagePurgeInterface::fetch
Days::submitConfigurationForm public function Form submission handler. Overrides MessagePurgeBase::submitConfigurationForm
Days::__construct public function Days constructor. Overrides MessagePurgeBase::__construct
MessagePurgeBase::$messageQuery protected property The entity query object for Message items.
MessagePurgeBase::$queue protected property The message deletion queue.
MessagePurgeBase::$weight protected property The weight of the purge plugin.
MessagePurgeBase::baseQuery protected function Get a base query.
MessagePurgeBase::description public function Returns the purge method description. Overrides MessagePurgeInterface::description
MessagePurgeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
MessagePurgeBase::getWeight public function Returns the weight of the purge plugin. Overrides MessagePurgeInterface::getWeight
MessagePurgeBase::label public function Returns the purge method label. Overrides MessagePurgeInterface::label
MessagePurgeBase::process public function Process the purgeable messages. Overrides MessagePurgeInterface::process
MessagePurgeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
MessagePurgeBase::setWeight public function Sets the weight for this purge plugin. Overrides MessagePurgeInterface::setWeight
MessagePurgeBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
MessagePurgeInterface::MESSAGE_DELETE_SIZE constant The maximum number of messages that a queue worker should delete at once.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.