Skip to content

Commit

Permalink
Content type subscriptions.
Browse files Browse the repository at this point in the history
- Starts on #31
- Excludes any Views, as config entities do not readily work with Views
  • Loading branch information
jhedstrom committed Oct 3, 2016
1 parent 2fe6ba7 commit de45ea9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
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()]);
}

}

0 comments on commit de45ea9

Please sign in to comment.