1

I have 14 blocks added through Drupal interface, for all of them I was override tpl.php file because I need some changes there (like block--block--1.tpl.php).

What I want to do is to create just one tpl.php file and apply it for all these blocks. So, if I need to change something someday, I change it just in one file instead 14 files.

How I can accomplish that in Drupal 7?

| improve this question | | | | |
2

As per template suggestion for Drupal 7, we have a general format as:

block--[region|[module|--delta]].tpl.php

base template: block.tpl.php

Theme hook suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:

block--module--delta.tpl.php block--module.tpl.php block--region.tpl.php "module" being the name of the module and "delta", the internal id assigned to the block by the module.

Now, in order to override and define your own suggestions apart from these, you can provide new template suggestions in your theme template.php file file using template_preprocess_block as:

function mytheme_preprocess_block(&$variables) {
    $variables['theme_hook_suggestions'][] = 'common_block_template';
}

Now you place(create) this common-block-template.tpl.php file in your themes templates folder and it will be default template for all your blocks and don't forget to clear drupal cache.

Note: This templates will be applied to every block present in your Drupal 7 website i.e. user login block, content block etc.. So make sure you do define logic as per your page requirements as for example if you want to limit this template suggestion for just a uri name mypage, then

function mytheme_preprocess_block(&$variables) {
 if (current_path() == 'mypage') {
  $variables['theme_hook_suggestions'][] = 'common_block_template';
 }
}

UPDATE: To target delta (with numeric values & to avoid custom module block) the were created using Drupal 7 site building block module:

function mytheme_preprocess_block(&$variables) {
    $block = $variables['elements']['#block'];
   if (is_numeric($block->delta)) {
      $variables['theme_hook_suggestions'][] = 'common_block_template';
    }
 }

If you need to target just block which has delta of 1 then:

function mytheme_preprocess_block(&$variables) {
    $block = $variables['elements']['#block'];
   if ($block->delta == 1) {
      $variables['theme_hook_suggestions'][] = 'common_block_template';
    }
 }
| improve this answer | | | | |
  • I'm getting these errors: Warning: array_reverse() expects parameter 1 to be array, string given in theme() and Warning: Invalid argument supplied for foreach() in theme(). Also, can I check if ($variables['id'] == 1) and apply template for block with ID 1 (in this case) only? – mixerowsky Nov 10 '16 at 11:47
  • sorry, I missed it is double dimensional array, It should be $variables['theme_hook_suggestions'][]. Check update. – sarathkm Nov 10 '16 at 11:52
  • Thank you. Errors is gone. Now, I just need to apply this suggestion for block with ID 3,7,8,11...how to do that? I tried to check ID (as I wrote in my previous comment) but template isn't applied. – mixerowsky Nov 10 '16 at 11:59
  • I suppose you mean block--block-1.tpl.php, the numeric 1 here i.e. the delta part. You can check if delta part is numeric or not and apply your suggestion. Check the update – sarathkm Nov 10 '16 at 12:02
  • So, I can also check if (is_numeric($variables['block']->delta) && $variables['block']->delta == 1) to target block with delta 1? – mixerowsky Nov 10 '16 at 12:09
0

Try block--block.tpl.php instead.

| improve this answer | | | | |

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.