LocalTaskDefault.php in Drupal 8
File
core/lib/Drupal/Core/Menu/LocalTaskDefault.php
View source
<?php
namespace Drupal\Core\Menu;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;
class LocalTaskDefault extends PluginBase implements LocalTaskInterface, CacheableDependencyInterface {
use DependencySerializationTrait;
protected $routeProvider;
protected $active = FALSE;
public function getRouteName() {
return $this->pluginDefinition['route_name'];
}
public function getRouteParameters(RouteMatchInterface $route_match) {
$route_parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
$route = $this
->routeProvider()
->getRouteByName($this
->getRouteName());
$variables = $route
->compile()
->getVariables();
$raw_parameters = $route_match
->getRawParameters();
$parameters = $route_match
->getParameters();
foreach ($variables as $name) {
if (isset($route_parameters[$name])) {
continue;
}
if ($raw_parameters
->has($name)) {
$route_parameters[$name] = $raw_parameters
->get($name);
}
elseif ($parameters
->has($name)) {
$route_parameters[$name] = $parameters
->get($name);
}
}
return $route_parameters;
}
public function getTitle(Request $request = NULL) {
return (string) $this->pluginDefinition['title'];
}
public function getWeight() {
if (!isset($this->pluginDefinition['weight'])) {
if ($this->pluginDefinition['base_route'] == $this->pluginDefinition['route_name']) {
$this->pluginDefinition['weight'] = -10;
}
else {
$this->pluginDefinition['weight'] = 0;
}
}
return (int) $this->pluginDefinition['weight'];
}
public function getOptions(RouteMatchInterface $route_match) {
$options = $this->pluginDefinition['options'];
if ($this->active) {
if (empty($options['attributes']['class']) || !in_array('is-active', $options['attributes']['class'])) {
$options['attributes']['class'][] = 'is-active';
}
}
return (array) $options;
}
public function setActive($active = TRUE) {
$this->active = $active;
return $this;
}
public function getActive() {
return $this->active;
}
protected function routeProvider() {
if (!$this->routeProvider) {
$this->routeProvider = \Drupal::service('router.route_provider');
}
return $this->routeProvider;
}
public function getCacheTags() {
if (!isset($this->pluginDefinition['cache_tags'])) {
return [];
}
return $this->pluginDefinition['cache_tags'];
}
public function getCacheContexts() {
if (!isset($this->pluginDefinition['cache_contexts'])) {
return [];
}
return $this->pluginDefinition['cache_contexts'];
}
public function getCacheMaxAge() {
if (!isset($this->pluginDefinition['cache_max_age'])) {
return Cache::PERMANENT;
}
return $this->pluginDefinition['cache_max_age'];
}
}