diff --git a/.travis.yml b/.travis.yml index 9e68280..0a6c248 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,6 +51,13 @@ before_script: - travis_retry git clone --branch 8.x-1.x --depth 1 https://github.com/Gizra/message_notify.git - cd .. + # Patch Flag for Config Entity flaggings. + # @todo Remove once https://www.drupal.org/node/2678756 is committed. + - cd modules/flag + - curl https://www.drupal.org/files/issues/2678756-20.patch > patch.txt + - git apply patch.txt && rm patch.txt + - cd - + # Install Composer dependencies on 8.1.x and above. - test ${DRUPAL_CORE} == "8.0.x" || composer self-update && composer install diff --git a/config/optional/flag.flag.subscribe_node_type.yml b/config/optional/flag.flag.subscribe_node_type.yml new file mode 100644 index 0000000..91e09e6 --- /dev/null +++ b/config/optional/flag.flag.subscribe_node_type.yml @@ -0,0 +1,26 @@ +langcode: en +status: true +dependencies: { } +id: subscribe_node_type +label: 'Content type' +bundles: { } +entity_type: node_type +enabled: false +global: false +weight: 0 +flag_short: Subscribe +flag_long: '' +flag_message: 'You are now subscribed to this content type.' +unflag_short: Unsubscribe +unflag_long: '' +unflag_message: 'You are no longer subscribed to this content type.' +unflag_denied_text: '' +flag_type: 'entity:node_type' +link_type: ajax_link +flagTypeConfig: + show_in_links: { } + show_as_field: 1 + show_on_form: 0 + show_contextual_link: 0 +linkTypeConfig: { } + diff --git a/message_subscribe_ui/src/Controller/SubscriptionController.php b/message_subscribe_ui/src/Controller/SubscriptionController.php index f1e939d..2ce7496 100644 --- a/message_subscribe_ui/src/Controller/SubscriptionController.php +++ b/message_subscribe_ui/src/Controller/SubscriptionController.php @@ -5,6 +5,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\flag\FlagInterface; @@ -146,7 +147,13 @@ public function tab(UserInterface $user, FlagInterface $flag = NULL) { if (!$flag) { // We are inside /message-subscribe so get the first flag. $flags = $this->subscribers->getFlags(); - $flag = reset($flags); + + // Grab the first non-config entity flag. + foreach ($flags as $flag) { + if (\Drupal::entityTypeManager()->getDefinition($flag->getFlaggableEntityTypeId()) instanceof ContentEntityTypeInterface) { + break; + } + } } $view = $this->getView($user, $flag); @@ -165,8 +172,9 @@ public function tab(UserInterface $user, FlagInterface $flag = NULL) { * @param \Drupal\flag\FlagInterface $flag * The flag for which to find a matching view. * - * @return \Drupal\views\ViewExecutable - * The corresponding view executable. + * @return \Drupal\views\ViewExecutable|bool + * The corresponding view executable. FALSE if the entity type is not a + * content entity. * * @throws \Drupal\message_subscribe\Exception\MessageSubscribeException * - If a view corresponding to the `subscribe_ENTITY_TYPE_ID` does not diff --git a/message_subscribe_ui/src/Plugin/Derivative/MessageSubscribeUiLocalTask.php b/message_subscribe_ui/src/Plugin/Derivative/MessageSubscribeUiLocalTask.php index 6be1975..2256759 100644 --- a/message_subscribe_ui/src/Plugin/Derivative/MessageSubscribeUiLocalTask.php +++ b/message_subscribe_ui/src/Plugin/Derivative/MessageSubscribeUiLocalTask.php @@ -3,6 +3,7 @@ namespace Drupal\message_subscribe_ui\Plugin\Derivative; use Drupal\Component\Plugin\Derivative\DeriverBase; +use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\message_subscribe\SubscribersInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -46,6 +47,12 @@ public function getDerivativeDefinitions($base_plugin_definition) { $first = TRUE; foreach ($this->subscribers->getFlags() as $flag) { + // @todo Remove this once config entities can have views with + // relationships. + if (!\Drupal::entityTypeManager()->getDefinition($flag->getFlaggableEntityTypeId()) instanceof ContentEntityTypeInterface) { + continue; + } + $this->derivatives[$flag->id()] = [ 'title' => $flag->label(), // First route gets the same route name as the parent (in order to diff --git a/tests/src/Kernel/SubscribersTest.php b/tests/src/Kernel/SubscribersTest.php index 37a6588..87a3149 100644 --- a/tests/src/Kernel/SubscribersTest.php +++ b/tests/src/Kernel/SubscribersTest.php @@ -2,9 +2,11 @@ namespace Drupal\Tests\message_subscribe\Kernel; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Session\AccountInterface; use Drupal\message\Entity\Message; use Drupal\message\Entity\MessageTemplate; +use Drupal\node\Entity\NodeType; use Drupal\simpletest\NodeCreationTrait; /** @@ -74,6 +76,10 @@ public function setUp() { $flag->enable(); $flag->save(); + $flag = $flags['subscribe_node_type']; + $flag->enable(); + $flag->save(); + $this->users[1] = $this->createUser([ 'flag subscribe_node', 'unflag subscribe_node', @@ -328,4 +334,24 @@ public function testHooks() { ], $uids); } + /** + * Tests config entity subscriptions. + */ + public function testConfigEntities() { + // Subscribe user 2 to 'article' nodes. + $flag = $this->flagService->getFlagById('subscribe_node_type'); + $node_type = NodeType::create([ + 'type' => Unicode::strtolower($this->randomMachineName()), + 'name' => $this->randomString(), + ]); + $node_type->save(); + $this->flagService->flag($flag, $node_type, $this->users[2]); + $message = Message::create([ + 'template' => 'foo', + 'uid' => $this->users[2], + ]); + $subscribers = $this->messageSubscribers->getSubscribers($node_type, $message); + $this->assertNotEmpty($subscribers[$this->users[2]->id()]); + } + }