CourierMessageController.php in Courier 8
File
courier_message_composer/src/Controller/CourierMessageController.php
View source
<?php
namespace Drupal\courier_message_composer\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\courier\Service\IdentityChannelManagerInterface;
class CourierMessageController extends ControllerBase implements ContainerInjectionInterface {
protected $entityTypeManager;
protected $identityChannelManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, IdentityChannelManagerInterface $identity_channel_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->identityChannelManager = $identity_channel_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('plugin.manager.identity_channel'));
}
public function channelList() {
$render['channels'] = [
'#title' => $this
->t('Channels'),
'#theme' => 'item_list',
'#items' => [],
];
foreach (array_keys($this->identityChannelManager
->getChannels()) as $channel) {
if ($this
->composeAnyIdentityForChannel($channel)) {
$definition = $this->entityTypeManager
->getDefinition($channel);
$item = [];
$item[] = [
'#type' => 'link',
'#title' => $definition
->getLabel(),
'#url' => Url::fromRoute('courier_message_composer.compose', [
'courier_channel' => $channel,
]),
];
$render['channels']['#items'][] = $item;
}
}
return $render;
}
protected function composeAnyIdentityForChannel($channel) {
$channels = $this->identityChannelManager
->getChannels();
if (array_key_exists($channel, $channels)) {
foreach ($channels[$channel] as $identity) {
$permission = "courier_message_composer compose {$channel} to {$identity}";
if ($this
->currentUser()
->hasPermission($permission)) {
return TRUE;
}
}
}
return FALSE;
}
}