View source
<?php
namespace Drupal\message_subscribe_ui\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\flag\FlagServiceInterface;
use Drupal\message_subscribe\SubscribersInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Subscriptions extends BlockBase implements FormInterface, ContainerFactoryPluginInterface {
protected $currentUser;
protected $flagService;
protected $formBuilder;
protected $routeMatch;
protected $subscribers;
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder, SubscribersInterface $subscribers, RouteMatchInterface $route_match, AccountProxyInterface $current_user, FlagServiceInterface $flag_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
$this->flagService = $flag_service;
$this->formBuilder = $form_builder;
$this->routeMatch = $route_match;
$this->subscribers = $subscribers;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('form_builder'), $container
->get('message_subscribe.subscribers'), $container
->get('current_route_match'), $container
->get('current_user'), $container
->get('flag'));
}
protected function getCurrentEntity() {
foreach ($this->routeMatch
->getParameters() as $parameter) {
if ($parameter instanceof EntityInterface) {
return $parameter;
}
}
}
public function build() {
if (!($entity = $this
->getCurrentEntity()) || !$this
->hasSubscribableEntites($entity)) {
return [
'#cache' => [
'contexts' => [
'user',
'url.path',
],
],
];
}
return $this->formBuilder
->getForm($this);
}
public function getFormId() {
return 'message_subscribe_ui_block';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$entity = $this
->getCurrentEntity();
$entities = [
$entity,
];
$entities += $entity
->referencedEntities();
$form['description'] = [
'#type' => 'markup',
'#markup' => $this
->t('Manage all <a href=":url">subscriptions</a>.', [
':url' => Url::fromRoute('message_subscribe_ui.tab', [
'user' => \Drupal::currentUser()
->id(),
])
->toString(),
]),
];
$form['subscriptions'] = [
'#type' => 'container',
'#tree' => TRUE,
];
foreach ($entities as $referenced_entity) {
if ($referenced_entity
->access('view')) {
$flags = $this->subscribers
->getFlags($referenced_entity
->getEntityTypeId(), $referenced_entity
->bundle(), $this->currentUser);
if (!empty($flags)) {
$flag = reset($flags);
$form['subscriptions'][$referenced_entity
->getEntityTypeId()][$referenced_entity
->id()] = [
'#type' => 'checkbox',
'#title' => $this
->getLabel($referenced_entity),
'#default_value' => !empty($this->flagService
->getFlagging($flag, $referenced_entity, $this->currentUser)),
'#flags' => $flags,
'#entity' => $referenced_entity,
];
}
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
protected function getLabel(EntityInterface $entity) {
$label = $entity instanceof UserInterface ? $entity
->getDisplayName() : $entity
->label();
return $this
->t('Subscribe to @label', [
'@label' => $label,
]);
}
protected function hasSubscribableEntites(EntityInterface $entity) {
$entities = [
$entity,
];
$entities += $entity
->referencedEntities();
foreach ($entities as $referenced_entity) {
if ($referenced_entity
->access('view')) {
$flags = $this->subscribers
->getFlags($referenced_entity
->getEntityTypeId(), $referenced_entity
->bundle(), $this->currentUser);
if (!empty($flags)) {
return TRUE;
}
}
}
return FALSE;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValue('subscriptions') as $entity_type => $entities) {
foreach ($entities as $entity_id => $subscribe) {
$flags = $form['subscriptions'][$entity_type][$entity_id]['#flags'];
$entity = $form['subscriptions'][$entity_type][$entity_id]['#entity'];
foreach ($flags as $flag) {
try {
if ($subscribe) {
$this->flagService
->flag($flag, $entity, $this->currentUser);
}
else {
$this->flagService
->unflag($flag, $entity, $this->currentUser);
}
} catch (\LogicException $e) {
}
}
}
}
}
}