diff --git a/message_subscribe.module b/message_subscribe.module index c325d50..0d92d5e 100755 --- a/message_subscribe.module +++ b/message_subscribe.module @@ -6,6 +6,7 @@ */ use Drupal\message\MessageInterface; +use Drupal\message_subscribe\Subscribers\DeliveryCandidate; /** * Implements hook_message_subscribe_get_subscribers(). @@ -57,9 +58,11 @@ function message_subscribe_message_subscribe_get_subscribers(MessageInterface $m foreach ($result as $row) { - $uids[$row->uid] = !empty($uids[$row->uid]) ? $uids[$row->uid] : ['notifiers' => []]; + $uids[$row->uid] = !empty($uids[$row->uid]) ? $uids[$row->uid] : new DeliveryCandidate([], [], $row->uid); // Register the flag name. - $uids[$row->uid]['flags'][] = $row->flag_id; + $flags = $uids[$row->uid]->getFlags(); + $flags[] = $row->flag_id; + $uids[$row->uid]->setFlags(array_unique($flags)); if ($range) { --$range; diff --git a/src/Subscribers.php b/src/Subscribers.php index 6d2cdb1..e5e990e 100644 --- a/src/Subscribers.php +++ b/src/Subscribers.php @@ -184,6 +184,7 @@ public function sendMessage(EntityInterface $entity, MessageInterface $message, foreach ($uids as $uid => $delivery_candidate) { // Array usage is deprecated, but supported until 2.0. if (!$delivery_candidate instanceof DeliveryCandidateInterface) { + trigger_error('Usage of arrays in subscriber information is deprecated and will be removed in Message Subscribe 2.0. Use a DeliveryCandidate object instead.', E_USER_DEPRECATED); $delivery_candidate += ['notifiers' => []]; $delivery_candidate = new DeliveryCandidate($delivery_candidate['flags'], $delivery_candidate['notifiers'], $uid); } diff --git a/tests/modules/message_subscribe_test/message_subscribe_test.module b/tests/modules/message_subscribe_test/message_subscribe_test.module index 6513225..47d883b 100644 --- a/tests/modules/message_subscribe_test/message_subscribe_test.module +++ b/tests/modules/message_subscribe_test/message_subscribe_test.module @@ -30,12 +30,18 @@ function message_subscribe_test_message_subscribe_get_subscribers_alter(array &$ \Drupal::state('message_subscribe_test')->set('alter_hook_called', TRUE); if (!\Drupal::state('message_subscribe_test')->get('disable_subscribers_alter', FALSE)) { - // Add non-existent user 10001. Using an array here until array support - // is removed in 2.0. - $uids[10001] = [ - 'flags' => ['bar_flag'], - 'notifiers' => ['email'], - ]; + // Add non-existent user 10001. + if (\Drupal::state('message_subscribe_test')->get('use_array', FALSE)) { + // Test to ensure array usage still works (it will throw a deprecated + // notice). + $uids[10001] = [ + 'flags' => ['bar_flag'], + 'notifiers' => ['email'], + ]; + } + else { + $uids[10001] = new DeliveryCandidate(['bar_flag'], ['email'], 10001); + } // Remove user 2. unset($uids[2]); diff --git a/tests/src/Kernel/SubscribersTest.php b/tests/src/Kernel/SubscribersTest.php index 012292e..6bc3534 100644 --- a/tests/src/Kernel/SubscribersTest.php +++ b/tests/src/Kernel/SubscribersTest.php @@ -313,10 +313,7 @@ public function testHooks() { $this->assertTrue(\Drupal::state('message_subscribe_test')->get('alter_hook_called')); $this->assertEquals([ 4 => new DeliveryCandidate(['foo_flag'], ['email'], 4), - 10001 => [ - 'flags' => ['bar_flag'], - 'notifiers' => ['email'], - ], + 10001 => new DeliveryCandidate(['bar_flag'], ['email'], 10001), ], $uids); // Disable the test module from adding a fake user. @@ -326,6 +323,12 @@ public function testHooks() { // called once for each subscriber, so 2 times). $this->messageSubscribers->sendMessage($node, $message, [], ['entity access' => FALSE]); $this->assertEquals(2, \Drupal::state('message_subscribe_test')->get('message_alter_hook_called', FALSE)); + + // Verify legacy array support. Will trigger a notice. + $this->setExpectedException(\PHPUnit_Framework_Error_Deprecated::class); + \Drupal::state('message_subscribe_test')->set('disable_subscribers_alter', FALSE); + \Drupal::state('message_subscribe_test')->set('use_array', TRUE); + $this->messageSubscribers->sendMessage($node, $message, [], ['entity access' => FALSE]); } /**