0
votes

I am using Drupal 6 and have my own node that is pulling in my custom php page. The URL is domain.com/blogs. I want to use clean URLs for arguments. For example, domain.com/blogs/1 will pull in the information from the database regarding the first blog post.

I've tried:

domain.com/blogs/1 and in the php code use arg(0) BUT I never even get to run the code because Drupal comes up with Page Not Found error.

Is there any way to let Drupal know that any arguments after domain.com/blogs is allowed and to load the node for /blogs also sending the args?

2

2 Answers

1
votes

Aliasing node/1234 to blogs doesn't mean you can control the rest of the menu router item.

For an example, if your unaliased node path is "node/1234" , you "own" the rest of the arguments. i.e You can access 567 from node/1234/567 from PHP like this:

<?php print arg(2); ?>

If you want to "own" /blogs URL part, you have to define it using hook_menu. As you have already touched php, I think the best way would be making the module. You can however use Views or Panels modules for that.

<?php
function MYMODULE_menu() {
  $items = array();

  $items['blogs'] = array(
    'title' => 'Main Blogs page', 
    'page callback' => 'MYMODULE_page', 
    'access arguments' => array('access content'), 
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['blogs/%'] = array(
    'page callback' => 'MYMODULE_blog_view', 
    'page arguments' => array(1),
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK,
  );

  return $items;
}
function MYMODULE_blog_view($blog_id){
// $node = node_load($blog_id);
// return print_r($node, TRUE);
}
?>

Now, in MYMODULE_blog_view function, you can get the arguments from arg(1) as well. You can also make it in a single page callback by combining both menu router items but I'd recommend separate router items.

Update( Aug 8 ) : The reason why I used node_load($blog_id) and other commented junk there is assuming you needed parts from the node (e.g: You want make show a View with the node ID argument, or something similar that needs specific data from the node). But never allow users to view a node loaded from node_load() because it's like god giving whatever the user asks for.

For nodes, let me explain a little gimmic. Put this in the callback function.

  $node = node_load($blog_id);
  if ($node && $node->type == 'blog'){
    $_GET['q'] = 'node/' . $node->nid;
    return menu_execute_active_handler('node/' . $node->nid, FALSE);
   }
   drupal_not_found(); // important!

This will load the node ID from the blog ID (which is from the URL), check if it's in 'blog' type and sets the menu handler to the relevant node ID path. Now, when you access blog/123 , you will see content of node/123 if it's a blog node. But it's not a redirection. It will not work as fully aliased URL but is helpful sometimes.

Alternately you can check if theuser has access to the node from node_access().

0
votes

here some links you can read about Defining and sending Views Arguments drupal.org/1

drupal.org/2