3

I'm trying to replace the output of $submitted in my nodes. I have found how to replace the value which is fine. The problem I'm facing is how to load a template file properly.

function pod_preprocess_node(&$vars) {
  $vars['submitted'] =  t('!datetime ', array(
    '!datetime' => include_once(__DIR__.'/system/submitted.tpl.php')
  ));
}

As you can see above, I've just included a template file and in there I can use $vars but this is clearly sub optimal. What I'd like to do is just load a template file from my theme which has some sensibly named variables in.

I've looked at hook_theme() which now allows me to call theme('pod_submitted', $vars) but I don't know how to make pod_submitted() include the right file etc.

I assume that somewhere I need to do the following

$vars['theme_hook_suggestions'][] = 'system__submitted';

But I'm not sure how my template function relates to/knows which template to call.

2
  • It sounds like you're treating 'submitted' as a field, but it is not a field. It's just a variable defined in the node module. As I think you know, it doesn't contain much but text giving the username and date it was submitted. Tell us why you want a separate template to display that info, and the smart people of drupal.stackexchange might give you a better way to architect a solution.
    – Evil E
    Commented Aug 23, 2013 at 23:34
  • I need to put some HTML in the submitted variable consistently across all nodes, even those with overridden templates. For example I can split the various bits of information in $submitted into lots of variables and output them in node.tpl.php individually but I want to just change the output across the board. At the moment my HTML is in my hook_preprocess_node() method which is not ideal. I want to put it in a template file so that it can be themed more appropriately. Commented Aug 26, 2013 at 12:53

2 Answers 2

5
+50

Well, what you're looking for is hook_theme. With hook_theme you can declare new theme implementations, which can be either functions or templates. I'll stick with templates for this answer. Just define it in YOURTHEME's template.php file:

function YOURTHEME_theme($existing, $type, $theme, $path) {
  return array(
    'YTsubmitted' => array(
      'template' => 'YTsubmitted',
      'variables' => array('whom' => NULL, 'when' => NULL),
    ),
  )
};

I'm calling the theme hook YTsubmitted and the template file YTsubmitted. Those names don't need to be the same, but I'm prefixing to avoid collisions. Now, whenever you call theme('YTsubmitted'), Drupal will look for YTsubmitted.tpl.php in your theme and use it for rendering. You can pass variables to it when calling, just like theme('YTsubmitted', array('whom' => 'Jake the dog')). Variables will default to values set when defining the theme hook in hook_theme (NULL in our example). But you can also preprocess variables:

function YOURTHEME_preprocess_ytsubmitted(&$vars, $hook) {
  $vars['extra'] = 'Mathematical!';
}

And now you can use it in your template file (YTsubmitted.tpl.php):

<p>Submitted by <?php print $whom ?> right after <?php print $when ?>. <?php print $extra ?>!</p>

That's precisely how Drupal sets several of the templates and its variables. For instance, node.tpl.php will print the $submitted variable, which can be redefined in your template.php file:

function YOURTHEME_preprocess_node(&$vars, $hook) {
  $vars['submitted'] = theme('YTsubmitted', array('whom' => 'Finn the human'));
}

AAAaaand you're done.

1
  • DO NOT FORGET TO CLEAR CACHES! Commented Aug 28, 2013 at 4:41
4

Is system/submitted.tpl.php in your theme directory?

Your method is not the correct way to accomplish what you are trying to do. I hope this clears it up.

hook_preprocess_node is pretty close. Do override the 'submitted' variable. Do add extra variables (you can name them how you please). Don't include a template file.

function themename_preprocess_node(&$vars) {
  $vars['submitted'] = "foo";
  $vars['foo'] = "bar";
  $vars['bar'] = "xyz";
}

Template override. You don't need to add a submitted template. Simply copy the node.tpl.php file from either your parent theme's directory, or the node module in core, and place it in your theme directory. Flush you caches. Edit that file and make your changes in there. For example adding in the $foo variable from above (note: All $vars['foo'] are exposed both as $vars['foo'] and $foo for convenience).

cp modules/node/node.tpl.php sites/all/mytheme/node.tpl.php

--

  <?php if ($display_submitted): ?>
    <div class="submitted">
      <?php print $submitted; ?>
      Foo:
      <?php print $foo; ?>
      Bar:
      <?php print $bar; ?>
    </div>
  <?php endif; ?>
4
  • The problem with this is that I have lots of overridden node--content-type.tpl.php files which means that everywhere I need to do this. If i make a change to it, then i have to edit the markup in each node-- tpl override. I would like to abstract the theming of the output of $submitted in order to allow a single place to update the markup. At the moment my markup is in my theme_preprocess_node() function. Commented Aug 26, 2013 at 12:54
  • The above answer is correct. You can add html tags and conditions to the $sumitted output as you like. There is no need to do these on your tpl.php file, this preprocess function is fine. If you need a template for a variable you need to create a module and use hook_theme. An example here. In any case if you have hard customized $sumitted on your node.tpl.php files you can do nothing else that changing this code. Commented Aug 26, 2013 at 19:00
  • I think if you wanted you could do some sort of include in all your node--*.tpl.php files. Then make your submitted changes in there. I've not done that but it shouldn't be too hard. What about this? drupal.org/node/1056074#comment-4075372 Commented Aug 28, 2013 at 22:21
  • There's no need for includes if you can use hook_theme to create new templates. I don't think includes will play nice with sub-themes, for instance. Commented Aug 30, 2013 at 11: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.