Commit def99269 authored by catch's avatar catch

Issue #2095959 by dawehner, Berdir, InternetDevels: Remove instances of menu_get_object('node').

parent 2fbe2b56
......@@ -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.
*
......
......@@ -2241,7 +2241,7 @@ function template_preprocess_page(&$variables) {
);
}
if ($node = menu_get_object()) {
if ($node = \Drupal::request()->attributes->get('node')) {
$variables['node'] = $node;
}
......
......@@ -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') {
......
......@@ -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();
}
}
}
......@@ -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();
......
......@@ -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;
}
}
......
......@@ -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,
......
......@@ -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) {
......
......@@ -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'));
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment