Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content type subscriptions. #34

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions config/optional/flag.flag.subscribe_node_type.yml
Original file line number Diff line number Diff line change
@@ -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: { }

14 changes: 11 additions & 3 deletions message_subscribe_ui/src/Controller/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/src/Kernel/SubscribersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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()]);
}

}