diff --git a/core/includes/menu.inc b/core/includes/menu.inc index 767555e436381d783f18bce9452e4bac36f0be07..ad7a55604c7fe4dc30f4daf9a22a5c1f876553f5 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -956,59 +956,6 @@ function menu_item_route_access(Route $route, $href, &$map, Request $request = N return \Drupal::service('access_manager')->check($route, $request, \Drupal::currentUser()); } -/** - * Gets a loaded object from a router item. - * - * menu_get_object() provides access to objects loaded by the current router - * item. For example, on the page node/%node, the router loads the %node object, - * and calling menu_get_object() will return that. Normally, it is necessary to - * specify the type of object referenced, however node is the default. - * The following example tests to see whether the node being displayed is of the - * "story" content type: - * @code - * $node = menu_get_object(); - * $story = $node->getType() == 'story'; - * @endcode - * - * @param $type - * Type of the object. These appear in hook_menu definitions as %type. Core - * provides aggregator_feed, aggregator_category, contact, filter_format, - * forum_term, menu, menu_link, node, taxonomy_vocabulary, user. See the - * relevant {$type}_load function for more on each. Defaults to node. - * @param $position - * The position of the object in the path, where the first path segment is 0. - * For node/%node, the position of %node is 1, but for comment/reply/%node, - * it's 2. Defaults to 1. - * @param $path - * See menu_get_item() for more on this. Defaults to the current path. - * - * @todo Remove this function in https://drupal.org/node/2091399. - */ -function menu_get_object($type = 'node', $position = 1, $path = NULL) { - $router_item = menu_get_item($path); - if (empty($router_item['map'][$position])) { - return; - } - - if (isset($router_item['load_functions'][$position]) && $router_item['load_functions'][$position] == $type . '_load') { - return $router_item['map'][$position]; - } - - // If the path is route-based, use the route path instead of the menu item. - // The most common use of this function is for the node page, which has a - // route path of '/node/{node}'. By splitting that path into parts, we check - // for the $type wrapped in curly braces at the correct $position, returning - // the value found there. - $request = \Drupal::request(); - if ($request->attributes->has(RouteObjectInterface::ROUTE_OBJECT)) { - $path = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)->getPath(); - $parts = explode('/', ltrim($path, '/')); - if (isset($parts[$position]) && $parts[$position] == '{' . $type . '}') { - return $router_item['map'][$position]; - } - } -} - /** * Renders a menu tree based on the current path. * diff --git a/core/includes/theme.inc b/core/includes/theme.inc index f98aaddd139cbee9d2d97ee3d51b03bb9deee6aa..0a04480eba9cc3bde83bc386133f31a9182c06ec 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2241,7 +2241,7 @@ function template_preprocess_page(&$variables) { ); } - if ($node = menu_get_object()) { + if ($node = \Drupal::request()->attributes->get('node')) { $variables['node'] = $node; } 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 3fd3f6457f799663735286e835e305620487ccf8..456a9934137356dcf9caf31bc84aa5f06751588f 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 new BookNavigationBlock instance. + * + * @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,7 +101,7 @@ public function blockSubmit($form, &$form_state) { */ public function build() { $current_bid = 0; - if ($node = menu_get_object()) { + 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 9845450f24a9f058339aeb6d3b625b36bf058668..edcbcac92d34216f07de953b89e7781002a2d9a3 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 @@ -8,9 +8,12 @@ namespace Drupal\node\Plugin\views\argument_default; use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase; +use Drupal\node\NodeInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; /** - * Default argument plugin to extract a node via menu_get_object + * Default argument plugin to extract a node. * * This plugin actually has no options so it odes not need to do a great deal. * @@ -21,17 +24,49 @@ */ class Node extends ArgumentDefaultPluginBase { - public function getArgument() { - foreach (range(1, 3) as $i) { - $node = menu_get_object('node', $i); - if (!empty($node)) { - return $node->id(); - } - } + /** + * The request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; - if (arg(0) == 'node' && is_numeric(arg(1))) { - return arg(1); - } + /** + * Constructs a new Node instance. + * + * @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} + */ + public function getArgument() { + if (($node = $this->request->attributes->get('node')) && $node instanceof NodeInterface) { + return $node->id(); + } + } } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeBlockFunctionalTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeBlockFunctionalTest.php index 7bcde78259fc636b4640b8f074dacdad104e6618..49fb5d95bc34ba537cc3d42c8b0833d0111cf09e 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeBlockFunctionalTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeBlockFunctionalTest.php @@ -138,6 +138,9 @@ public function testRecentNodeBlock() { // Create a page node. $node5 = $this->drupalCreateNode(array('uid' => $this->adminUser->id(), 'type' => 'page')); + $this->drupalLogout(); + $this->drupalLogin($this->webUser); + // Verify visibility rules. $this->drupalGet(''); $label = $block->label(); diff --git a/core/modules/node/node.module b/core/modules/node/node.module index dcab38efc9610ece32a54d6ebb4c6182a32d63dd..7c4f386ab4427119160ae39fee5d1e408679847e 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -21,6 +21,7 @@ use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Template\Attribute; use Drupal\file\Entity\File; +use Symfony\Cmf\Component\Routing\RouteObjectInterface; /** * Denotes that the node is not published. @@ -581,7 +582,10 @@ function node_revision_delete($revision_id) { * The ID of the node if this is a full page view, otherwise FALSE. */ function node_is_page(NodeInterface $node) { - $page_node = menu_get_object(); + $request = \Drupal::request(); + if ($request->attributes->get(RouteObjectInterface::ROUTE_NAME) == 'node.view') { + $page_node = $request->attributes->get('node'); + } return (!empty($page_node) ? $page_node->id() == $node->id() : FALSE); } @@ -590,7 +594,7 @@ function node_is_page(NodeInterface $node) { */ function node_preprocess_html(&$variables) { // If on an individual node page, add the node type to body classes. - if ($node = menu_get_object()) { + if (($node = \Drupal::request()->attributes->get('node')) && $node instanceof NodeInterface) { $variables['attributes']['class'][] = drupal_html_class('node-type-' . $node->getType()); } } @@ -1159,26 +1163,23 @@ function node_block_access($block) { // @see node_form_block_form_alter() return; } - $node = menu_get_object(); - $node_types = node_type_get_types(); - if (arg(0) == 'node' && arg(1) == 'add' && arg(2)) { - $node_add_arg = strtr(arg(2), '-', '_'); - } // For blocks with node types associated, if the node type does not match // the settings from this block, deny access to it. - if (!empty($node)) { - // This is a node or node edit page. + $request = \Drupal::request(); + if ($node = $request->attributes->get('node')) { + // Page has node. return in_array($node->bundle(), $allowed_types); } - elseif (isset($node_add_arg) && isset($node_types[$node_add_arg])) { - // This is a node creation page - return in_array($node_add_arg, $allowed_types); - } - else { - // This page does not match the $allowed_types so deny access. - return FALSE; + // This is a node creation page. + // $request->attributes->has('node_type') would also match administration + // configuration pages, which the previous URI path options did not. + if ($request->attributes->get(RouteObjectInterface::ROUTE_NAME) == 'node.add') { + $node_type = $request->attributes->get('node_type'); + return in_array($node_type->id(), $allowed_types); } + + return FALSE; } } diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 54e04a39d16cc72de31c638458a8b3b6c763554c..492c8c86579f030ec25c529ec91d65eff50bf7c6 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -449,7 +449,7 @@ function hook_page_build(&$page) { } // Append a standard disclaimer to the content region on a node detail page. - if (menu_get_object('node', 1)) { + if (\Drupal::request()->attributes->get('node')) { $page['content']['disclaimer'] = array( '#markup' => t('Acme, Inc. is not responsible for the contents of this sample code.'), '#weight' => 25, 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 57ad4285d229321e99680c26c91ded54aa798555..2f6e58b629b1f4bff55b355883071100053168eb 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 @@ -10,6 +10,9 @@ use Drupal\views\ViewExecutable; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase; +use Drupal\node\NodeInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; /** * Taxonomy tid default argument. @@ -21,6 +24,42 @@ */ class Tid extends ArgumentDefaultPluginBase { + /** + * The request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** + * Constructs a new Tid instance. + * + * @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') + ); + } + /** * Overrides \Drupal\views\Plugin\views\Plugin\views\PluginBase::init(). */ @@ -114,6 +153,9 @@ public function submitOptionsForm(&$form, &$form_state, &$options = array()) { $options['vids'] = array_filter($options['vids']); } + /** + * {@inheritdoc} + */ public function getArgument() { // Load default argument from taxonomy page. if (!empty($this->options['term_page'])) { @@ -123,14 +165,8 @@ public function getArgument() { } // Load default argument from node. if (!empty($this->options['node'])) { - foreach (range(1, 3) as $i) { - $node = menu_get_object('node', $i); - if (!empty($node)) { - break; - } - } // Just check, if a node could be detected. - if ($node) { + 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 3bf75efba0ef10ac24cf5bc5cccb64d332d22c5c..c11bc815419272a39730ed3adf57650e99e02c23 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 @@ -8,6 +8,9 @@ namespace Drupal\views\Plugin\views\argument; use Drupal\Core\Database\Database; +use Drupal\node\NodeInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; /** * Abstract argument handler for dates. @@ -41,16 +44,53 @@ class Date extends Formula { */ protected $argFormat = 'Y-m-d'; + /** + * The request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + var $option_name = 'default_argument_date'; + /** + * Constructs a new Date instance. + * + * @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') + ); + } + /** * Add an option to set the default value to the current date. */ public function defaultArgumentForm(&$form, &$form_state) { parent::defaultArgumentForm($form, $form_state); - $form['default_argument_type']['#options'] += array('date' => t('Current date')); - $form['default_argument_type']['#options'] += array('node_created' => t("Current node's creation time")); - $form['default_argument_type']['#options'] += array('node_changed' => t("Current node's update time")); } + $form['default_argument_type']['#options'] += array('date' => $this->t('Current date')); + $form['default_argument_type']['#options'] += array('node_created' => $this->t("Current node's creation time")); + $form['default_argument_type']['#options'] += array('node_changed' => $this->t("Current node's update time")); + } /** * Set the empty argument value to the current date, @@ -61,18 +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'))) { - foreach (range(1, 3) as $i) { - $node = menu_get_object('node', $i); - if (!empty($node)) { - continue; - } - } - - if (arg(0) == 'node' && is_numeric(arg(1))) { - $node = node_load(arg(1)); - } + $node = $this->request->attributes->get('node'); - if (empty($node)) { + if (!($node instanceof NodeInterface)) { return parent::getDefaultArgument(); } elseif ($this->options['default_argument_type'] == 'node_created') { @@ -90,7 +121,7 @@ public function getDefaultArgument($raw = FALSE) { * {@inheritdoc} */ public function getSortName() { - return t('Date', array(), array('context' => 'Sort order')); + return $this->t('Date', array(), array('context' => 'Sort order')); } /**