0

I wanted to be able to control the layout and containers for all elements (choice,title,vote etc) inside my poll block and decided to add my own template for the same.

Like any noob, I checked the block with devel and got the block template file,preprocess methods used (in my case poll-vote.tpl inside drupal core poll module and the template_preprocess_poll_vote inside poll.module) and the candidate file name for the template as poll-vote-block.tpl.php inside my themes directory (sites/all/themes/my_theme) and in the theme's corresponding template.php used the preprocess function as follows:

function my_theme_preprocess_poll_vote(&$variables) {
  echo 'Im in';
  $form = $variables['form'];
  $variables['choice'] = drupal_render($form['choice']);
  $variables['title'] = check_plain($form['#node']->title);
  $variables['vote'] = drupal_render($form['vote']);
  $variables['rest'] = drupal_render($form);
  $variables['block'] = $form['#block'];
  // If this is a block, allow a different tpl.php to be used.
  if ($variables['block']) {
    $variables['template_files'][] = 'poll-vote-block-custom';
  }
  print_r($variables['template_files']);
}

Initially I set the template_files value as 'poll-vote-block' and it didn't seem to be affecting the block, which i confirmed with devel again.

I printed the echo message to ensure the preprocess was called and it did print it. I renamed the template_files (and the template file) and printed it to get the following:

Array ( [0] => poll-vote-block [1] => poll-vote-block-custom )

I once came across theme templating in drupal docs that preprocess methods are not necessarily overridden but placed in a queue... to me it appears that my preprocess method's tpl file is set with lower priority and hence unable to override. Hence I'm unable to fulfil my requirement of theming the poll block

It'd be nice know if I'm mistaken with this conception and hope someone would provide me some insight on this to get my problem solved.

Thanks in advance!!

Edit

The problem was finally solved. I created a block-poll.tpl.php file and was then able to override the default poll-vote.tpl.php with my preprocess method and poll-vote-block-custom.tpl.php.

Apparently just like the case where one needs block.tpl.php for his block-module-delta.tpl.php to override a default functionality this might've been my case.

Marking jimajamma's answer as solution as it proves to be a good documentation and reference material worth being a wiki for Drupal theming for novice developers.

Cheers!!

1 Answer 1

1

If all you are just trying to change is the layout of the form, and not the data contained in it, all you should need to do is change the CSS for the various wrappers.

In this example, that would be looking at

<div class="poll">
  <div class="vote-form">
    <div class="choices">
      <?php if ($block): ?>
        <div class="title"><?php print $title; ?>:</div>
      <?php endif; ?>
      <?php print $choice; ?>
    </div>
    <?php print $vote; ?>
  </div>
  <?php // This is the 'rest' of the form, in case items have been added. ?>
  <?php print $rest ?>
</div>

so, if in your theme CSS you added

.poll {
  color: red;
  background-color: blue;
}

this block would now be in red and blue.

and if you added

.vote-form {
  border: 2px solid green;
}

then the form would have a border of green. etc etc etc.

Now, if you need to change things up more, add some more divs or change their order, etc, you can just copy the standard poll template to your theme directory, refresh your caches and drupal will pick it up and override the one from poll itself, no need to name it anything different.

4
  • thanks for the reply mate... i might want to change the order of the components within the block and i tried just the same of copying the template and clearing the cache multiple times, it didn't work... but i'm really curious about what i tried, if it is correct or not... Commented Dec 14, 2011 at 15:29
  • 1
    the preprocess hook's function is to allow a programmer to change or add variables, but all you are doing in yours is setting them to what they would be anyway. for example, if your overridden template needed a variable called foo, you would create a $variables['foo'] and set it to whatever and then $foo would be available in the template. or if you wanted an existing variable wrapped in a div, you could do something like $variables['existingvariable']='<div class="myclass">' . whatever . '</div>'; Do a print_r of $variables at the beginning of your preprocess to see more about what I mean.
    – Jimajamma
    Commented Dec 14, 2011 at 16:52
  • thanks... i'd done that earlier and quite understand the part about setting variables in preprocess methods to be used in tpl files... but shouldn't it have worked for $variables['template_files'] setting the template name that i had decided to have? Commented Dec 14, 2011 at 18:07
  • Yes, it should. Otherwise, it wouldn't pick up the [0] version of it in the first place. Why it's not picking up your custom one gives me pause that something else like a cache isn't being cleared, but you've done all that. Drupal is all about namespaces, so perhaps block needs to be at the end, eg poll-vote-custom-block.tpl.php. Odder things have happened.
    – Jimajamma
    Commented Dec 14, 2011 at 18:22

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.