So I have multiple themes running on my drupal site. I'm building a new theme and I'm confused about some of the php calls and looking for help or a place where I can find help.

For example the line in file node.tpl.php: print $submitted
creates "Submitted by IrnBru001 on Thu, 2006-12-21 18:14." in one template
and "Submitted by IrnBru001 on Thu, 2006-12-21 18:14." in another.

I'm not understanding where in the two themes $submitted is being defined

Where/how do I control what that call does in different themes.

Thanks for any help!

IrnBru001

Comments

styro’s picture

Those variables are defined in the PHPTemplate theme engine. It is defined in the phptemplate_node() function in the phptemplate.engine file.

Here is the relevant bit:

<?php
    $variables['submitted'] =  t('Submitted by %a on %b.', array('%a' => theme('username', $node), '%b' => format_date($node->created)));
?>

But don't edit that file directly.

You could either:

1) Just change the PHP in node.tpl.php to print out what you want

or

2) Override the theme_username() function to make the username display differently

http://drupal.org/node/11811
http://api.drupal.org/api/4.7/function/theme_username

Note: If you look at the theme_username() function you'll see that the user name is only printed as a link if the current user has the rights to access user profiles. That is probably why the name displays differently for you.

3) you could override the default values using this technique
http://drupal.org/node/16383

eg:

<?php
function  _phptemplate_variables($hook, $vars) {
   switch($hook) {
     case 'node' :
        $vars['submitted'] = my_new_submitted_text_function();
        break;
   }
   return $vars;
}
?>

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

IrnBru001’s picture

And it seem the third way makes the most sense to me. Now how do I define my_new_submitted_text_function();
lets say I want to make $title presentable on case 'comment' with out and HTML formatting.
I get

function  _phptemplate_variables($hook, $vars) {
   switch($hook) {
     case 'comment' :
        $vars['title'] = my_comment_title_function();
        break;
   }
   return $vars;
}

But I don't get the next step which is how I define, in this case, my_comment_title_function()

styro’s picture

Note I haven't tested whether or not those values get generated correctly, so I'm not sure it will work verbatim but you should get the general idea.

You don't need a function, you could just assign the value to $vars directly in your template.php file - eg:

<?php
function  _phptemplate_variables($hook, $vars) {
   switch($hook) {
     case 'node' :
        $node = $vars['node'];
        $vars['submitted'] = t('submitted by: %name', array('%name' => $node->name));
        break;
     case 'comment' :
        $comment = $vars['comment'];
        $vars['title'] = $comment->title;
        break;
   }
   return $vars;
}
?>

Or if you want more decisions made about what to return and when, define some theme specific functions in your template.php file:

<?php
function  _phptemplate_variables($hook, $vars) {
   switch($hook) {
     case 'node' :
        $node = $vars['node'];
        $vars['submitted'] = yourthemename_get_node_submitted_text($node);
        break;
     case 'comment' :
        $comment = $vars['comment'];
        $vars['title'] = yourthemename_get_comment_title($comment);
        break;
   }
   return $vars;
}

function yourthemename_get_node_submitted_text($node) {
  // You could do all kinds of other crazy stuff in here to figure out what to return
  return t('submitted by: %name', array('%name' => $node->name));
}

function yourthemename_get_comment_title($comment) {
  // You could do all kinds of other crazy stuff in here to figure out what to return
  return $comment->title;
}


?>

The "yourthemename_" prefix is so that they won't have naming conflicts with any other functions in Drupal.

or as another option you could define those functions in a module file, so that many different themes could use them - in which case they would have a "yourmodulename_" prefix.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

IrnBru001’s picture

I think this is just over my head at the moment. I don't know enough php. Is PHP in a Nutshell a good book for me to start learning with?

Is there a better book you would suggest?

styro’s picture

Would be a good book if you already have some (not necessarily a lot of) programming experience in another language. It concisely runs through all the PHP ways of doing things, but it doesn't explain programming logic itself.

If you haven't programmed before, you will need another book to learn from. But the Nutshell book could still be a useful reference book.

I wouldn't be able to recommend a PHP learning book because the last one I read was aimed at PHP 3.x, and because I'm really a sysadmin rather than a developer I hadn't used PHP much since. Since starting to use Drupal I've just used the online reference docs at http://php.net to pick it back up - although I have flicked through PHP in a Nutshell a few times. It's a good book, and the only real weakness is that the index is slightly incomplete.

PHP is pretty simple to pick up though, and I find (for the most parts) you only really need the basics to do anything in Drupal. I find you can do almost anything just by understanding functions, arrays, and if statements (barring any SQL of course) - the Drupal API takes more learning effort than PHP itself. You could probably get there with web tutorials if you wanted. I'm sure any "learn PHP book" with halfway positive reviews on Amazon would be more than good enough.

One tip when reading reviews of PHP books is to try and get a feel for how well the authors understand security. A lot of PHP books (especially the early ones) taught appalling security practices.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ