0

I have a problem concerning a landing page and 404 errors.

Context : I have a content type "news", with auto url /news/my-news-title. I have created programmatically a kind of homepage for news, with /news as URL

What's wrong : My problem is that if I have a link like /news/aiejaziejfnjdfnj (it doesn't exist), I don't have a 404 error and this is my /news landing page which is shown. What I wish, with you help, is to have a 404 error when I have a non existant URL.

I'm going to show you what I have done, and hope sincerely you'll have an idea of what i'm doing wrong. In a custom module, I have :

function mymodule_menu(){
    $items = array();
    $items['news'] = array(
       'title' => "Les news",
       'page callback' => 'landing_page',
       'access arguments' => array('access content'),
    );
    return $items;
}
function landing_page(){
    return "nothing";
}

Then, I have a template page--news.tpl.php in which I have write things manually without showing $content (the "nothing").

So, why /news/aizejziej is accepted and is showing /news ? Is it function landing_page() which is doing something bad by always returning something ?

Thanks in advance for your help/ideas :)

Edit : Is it possible to create a node with /news as alias, and then theme it with page--node--[node-id].tpl.php (or kind of) ? I'll didn't have my bad hook_menu and landing_page thing...

  • if (arg(1)) { drupal_not_found(); }? To answer So, why /news/aizejziej is accepted and is showing /news ?, that's just how the menu system works. aizejziej is considered a parameter to the news route – Clive Feb 12 '15 at 15:32
  • To test arg(1) is a good idea :) Thanks ! I wasn't thinking that it doesn't concern the true nodes path because of the non call of landing_page... Thanks again :) – Seldell Feb 12 '15 at 15:53
2

Like @Clive said, Drupal will try to resolve that path if it can. Just put a check in your page callback to see if there's an extra argument. If there is and it's valid the menu router will handle it. Otherwise it will fall back to your defined path, where if it finds something after /news, it will return drupal_not_found(). The 'return' is important, otherwise you might see the page render twice.

function MYMODULE_menu(){
    $items = array();
    $items['news'] = array(
       'title' => "Les news",
       'page callback' => 'landing_page',
       'access arguments' => array('access content'),
    );
    return $items;
}

function landing_page(){
    if (arg(1)) {
        return drupal_not_found();
    }

    return 'something';
}
  • Yep it works much better with this test. Thank you :) – Seldell Feb 12 '15 at 15:54

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.