2

What I'm trying to achieve is: create a new content of type arquivo, then when accessing the new content url, instead of showing a page with the fields and etc., the file from a field_file is opened/downloaded. Tried to achieve this using a custom template for the content type with no success. The code for the tpl.php is:

<?php
$items = field_get_items('node', $node, 'field_arquivo');
$filepath = $items[1]['uri'];
$path = file_create_url($filepath);
drupal_goto($path); 
?>

Any help is appreciated

1 Answer 1

3

You could make a custom module and put this function in it

/**
 * Implements hook_node_view().
 */
function custom_tweaks_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'arquivo' && $view_mode == 'full') {
    $file = $node->field_file[$langcode][0]['uri'];
    if ($file) {
      $file_uri = file_create_url($file);
      drupal_goto($file_uri);
    }
  }
}
3
  • Updated the function with a check if there is a file present on the node and to only redirect when the full node is being displayed.
    – devunder
    Feb 19, 2017 at 21:36
  • 1
    A simple change that would make it more portable is changing 'und' for $langcode. In case anyone is dealing with a site with multiple languages.
    – rovr138
    Feb 19, 2017 at 22:46
  • But ofcourse. I updated the code @rovr138.
    – devunder
    Feb 20, 2017 at 7:21

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.