|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_workflow\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Event\KernelEvent; |
|
8
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
10
|
|
|
use Drupal\Core\Cache\CacheTagsInvalidator; |
|
11
|
|
|
use Drupal\Core\Theme\ThemeManager; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class LatestCheckSubscriber. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Drupal\df_tools_workflow |
|
18
|
|
|
*/ |
|
19
|
|
|
class LatestCheckSubscriber implements EventSubscriberInterface { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Drupal\Core\Cache\CacheTagsInvalidator definition. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Drupal\Core\Cache\CacheTagsInvalidator |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cacheTagsInvalidator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Drupal\Core\Theme\ThemeManager definition. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \Drupal\Core\Theme\ThemeManager |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $themeManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(CacheTagsInvalidator $cache_tags_invalidator, ThemeManager $theme_manager) { |
|
39
|
|
|
$this->cacheTagsInvalidator = $cache_tags_invalidator; |
|
40
|
|
|
$this->themeManager = $theme_manager; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
static function getSubscribedEvents() { |
|
|
|
|
|
|
47
|
|
|
$events[KernelEvents::REQUEST][] = array('checkForLatest'); |
|
|
|
|
|
|
48
|
|
|
return $events; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Given 'node' in the request params, |
|
53
|
|
|
* and the current user can view 'latest' revisions: |
|
54
|
|
|
* Invalidate the current theme's local actions cache, |
|
55
|
|
|
* so that hook_menu_local_tasks_alter() is called. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event |
|
58
|
|
|
*/ |
|
59
|
|
|
public function checkForLatest(GetResponseEvent $event) { |
|
60
|
|
|
if ($node = $event->getRequest()->get('node')) { |
|
|
|
|
|
|
61
|
|
|
if (\Drupal::currentUser()->hasPermission('view latest version')) { |
|
62
|
|
|
$theme = $this->themeManager->getActiveTheme()->getName(); |
|
63
|
|
|
$tag = 'config:block.block.' . $theme . '_local_actions'; |
|
64
|
|
|
$this->cacheTagsInvalidator->invalidateTags([$tag]); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.