Drupal - getting node id from a view to configure a link in a block

How can I build a block in Drupal that can show the node identifier of the view page on which the block is currently sitting?

I use views to create a large fragment of my site, but I need to be able to create โ€œsmartโ€ blocks in PHP mode that will have dynamic content depending on what the view displays.

How can I find the $ nid currently displayed?

+6
drupal drupal-views drupal-6
source share
6 answers

Here is a more reliable way to get the node identifier:

<?php // Check that the current URL is for a specific node: if(arg(0) == 'node' && is_numeric(arg(1))) { return arg(1); // Return the NID } else { // Whatever it is we're looking at, it not a node return NULL; // Return an invalid NID } ?> 

This method works even if you have your own path for node with path and / or pathauto .

Just for reference, if you don't include the path module, the default URLs that Drupal creates are called "system paths" in the documentation. If you enable the path module, you can set custom paths called โ€œaliasesโ€ in the documentation.

Since I always had the path module turned on, at first I was embarrassed that the arg function could always return part of the alias, and not part of the path to the system.

As it turned out, the arg function will always return the system path, because the arg function is based on $_GET['q'] ... After a little research, it seems that $_GET['q'] will always return the system path.

If you want to get the path from the actual page request, you need to use $_REQUEST['q'] . If the path module is enabled, $_REQUEST['q'] can return either an alias or a system path.

+14
source share

For a solution, especially one that includes a mid-way view argument, such as department/%/list , see the Node ID blog post as View Argument from an SEO-oriented URL .

+7
source share

In the end, this snippet did the job - it just deleted the clean URL and provided the last argument.

 <?php $refer= $_SERVER ['REQUEST_URI']; $nid = explode("/", $refer); $nid = $nid[3]; ?> 

Given the response to the comment above, it was probably reduced to this using the Drupal arg() function to get the request part of the path:

 <?php $nid = arg(3); ?> 
+2
source share

There are several ways to do this:

  • You can make your blocks with Views and pass nid through an argument.

  • You can manually pass nid by accessing the $ view object using the following code. This is an array in $ view-> result. Each row in the view represents an object in this array, and a nid in this object for each of them. So you can run foreach on this and easily get a complete list of all the lines in the view.

The first option is much simpler, so if it suits your needs, I would go with that. Hope this helps!

+1
source share

You should consider the panel module. This is a very large module and requires some work before you can really use its potential. Therefore, take this into consideration.

You can use it to customize a page containing several views / blocks that can be placed in different regions. It uses a concept called context, which may be something related to what you are viewing. You can use this context to determine which node is being viewed, not only change the blocks, but also the layout. It is also a little cleaner since you can port PHP code from the admin interface.

On the other hand, it is also written by the author of the views.

+1
source share

This topic is quite old, I see. To freshen up something new about Drupal 7, the correct way to get node id uses the menu_get_object () function;

examp:

 $node = menu_get_object(); $contentType = node_type_get_name( $node ); 

Drupal 8 has a different method. check this

arg () is deprecated

Hope this helps!

0
source share

All Articles