Skip to content

Commit 0299343

Browse files
committed
Content type subscriptions.
- Starts on #31 - Excludes any Views, as config entities do not readily work with Views
1 parent 2fe6ba7 commit 0299343

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
langcode: en
2+
status: true
3+
dependencies: { }
4+
id: subscribe_node_type
5+
label: 'Content type'
6+
bundles: { }
7+
entity_type: node_type
8+
enabled: false
9+
global: false
10+
weight: 0
11+
flag_short: Subscribe
12+
flag_long: ''
13+
flag_message: 'You are now subscribed to this content type.'
14+
unflag_short: Unsubscribe
15+
unflag_long: ''
16+
unflag_message: 'You are no longer subscribed to this content type.'
17+
unflag_denied_text: ''
18+
flag_type: 'entity:node_type'
19+
link_type: ajax_link
20+
flagTypeConfig:
21+
show_in_links: { }
22+
show_as_field: 1
23+
show_on_form: 0
24+
show_contextual_link: 0
25+
linkTypeConfig: { }
26+

message_subscribe_ui/src/Controller/SubscriptionController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Core\Access\AccessResult;
66
use Drupal\Core\Config\ConfigFactoryInterface;
77
use Drupal\Core\Controller\ControllerBase;
8+
use Drupal\Core\Entity\ContentEntityTypeInterface;
89
use Drupal\Core\Session\AccountInterface;
910
use Drupal\Core\Session\AccountProxyInterface;
1011
use Drupal\flag\FlagInterface;
@@ -146,7 +147,13 @@ public function tab(UserInterface $user, FlagInterface $flag = NULL) {
146147
if (!$flag) {
147148
// We are inside /message-subscribe so get the first flag.
148149
$flags = $this->subscribers->getFlags();
149-
$flag = reset($flags);
150+
151+
// Grab the first non-config entity flag.
152+
foreach ($flags as $flag) {
153+
if (\Drupal::entityTypeManager()->getDefinition($flag->getFlaggableEntityTypeId()) instanceof ContentEntityTypeInterface) {
154+
break;
155+
}
156+
}
150157
}
151158

152159
$view = $this->getView($user, $flag);
@@ -165,8 +172,9 @@ public function tab(UserInterface $user, FlagInterface $flag = NULL) {
165172
* @param \Drupal\flag\FlagInterface $flag
166173
* The flag for which to find a matching view.
167174
*
168-
* @return \Drupal\views\ViewExecutable
169-
* The corresponding view executable.
175+
* @return \Drupal\views\ViewExecutable|bool
176+
* The corresponding view executable. FALSE if the entity type is not a
177+
* content entity.
170178
*
171179
* @throws \Drupal\message_subscribe\Exception\MessageSubscribeException
172180
* - If a view corresponding to the `subscribe_ENTITY_TYPE_ID` does not

message_subscribe_ui/src/Plugin/Derivative/MessageSubscribeUiLocalTask.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\message_subscribe_ui\Plugin\Derivative;
44

55
use Drupal\Component\Plugin\Derivative\DeriverBase;
6+
use Drupal\Core\Entity\ContentEntityTypeInterface;
67
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
78
use Drupal\message_subscribe\SubscribersInterface;
89
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -46,6 +47,12 @@ public function getDerivativeDefinitions($base_plugin_definition) {
4647

4748
$first = TRUE;
4849
foreach ($this->subscribers->getFlags() as $flag) {
50+
// @todo Remove this once config entities can have views with
51+
// relationships.
52+
if (!\Drupal::entityTypeManager()->getDefinition($flag->getFlaggableEntityTypeId()) instanceof ContentEntityTypeInterface) {
53+
continue;
54+
}
55+
4956
$this->derivatives[$flag->id()] = [
5057
'title' => $flag->label(),
5158
// First route gets the same route name as the parent (in order to

tests/src/Kernel/SubscribersTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Drupal\Tests\message_subscribe\Kernel;
44

5+
use Drupal\Component\Utility\Unicode;
56
use Drupal\Core\Session\AccountInterface;
67
use Drupal\message\Entity\Message;
78
use Drupal\message\Entity\MessageTemplate;
9+
use Drupal\node\Entity\NodeType;
810
use Drupal\simpletest\NodeCreationTrait;
911

1012
/**
@@ -74,6 +76,10 @@ public function setUp() {
7476
$flag->enable();
7577
$flag->save();
7678

79+
$flag = $flags['subscribe_content_type'];
80+
$flag->enable();
81+
$flag->save();
82+
7783
$this->users[1] = $this->createUser([
7884
'flag subscribe_node',
7985
'unflag subscribe_node',
@@ -328,4 +334,24 @@ public function testHooks() {
328334
], $uids);
329335
}
330336

337+
/**
338+
* Tests config entity subscriptions.
339+
*/
340+
public function testConfigEntities() {
341+
// Subscribe user 2 to 'article' nodes.
342+
$flag = $this->flagService->getFlagById('subscribe_node_type');
343+
$node_type = NodeType::create([
344+
'type' => Unicode::strtolower($this->randomMachineName()),
345+
'name' => $this->randomString(),
346+
]);
347+
$node_type->save();
348+
$this->flagService->flag($flag, $node_type, $this->users[2]);
349+
$message = Message::create([
350+
'template' => 'foo',
351+
'uid' => $this->users[2],
352+
]);
353+
$subscribers = $this->messageSubscribers->getSubscribers($node_type, $message);
354+
$this->assertNotEmpty($subscribers[$this->users[2]->id()]);
355+
}
356+
331357
}

0 commit comments

Comments
 (0)