4

I am currently trying to eliminate my need for the panels module from a site I am working on and don’t have a lot of PHP experience. How would I go about changing the way a node is displayed based on an argument added to a URL? (E.g. http://example.com/node/123/argument.) I already have the node type themed but I need to display the node in a different form based on the argument. Thanks in advanced for your help.

0

Since I didn't want to use Views to solve this. I came up with this solution:

I discovered that a simple if statement combined with preg_match, Drupal's check_plain (for security) did the job nicely. Once the conditions were set up, you can then use theme_hook_suggestions to make a nice logical nomenclature for the tpl.php files. The following code is inserted into the template.php file for your theme:

    function THEME_preprocess_node(&$variables, $hook)
    if (preg_match('/\/ARGUMENT$/', check_plain(current_path()))){
$variables['theme_hook_suggestions'][] = 'node__' . $variables['type'] . '__ARGUMENT';
    }

To use this script, replace the words THEME (with your theme's machine name), ARGUMENT (with the word you wish to use as the argument.

This will then use node--type--ARGUMENT.tpl.php for theming any page whose URL ends in /ARGUMENT.

0

one solution is to use Views. Create views page, for path add node/%/argument, choose Content on Format Show and View mode you want. See Entity view mode module for ability to add more View modes per entity. Add one to Article named "Custom view" for example. Add next code in template.php (change THEME to your maschine theme name).

function THEME_preprocess_node(&$variables) {
  $variables['theme_hook_suggestions'][] = 'node__' . $variables['view_mode'];
}

Then copy node.tpl.php to node--custom-view.tpl.php in current the dir - if your already add new view mode with name "Custom view" and edit node view template as you want.

To have node--Content-Type--View-Mode.tpl.php like node--article--custom-view.tpl.php the above code must be

function THEME_preprocess_node(&$variables) {
  $variables['theme_hook_suggestions'][] = 'node__' . $variables['type'] . '__' . $variables['view_mode'];
}

EDIT:

Programmaticaly solution with code in custom module:

function HOOK_menu() {
   $items['node/%/argument'] = array(
    'page callback' => 'node_custom_view',
    'page arguments' => array(1, 2),
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function node_custom_view($arg1, $arg2) {
  $content = '';

  if (is_numeric($arg1) && $arg2 == 'argument') {
    $node = node_load($arg1, $vid = NULL, $reset = FALSE);
    $content = drupal_render(node_view($node, $view_mode = 'custom_view', $langcode = NULL));
  }

  return $content;
}

"custom_view" is our custom display for node created by Entity view mode module, then we enable this for content type from Manage display

  • Is it possible to do this without views? – Ryan Sep 10 '12 at 20:27
  • yes. views give us path and render content, without views we need to create path and render content other way. programmaticaly this look like: see EDIT on my answer above – svetlio Sep 11 '12 at 11:58
  • Thank you for your answer. Custom modules are a bit out of my abilities at the moment but I have come up with a means of modifying template.php and using .tpl.php files. – Ryan Sep 12 '12 at 2:13
0

Use the ThemeKey module. Here is a quote about it (from the module's project page):

... allows you to define simple or sophisticated theme-switching rules which allow automatic selection of a theme depending on current path, taxonomy terms, language, node-type, and many, many other properties. It can also be easily extended to support additional properties exposed by other modules. In combination with Drupal's theme inheritance you can easily achieve features like:

  • individually-styled channels
  • a front-page / "splash" screen
  • a date/time-selected Christmas theme
  • mobile themes for different auto-detected mobile devices
  • special themes for "limited" or "old" browsers
  • content, user, or role -specific themes
  • indicating your environment (production, staging, testing, sandbox, … )
  • testing your redesign safely on a live server

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.