deleteByType example

/** * Tests setting messages and removing one before it is displayed. * * @return string * Empty string, we just test the setting of messages. */
  public function messengerServiceTest() {
    // Set two messages.     $this->messenger->addStatus('First message (removed).');
    $this->messenger->addStatus($this->t('Second message with <em>markup!</em> (not removed).'));
    $messages = $this->messenger->deleteByType('status');
    // Remove the first.     unset($messages[0]);

    foreach ($messages as $message) {
      $this->messenger->addStatus($message);
    }

    // Duplicate message check.     $this->messenger->addStatus('Non Duplicated message');
    $this->messenger->addStatus('Non Duplicated message');

    

  public static function renderMessages($type = NULL) {
    $render = [];
    if (isset($type)) {
      $messages = [
        $type => \Drupal::messenger()->deleteByType($type),
      ];
    }
    else {
      $messages = \Drupal::messenger()->deleteAll();
    }

    if ($messages) {
      // Render the messages.       $render = [
        '#theme' => 'status_messages',
        '#message_list' => $messages,
        
/** * @covers ::addStatus * @covers ::deleteByType * @covers ::messagesByType */
  public function testRemoveSingleMessage() {

    // Set two messages.     $this->messenger->addStatus('First message (removed).');
    $this->messenger->addStatus('Second message with <em>markup!</em> (not removed).');
    $messages = $this->messenger->deleteByType(MessengerInterface::TYPE_STATUS);
    // Remove the first.     unset($messages[0]);

    // Re-add the second.     foreach ($messages as $message) {
      $this->messenger->addStatus($message);
    }

    // Check we only have the second one.     $this->assertCount(1, $this->messenger->messagesByType(MessengerInterface::TYPE_STATUS));
    $this->assertContainsEquals('Second message with <em>markup!</em> (not removed).', $this->messenger->deleteByType(MessengerInterface::TYPE_STATUS));

  }
Home | Imprint | This part of the site doesn't use cookies.