You are here

public function MessagePurgeOrchestrator::purgeAllTemplateMessages in Message 8

Purgers all messages for all templates as configured.

Messages set to ignore global purge settings will use their own configuration, otherwise global settings will be used.

File

src/MessagePurgeOrchestrator.php, line 56

Class

MessagePurgeOrchestrator
Purges messages from the system based on global and template configurations.

Namespace

Drupal\message

Code

public function purgeAllTemplateMessages() {

  // Names of non global-purge-settings overriding message templates.

  /** @var \Drupal\message\MessageTemplateInterface[] $no_override_templates */
  $no_override_templates = [];

  // Message templates that override global purge settings.

  /** @var \Drupal\message\MessageTemplateInterface[] $override_templates */
  $override_templates = [];

  // Iterate all message templates to distinguish between overriding and non-
  // overriding templates.

  /** @var \Drupal\message\MessageTemplateInterface[] $message_templates */
  $message_templates = $this->entityTypeManager
    ->getStorage('message_template')
    ->loadMultiple();
  foreach ($message_templates as $message_template) {
    if (!$message_template
      ->getSetting('purge_override')) {
      $no_override_templates[] = $message_template;
    }
    else {
      $override_templates[] = $message_template;
    }
  }

  // Purge messages of templates overriding the global settings.
  foreach ($override_templates as $message_template) {
    $settings = $message_template
      ->getSetting('purge_methods');
    if (empty($settings)) {

      // Ignore messages that have no enabled purge methods.
      continue;
    }
    $this
      ->purgeMessagesByTemplate($message_template, $settings);
  }

  // Purge messages for templates that are not overriding global settings.
  if (!empty($no_override_templates)) {

    // Only purge if globally enabled.
    if ($this->globalConfig
      ->get('purge_enable')) {
      foreach ($no_override_templates as $message_template) {
        $this
          ->purgeMessagesByTemplate($message_template, $this->globalConfig
          ->get('purge_methods'));
      }
    }
  }
}