You are here

message_subscribe_test.module in Message Subscribe 8

Test implementations of message_subscribe hooks.

File

tests/modules/message_subscribe_test/message_subscribe_test.module
View source
<?php

/**
 * @file
 * Test implementations of message_subscribe hooks.
 */
use Drupal\message\MessageInterface;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;
use Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface;

/**
 * Implements hook_message_subscribe_get_subscribers().
 */
function message_subscribe_test_message_subscribe_get_subscribers(MessageInterface $message, $subscribe_options = [], $context = []) {

  // Set state variable.
  \Drupal::state('message_subscribe_test')
    ->set('hook_called', TRUE);

  // Add user 4.
  return [
    4 => new DeliveryCandidate([
      'foo_flag',
    ], [
      'email',
    ], 4),
  ];
}

/**
 * Implements hook_message_subscribe_test_message_subscribe_get_subscribers_alter().
 */
function message_subscribe_test_message_subscribe_get_subscribers_alter(array &$uids, array $values) {

  // Set state variable.
  \Drupal::state('message_subscribe_test')
    ->set('alter_hook_called', TRUE);
  if (!\Drupal::state('message_subscribe_test')
    ->get('disable_subscribers_alter', FALSE)) {
    $uids[10001] = new DeliveryCandidate([
      'bar_flag',
    ], [
      'email',
    ], 10001);

    // Remove user 2.
    unset($uids[2]);
  }
}

/**
 * Implements hook_message_subscribe_message_alter().
 */
function message_subscribe_test_message_subscribe_message_alter(MessageInterface $message, DeliveryCandidateInterface $delivery_candidate) {
  $calls = \Drupal::state('message_subscribe_test')
    ->get('message_alter_hook_called', 0);
  \Drupal::state('message_subscribe_test')
    ->set('message_alter_hook_called', $calls + 1);
}