Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Menu/LocalActionDefault.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Menu; | |
4 | |
5 use Drupal\Component\Plugin\PluginBase; | |
6 use Drupal\Core\Cache\Cache; | |
7 use Drupal\Core\Cache\CacheableDependencyInterface; | |
8 use Drupal\Core\DependencyInjection\DependencySerializationTrait; | |
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
10 use Drupal\Core\Routing\RouteMatchInterface; | |
11 use Drupal\Core\Routing\RouteProviderInterface; | |
12 use Symfony\Component\DependencyInjection\ContainerInterface; | |
13 use Symfony\Component\HttpFoundation\Request; | |
14 | |
15 /** | |
16 * Provides a default implementation for local action plugins. | |
17 */ | |
18 class LocalActionDefault extends PluginBase implements LocalActionInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface { | |
19 | |
20 use DependencySerializationTrait; | |
21 | |
22 /** | |
23 * The route provider to load routes by name. | |
24 * | |
25 * @var \Drupal\Core\Routing\RouteProviderInterface | |
26 */ | |
27 protected $routeProvider; | |
28 | |
29 /** | |
30 * Constructs a LocalActionDefault object. | |
31 * | |
32 * @param array $configuration | |
33 * A configuration array containing information about the plugin instance. | |
34 * @param string $plugin_id | |
35 * The plugin_id for the plugin instance. | |
36 * @param mixed $plugin_definition | |
37 * The plugin implementation definition. | |
38 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider | |
39 * The route provider to load routes by name. | |
40 */ | |
41 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider) { | |
42 parent::__construct($configuration, $plugin_id, $plugin_definition); | |
43 | |
44 $this->routeProvider = $route_provider; | |
45 } | |
46 | |
47 /** | |
48 * {@inheritdoc} | |
49 */ | |
50 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
51 return new static( | |
52 $configuration, | |
53 $plugin_id, | |
54 $plugin_definition, | |
55 $container->get('router.route_provider') | |
56 ); | |
57 } | |
58 | |
59 /** | |
60 * {@inheritdoc} | |
61 */ | |
62 public function getRouteName() { | |
63 return $this->pluginDefinition['route_name']; | |
64 } | |
65 | |
66 /** | |
67 * {@inheritdoc} | |
68 */ | |
69 public function getTitle(Request $request = NULL) { | |
70 // Subclasses may pull in the request or specific attributes as parameters. | |
71 // The title from YAML file discovery may be a TranslatableMarkup object. | |
72 return (string) $this->pluginDefinition['title']; | |
73 } | |
74 | |
75 /** | |
76 * {@inheritdoc} | |
77 */ | |
78 public function getWeight() { | |
79 return $this->pluginDefinition['weight']; | |
80 } | |
81 | |
82 /** | |
83 * {@inheritdoc} | |
84 */ | |
85 public function getRouteParameters(RouteMatchInterface $route_match) { | |
86 $parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : []; | |
87 $route = $this->routeProvider->getRouteByName($this->getRouteName()); | |
88 $variables = $route->compile()->getVariables(); | |
89 | |
90 // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has | |
91 // processed the Request attributes, and in that case the _raw_variables | |
92 // attribute holds the original path strings keyed to the corresponding | |
93 // slugs in the path patterns. For example, if the route's path pattern is | |
94 // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then | |
95 // $raw_variables->get('filter_format') == 'plain_text'. | |
96 $raw_variables = $route_match->getRawParameters(); | |
97 | |
98 foreach ($variables as $name) { | |
99 if (isset($parameters[$name])) { | |
100 continue; | |
101 } | |
102 | |
103 if ($raw_variables && $raw_variables->has($name)) { | |
104 $parameters[$name] = $raw_variables->get($name); | |
105 } | |
106 elseif ($value = $route_match->getRawParameter($name)) { | |
107 $parameters[$name] = $value; | |
108 } | |
109 } | |
110 // The UrlGenerator will throw an exception if expected parameters are | |
111 // missing. This method should be overridden if that is possible. | |
112 return $parameters; | |
113 } | |
114 | |
115 /** | |
116 * {@inheritdoc} | |
117 */ | |
118 public function getOptions(RouteMatchInterface $route_match) { | |
119 return (array) $this->pluginDefinition['options']; | |
120 } | |
121 | |
122 /** | |
123 * {@inheritdoc} | |
124 */ | |
125 public function getCacheTags() { | |
126 if (!isset($this->pluginDefinition['cache_tags'])) { | |
127 return []; | |
128 } | |
129 return $this->pluginDefinition['cache_tags']; | |
130 } | |
131 | |
132 /** | |
133 * {@inheritdoc} | |
134 */ | |
135 public function getCacheContexts() { | |
136 if (!isset($this->pluginDefinition['cache_contexts'])) { | |
137 return []; | |
138 } | |
139 return $this->pluginDefinition['cache_contexts']; | |
140 } | |
141 | |
142 /** | |
143 * {@inheritdoc} | |
144 */ | |
145 public function getCacheMaxAge() { | |
146 if (!isset($this->pluginDefinition['cache_max_age'])) { | |
147 return Cache::PERMANENT; | |
148 } | |
149 return $this->pluginDefinition['cache_max_age']; | |
150 } | |
151 | |
152 } |