annotate core/lib/Drupal/Core/Menu/LocalTaskDefault.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Menu;
Chris@0 4
Chris@0 5 use Drupal\Component\Plugin\PluginBase;
Chris@0 6 use Drupal\Core\Cache\Cache;
Chris@0 7 use Drupal\Core\Cache\CacheableDependencyInterface;
Chris@0 8 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
Chris@0 9 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 10 use Symfony\Component\HttpFoundation\Request;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Default object used for LocalTaskPlugins.
Chris@0 14 */
Chris@0 15 class LocalTaskDefault extends PluginBase implements LocalTaskInterface, CacheableDependencyInterface {
Chris@0 16
Chris@0 17 use DependencySerializationTrait;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The route provider to load routes by name.
Chris@0 21 *
Chris@0 22 * @var \Drupal\Core\Routing\RouteProviderInterface
Chris@0 23 */
Chris@0 24 protected $routeProvider;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * TRUE if this plugin is forced active for options attributes.
Chris@0 28 *
Chris@0 29 * @var bool
Chris@0 30 */
Chris@0 31 protected $active = FALSE;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * {@inheritdoc}
Chris@0 35 */
Chris@0 36 public function getRouteName() {
Chris@0 37 return $this->pluginDefinition['route_name'];
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * {@inheritdoc}
Chris@0 42 */
Chris@0 43 public function getRouteParameters(RouteMatchInterface $route_match) {
Chris@0 44 $parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
Chris@0 45 $route = $this->routeProvider()->getRouteByName($this->getRouteName());
Chris@0 46 $variables = $route->compile()->getVariables();
Chris@0 47
Chris@0 48 // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has
Chris@0 49 // processed the Request attributes, and in that case the _raw_variables
Chris@0 50 // attribute holds the original path strings keyed to the corresponding
Chris@0 51 // slugs in the path patterns. For example, if the route's path pattern is
Chris@0 52 // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
Chris@0 53 // $raw_variables->get('filter_format') == 'plain_text'.
Chris@0 54
Chris@0 55 $raw_variables = $route_match->getRawParameters();
Chris@0 56
Chris@0 57 foreach ($variables as $name) {
Chris@0 58 if (isset($parameters[$name])) {
Chris@0 59 continue;
Chris@0 60 }
Chris@0 61
Chris@0 62 if ($raw_variables && $raw_variables->has($name)) {
Chris@0 63 $parameters[$name] = $raw_variables->get($name);
Chris@0 64 }
Chris@0 65 elseif ($value = $route_match->getRawParameter($name)) {
Chris@0 66 $parameters[$name] = $value;
Chris@0 67 }
Chris@0 68 }
Chris@0 69 // The UrlGenerator will throw an exception if expected parameters are
Chris@0 70 // missing. This method should be overridden if that is possible.
Chris@0 71 return $parameters;
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public function getTitle(Request $request = NULL) {
Chris@0 78 // The title from YAML file discovery may be a TranslatableMarkup object.
Chris@0 79 return (string) $this->pluginDefinition['title'];
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Returns the weight of the local task.
Chris@0 84 *
Chris@0 85 * @return int
Chris@0 86 * The weight of the task. If not defined in the annotation returns 0 by
Chris@0 87 * default or -10 for the root tab.
Chris@0 88 */
Chris@0 89 public function getWeight() {
Chris@0 90 // By default the weight is 0, or -10 for the root tab.
Chris@0 91 if (!isset($this->pluginDefinition['weight'])) {
Chris@0 92 if ($this->pluginDefinition['base_route'] == $this->pluginDefinition['route_name']) {
Chris@0 93 $this->pluginDefinition['weight'] = -10;
Chris@0 94 }
Chris@0 95 else {
Chris@0 96 $this->pluginDefinition['weight'] = 0;
Chris@0 97 }
Chris@0 98 }
Chris@0 99 return (int) $this->pluginDefinition['weight'];
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * {@inheritdoc}
Chris@0 104 */
Chris@0 105 public function getOptions(RouteMatchInterface $route_match) {
Chris@0 106 $options = $this->pluginDefinition['options'];
Chris@0 107 if ($this->active) {
Chris@0 108 if (empty($options['attributes']['class']) || !in_array('is-active', $options['attributes']['class'])) {
Chris@0 109 $options['attributes']['class'][] = 'is-active';
Chris@0 110 }
Chris@0 111 }
Chris@0 112 return (array) $options;
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * {@inheritdoc}
Chris@0 117 */
Chris@0 118 public function setActive($active = TRUE) {
Chris@0 119 $this->active = $active;
Chris@0 120 return $this;
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * {@inheritdoc}
Chris@0 125 */
Chris@0 126 public function getActive() {
Chris@0 127 return $this->active;
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * Returns the route provider.
Chris@0 132 *
Chris@0 133 * @return \Drupal\Core\Routing\RouteProviderInterface
Chris@0 134 * The route provider.
Chris@0 135 */
Chris@0 136 protected function routeProvider() {
Chris@0 137 if (!$this->routeProvider) {
Chris@0 138 $this->routeProvider = \Drupal::service('router.route_provider');
Chris@0 139 }
Chris@0 140 return $this->routeProvider;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * {@inheritdoc}
Chris@0 145 */
Chris@0 146 public function getCacheTags() {
Chris@0 147 if (!isset($this->pluginDefinition['cache_tags'])) {
Chris@0 148 return [];
Chris@0 149 }
Chris@0 150 return $this->pluginDefinition['cache_tags'];
Chris@0 151 }
Chris@0 152
Chris@0 153 /**
Chris@0 154 * {@inheritdoc}
Chris@0 155 */
Chris@0 156 public function getCacheContexts() {
Chris@0 157 if (!isset($this->pluginDefinition['cache_contexts'])) {
Chris@0 158 return [];
Chris@0 159 }
Chris@0 160 return $this->pluginDefinition['cache_contexts'];
Chris@0 161 }
Chris@0 162
Chris@0 163 /**
Chris@0 164 * {@inheritdoc}
Chris@0 165 */
Chris@0 166 public function getCacheMaxAge() {
Chris@0 167 if (!isset($this->pluginDefinition['cache_max_age'])) {
Chris@0 168 return Cache::PERMANENT;
Chris@0 169 }
Chris@0 170 return $this->pluginDefinition['cache_max_age'];
Chris@0 171 }
Chris@0 172
Chris@0 173 }