You are here

public function MessageCheckAndDeleteWorker::processItem in Message 8

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

src/Plugin/QueueWorker/MessageCheckAndDeleteWorker.php, line 67

Class

MessageCheckAndDeleteWorker
Deletes messages that no longer have references within multivalued fields.

Namespace

Drupal\message\Plugin\QueueWorker

Code

public function processItem($data) {

  // $data is expected to be an array where the keys are message IDs and the
  // values are arrays of multi-valued entity reference field names to check.
  if (!empty($data)) {

    // Check messages with multiple cardinality references; Only delete such
    // messages if the entity being deleted is the last one referenced by the
    // message.
    $messages = $this->messageStorage
      ->loadMultiple(array_keys($data));
    foreach ($data as $id => $fields) {
      foreach ($fields as $field_name) {
        if (isset($messages[$id])) {
          $message = $messages[$id];
          if (count($message
            ->get($field_name)
            ->referencedEntities()) === 0) {
            $this->messageStorage
              ->delete([
              $message,
            ]);

            // As soon as one field qualifies, we can delete the entity. No
            // need to check the other fields.
            break;
          }
        }
      }
    }
  }
}