You are here

public function PrivateMessageThread::filterNewestMessages in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Entity/PrivateMessageThread.php \Drupal\private_message\Entity\PrivateMessageThread::filterNewestMessages()

Filter the list down to only the newest messages.

Note that other messages will be loadable through AJAX.

Overrides PrivateMessageThreadInterface::filterNewestMessages

File

src/Entity/PrivateMessageThread.php, line 111

Class

PrivateMessageThread
Defines the Private Message Thread entity.

Namespace

Drupal\private_message\Entity

Code

public function filterNewestMessages() {
  $messages = $this
    ->getMessages();
  if (count($messages) > \Drupal::config('private_message_thread.settings')
    ->get('message_count')) {
    $list = $this
      ->get('private_messages');
    $filtered_messages = array_slice($messages, -1 * \Drupal::config('private_message_thread.settings')
      ->get('message_count'));
    $first_message = array_shift($filtered_messages);
    $first_key = $first_message
      ->id();
    foreach ($list
      ->referencedEntities() as $list_item) {
      if ($list_item
        ->id() < $first_key) {
        $list
          ->removeItem(0);
      }
      else {
        break;
      }
    }
  }
}