1

I was using menu_get_object() to load node information in Drupal 6.

if ($node = menu_get_object()) {
    // Get the nid
    $nid = $node->nid;
  }

Since menu_get_object() is deprecated in Drupal 8, I was using the following code.

if ($node_id = \Drupal::request()->attributes->get('node')) { #menu_get_object()   
    //load all the information of the node
    $node = node_load($node_id);
  }
  echo '<pre>';print_r($node);exit();echo '</pre>'; //its okay
  echo $node->type->value; //not working

This is not getting the node values, but printing the node information's in array. How do I resolve this ?

UPDATE

I am getting all other values like $node->title->value and $node->nid->value, but not $node->type->value.

5

See https://drupal.stackexchange.com/a/202185/45409

In Drupal 8, you can get url arguments based on what's in the routing.

To get the id alone:

$nid = \Drupal::routeMatch()->getRawParameter('node');

If you plan to load the node object, you can simply get it this way:

$node = \Drupal::routeMatch()->getParameter('node');

To find the parameter names, you can use:

$possible_parameters = \Drupal::routeMatch()->getParameters();

To find the node type, use:

$node->bundle();
  • i actually need the values inside node object , when i use it like : $nid = \Drupal::routeMatch()->getParameter('node'); $node = node_load($nid); #echo '<pre>';print_r($node);exit(); echo '</pre>'; echo $node->type->value;exit(); am getting all other values , but not $node->type->value; – Crazyrubixfan Jun 7 '16 at 15:48
  • try $node->bundle() – oknate Jun 7 '16 at 17:51
  • Also, try dump() in Drupal 8, instead of print_r, it's pretty nifty. – oknate Jun 7 '16 at 17:51
  • Also, avoid using node_load, it's deprecated, use \Drupal::entityManager()->getStorage('node')->load($nid); – oknate Jun 7 '16 at 17:52
  • It would be $node->type->target_id, because it's an entity reference field. It's not always ->value. But either bundle() or getType() gives you the same information and is easier to use. – Berdir Jun 18 '16 at 18:25
3

\Drupal::request()->attributes->get('node') will return the node object, not the node id.

if ($node = \Drupal::request()->attributes->get('node')) {   
  kint($node);
  echo $node->getType();    
}
  • kint($node) ??! – Crazyrubixfan Jun 7 '16 at 9:28
  • PHP Fatal error: Call to a member function getType() on string – Crazyrubixfan Jun 7 '16 at 9:30
  • @Icecreamsandwich kint() is for debugging in Drupal 8 and it comes with the devel module. You can also use print_r(). – Oana Hulpoi Jun 7 '16 at 9:35
  • @Icecreamsandwich $node should be an object, not a string. – Oana Hulpoi Jun 7 '16 at 9:38
3

If you compare what Drupal 7 does in template_preprocess_page() with what Drupal 8 does in the same function, you will see that the first function uses the following code.

  if ($node = menu_get_object()) {
    $variables['node'] = $node;
  }

The second function uses the following code.

  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $variables['node'] = $node;
  }

page.html.twig descrives node as follows:

node: Fully loaded node, if there is an automatically-loaded node associated with the page and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).

Also, node_load() should be replaced with the following code.

use \Drupal\node\Entity\Node;

$node = Node::load($nid);

You don't need Node::load() since $variables['node'] already contains a full-loaded node object.

  • This actually does the same , i am getting all the details except type when try to access like $node = Node::load($nid);cho $node->type->value;exit(); – Crazyrubixfan Jun 10 '16 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.