You are here

class MessageCheckAndDeleteWorker in Message 8

Deletes messages that no longer have references within multivalued fields.

When an entity is deleted, messages may be removed that reference that entity. In the case of single-valued fields, it's easy to verify this. However, for multi-valued fields, we have to first check if there are references to any other entities in that field. Only when the last reference is removed should we delete the message. This worker covers this more complex scenario.

Plugin annotation


@QueueWorker(
  id = "message_check_delete",
  title = @Translation("Delete messages if an entity is referenced"),
  cron = {"time" = 10}
)

Hierarchy

Expanded class hierarchy of MessageCheckAndDeleteWorker

File

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

Namespace

Drupal\message\Plugin\QueueWorker
View source
class MessageCheckAndDeleteWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * The message storage handler.
   *
   * @var \Drupal\Core\Entity\ContentEntityStorageInterface
   */
  protected $messageStorage;

  /**
   * Constructs a new MessageCheckAndDeleteWorker object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->messageStorage = $entity_type_manager
      ->getStorage('message');
  }

  /**
   * {@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'));
  }

  /**
   * {@inheritdoc}
   */
  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;
            }
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessageCheckAndDeleteWorker::$messageStorage protected property The message storage handler.
MessageCheckAndDeleteWorker::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MessageCheckAndDeleteWorker::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
MessageCheckAndDeleteWorker::__construct public function Constructs a new MessageCheckAndDeleteWorker object. Overrides PluginBase::__construct
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.