3

I created a simple download module to redirect a user when they click the download button to another page. Now I need to change the the page title every time a new download is triggered.

This is my code.

function custom_download_menu() {
  $items['download-code'] = array(
    'title callback' => 'custom_download_page_title',
    'page callback' => 'custom_download_view',
    'access arguments' => array('access content'),
    'access callback' => TRUE,
  );

  return $items;
}

function custom_download_page_title() {
  if (isset($_POST['title'])) {
    $title = "Downloading " . $_POST['title'] . " Code";
  }
  else {
    $title = "Download Code";
  }

  return $title;
}

But most of the time, the title does not change. It seems that it is using the title that is cached before.

I already added the following code so the page will not be cached:

drupal_add_http_header("Last-Modified", gmdate("D, d M Y H:i:s") . " GMT");
drupal_add_http_header("Cache-Control", "no-store, no-cache, must-revalidate"); // HTTP/1.1
drupal_add_http_header("Cache-Control", "post-check=0, pre-check=0", false);
drupal_add_http_header("Pragma", "no-cache"); // HTTP/1.0
drupal_add_http_header("Expires", "Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

Am I missing something?

Update:

<form id="form1" name="form1" method="post" action="/download-code">
    <input type="hidden" name="title" id="title" value="[node:title]" />
    <input type="hidden" name="download_code" id="download_code" value="[node:field_code_file]" />
    <input type="hidden" name="uri" id="uri" value="[node:url:relative]" />
    <input type="submit" class="downloadButton" value="Download Code">
</form>

2 Answers 2

3

You're missing the title arguments key in hook_menu

e.g:

/**
 * Implements hook_menu
 */
function custom_download_menu() {
  $items = array();

  $items['download-code/%'] = array(
    'title callback' => 'custom_download_page_title',
    'title arguments' => array(1), 
    'page callback' => 'custom_download_view',
    'access callback' => TRUE,
  );

  return $items;
}

/**
 * hook_menu title callback
 */
function custom_download_page_title($file) {
  if (isset($file) {
    $title = "Downloading " . $file . " Code";
  }
  else {
    $title = "Download Code";
  }
  return $title;
}

If the download link is

download-code/my-file.txt

The page title should then reflect that.

3
  • I'm using $_POST method and not $_GET. So this code still does not work for me.
    – jaypabs
    Commented Oct 5, 2013 at 5:25
  • You're using HTTP POST to download a file? you should probably use GET Commented Oct 5, 2013 at 5:27
  • I can't use HTTP GET because I am using the code above. Please see my update on my post. If I will use GET the file will reveal. I use this because I don't know how to get the file using NID.
    – jaypabs
    Commented Oct 5, 2013 at 5:31
0

Instead of a title callback, in the page callback for the download-code page, put the check and set title using, drupal_set_title.

Put this code at the top of the callback function:

if (isset($_POST['title']))
        $title = "Downloading " . $_POST['title'] . " Code";
    else
        $title = "Download Code";
drupal_set_title($title);

Please make sure you clear menu cache and check to see if this works

8
  • No. There is a value in the POST method. Please try to look at my site at bit.ly/1fPIyst. Click the download button at the bottom of the page and look at the Title of the page.
    – jaypabs
    Commented Oct 5, 2013 at 3:44
  • just had a look, when directly accessing "download-code" page the title is "Download Code" and when reaching through Download button the title is "Downloading Calculator - My First Basic4Android Application Code". This is what you require no?..Are you getting some different behaviour?
    – Neo
    Commented Oct 5, 2013 at 3:53
  • When you reach the /download-code you will see a "Downloading Calculator - My First Basic4Android Application Code" below the "home" menu. But what I mean is the Title of the page. Look at the title bar of your browser and you will see that the title is "Downloading TTJ- Inventory System Code | Free source code, tutorials and articles"
    – jaypabs
    Commented Oct 5, 2013 at 3:58
  • To make sure that you understand what I main, access the page source of download-code and look at the title tag within the head tag. The value is: <title>Downloading TTJ- Inventory System Code | Free source code, tutorials and articles</title>.
    – jaypabs
    Commented Oct 5, 2013 at 4:00
  • I saw this. ok let us try this approach. I updated my answer.
    – Neo
    Commented Oct 5, 2013 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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