diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 37d5ec2..7dc62f3 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2281,9 +2281,8 @@ function template_preprocess_page(&$variables) { ); } - $request = \Drupal::request(); - if ($request->attributes->has('node')) { - $variables['node'] = $request->attributes->get('node'); + if ($node = \Drupal::request()->attributes->get('node')) { + $variables['node'] = $variables['node']; } // Prepare render array for messages. drupal_get_messages() is called later, diff --git a/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php b/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php index d8aa371..571a91e 100644 --- a/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php +++ b/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php @@ -8,6 +8,9 @@ namespace Drupal\book\Plugin\Block; use Drupal\block\BlockBase; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; /** * Provides a 'Book navigation' block. @@ -18,7 +21,44 @@ * category = @Translation("Menus") * ) */ -class BookNavigationBlock extends BlockBase { +class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInterface { + + /** + * The request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** + * Constructs a Drupal\Component\Plugin\PluginBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Symfony\Component\HttpFoundation\Request $request + * The request object. + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, Request $request) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->request = $request; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('request') + ); + } /** * {@inheritdoc} @@ -61,9 +101,7 @@ public function blockSubmit($form, &$form_state) { */ public function build() { $current_bid = 0; - $request = \Drupal::request(); - if ($request->attributes->has('node')) { - $node = $request->attributes->get('node'); + if ($node = $this->request->get('node')) { $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid']; } if ($this->configuration['block_mode'] == 'all pages') { diff --git a/core/modules/node/lib/Drupal/node/Plugin/views/argument_default/Node.php b/core/modules/node/lib/Drupal/node/Plugin/views/argument_default/Node.php index b920d11..59d66ce 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/views/argument_default/Node.php +++ b/core/modules/node/lib/Drupal/node/Plugin/views/argument_default/Node.php @@ -45,6 +45,7 @@ class Node extends ArgumentDefaultPluginBase { */ public function __construct(array $configuration, $plugin_id, array $plugin_definition, Request $request) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->request = $request; } @@ -64,11 +65,8 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function getArgument() { - if ($this->request->attributes->has('node')) { - $node = $this->request->attributes->get('node'); - if ($node instanceof NodeInterface) { - return $node->id(); - } + if (($node = $this->request->attributes->get('node')) && $node instanceof NodeInterface) { + return $node->id(); } } } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index c7e2785..24fc6ed 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -595,12 +595,8 @@ function node_is_page(EntityInterface $node) { */ function node_preprocess_html(&$variables) { // If on an individual node page, add the node type to body classes. - $request = \Drupal::request(); - if ($request->attributes->has('node')) { - $node = $request->attributes->get('node'); - if ($node instanceof NodeInterface) { - $variables['attributes']['class'][] = drupal_html_class('node-type-' . $node->getType()); - } + if (($node = \Drupal::request()->attributes->get('node')) && $node instanceof NodeInterface) { + $variables['attributes']['class'][] = drupal_html_class('node-type-' . $node->getType()); } } @@ -1172,9 +1168,8 @@ function node_block_access($block) { // For blocks with node types associated, if the node type does not match // the settings from this block, deny access to it. $request = \Drupal::request(); - if ($request->attributes->has('node')) { + if ($node = \Drupal::request()->attributes->get('node')) { // Page has node. - $node = $request->attributes->get('node'); return in_array($node->bundle(), $allowed_types); } // This is a node creation page. diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php index 620767e..f94efd1 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php @@ -165,12 +165,8 @@ public function getArgument() { } // Load default argument from node. if (!empty($this->options['node'])) { - if ($this->request->attributes->has('node')) { - $node = $this->request->attributes->get('node'); - } - // Just check, if a node could be detected. - if (!empty($node) && $node instanceof NodeInterface) { + if (($node = $this->request->attributes->has('node')) && $node instanceof NodeInterface) { $taxonomy = array(); $instances = field_info_instances('node', $node->getType()); foreach ($instances as $instance) { diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php b/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php index b9cf60c..3ac31eb 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php @@ -101,11 +101,9 @@ public function getDefaultArgument($raw = FALSE) { return date($this->argFormat, REQUEST_TIME); } elseif (!$raw && in_array($this->options['default_argument_type'], array('node_created', 'node_changed'))) { - if ($this->request->attributes->has('node')) { - $node = $this->request->attributes->get('node'); - } + $node = $this->request->attributes->get('node'); - if (empty($node) || !$node instanceof NodeInterface) { + if (!($node instanceof NodeInterface)) { return parent::getDefaultArgument(); } elseif ($this->options['default_argument_type'] == 'node_created') {