18

There is a lot about theming custom blocks, but what about blocks created by views?

My views block has id: block-views-posts-listing-block,

I try many ways for naming template file, ex: block--block--views-posts-listing-block.tpl.php, but I can't find correct file name.

2
  • 1
    The Theme developer module can tell you the name of the template file.
    – Oswald
    Commented Sep 17, 2011 at 16:10
  • Thanks, but I detect that Themer for D7 sometimes doesn't work propeperly.
    – Codium
    Commented Sep 18, 2011 at 8:07

9 Answers 9

20

Edit your view. and Under the "Advanced" settings of your Block details, click on "Theme: Information". A list of options of template files will be displayed.

for example, if your block has a name "posts listing", see possible names listed by "Theme Information" option:

* Display output: views-view.tpl.php, views-view--posts-listing.tpl.php, views-view--default.tpl.php, views-view--block.tpl.php, views-view--posts-listing--block.tpl.php
* Style output: views-view-unformatted.tpl.php, views-view-unformatted--posts-listing.tpl.php, views-view-unformatted--default.tpl.php, views-view-unformatted--block.tpl.php, views-view-unformatted--posts-listing--block.tpl.php
* Row style output: views-view-fields.tpl.php, views-view-fields--posts-listing.tpl.php, views-view-fields--default.tpl.php, views-view-fields--block.tpl.php, views-view-fields--posts-listing--block.tpl.php
* Field Content: Title (ID: title): views-view-field.tpl.php, views-view-field--title.tpl.php, views-view-field--posts-listing.tpl.php, views-view-field--posts-listing--title.tpl.php, views-view-field--block.tpl.php, views-view-field--block--title.tpl.php, views-view-field--posts-listing--block.tpl.php, views-view-field--posts-listing--block--title.tpl.php 

for example: your block has the name posts listing, and has style unformatted, then "Theme Information" suggest the name views-view-unformatted--posts-listing--block.tpl.php (this is only one of several names). you can copy the original views-view-unformatted.tpl.php template file (located in /modules/views/theme) to your current theme directory, and rename this file with the name suggested :views-view-unformatted--posts-listing--block.tpl.php. then make the changes inside your template file. If this template does not allow make your wanted changes, seek other templates according your needs.

In the end, if you add a new template, rescan template files list:

Important! When adding, removing, or renaming template files, it is necessary to make Drupal aware of the changes by making it rescan the files on your system. By clicking "Rescan template Files" button you clear Drupal's theme registry and thereby trigger this rescanning process. The highlighted templates (in your template list above "Rescan template Files" button) will then reflect the new state of your system.

Also Theme Developer Module http://drupal.org/project/devel_themer suggested by Oswald could be useful to detect possible template/s to change specific elements according your criteria. I do not recomend this module enabled in productions enviroments (in my case, even I have disabled the module in test enviroments, because seems to have conflicts with views, but when it has been necessary, has been very useful)

8
  • No it doesn't help with theme views block output. It's only for theme view content, but I want to theme block that wrap my view
    – Codium
    Commented Sep 18, 2011 at 13:41
  • Hello, please review again the list of templates indicated by "Theme Information" in View->Block settings, there are several templates that end with --block in his name. Remember reescan templates, and remember put the template inside directory of your current theme (copy the selected template from modules/views/theme). the template must have the name suggested by the "Theme Information" list. It works.
    – MWt
    Commented Sep 18, 2011 at 16:12
  • @drupality, I have added an example with steps to review the process, please review in "for example" paragraph, in the answer.
    – MWt
    Commented Sep 18, 2011 at 16:33
  • Thanks but I can't remove default div class="content" wrapper with that too. I can only alter html created by views
    – Codium
    Commented Sep 19, 2011 at 10:56
  • 2
    @moon.watcher I think you misunderstand. Ending the template file with "block" doesn't affect the theming of the block itself. It makes that template file specific to the block that the view is outputting (as a view can output multiple blocks from the same view). The themed content from the view is output to the block theming engine, and is wrapped in the markup provided by block.tpl.php. What drupality is trying to do (as well as I am) is change the theming of the block itself.
    – user3337
    Commented Oct 12, 2011 at 19:56
18

For those seeking the answer in D7, according to Theme Developer(aka devel_themer), you can create a tpl override for a view's block using the convention:

block--views--{view's machine_name}-block

Therefore in the example give you will get

block--views-- post-listing -block.tpl.php

Without the spaces (I couldn't edit this properly to allow it all to be inline.

3
  • I confirm noTxt is correct, go to the block section, and edit the block, the url should show you the view's block name with underscore, my_view_name-block. replace the underscores so my-view-name, and use this format: "block--views--my-view-name=block.tpl.php"
    – Nevos
    Commented Jun 16, 2012 at 15:25
  • 2
    Yes, this is more correct answer, so: block--views--VIEWNAME-MACHINENAMEOFDISPLAY.tpl.php Attention to single dash before Machine name! Machine name = by default it's named as block, block-1 and so on, but you can rename it in views UI.
    – Nikit
    Commented Apr 17, 2013 at 2:24
  • how do I get data inside block file?
    – May'Habit
    Commented Oct 26, 2020 at 9:07
7

At the end of half an hour changing tpl names, here is my final conclusion:

block--views--[view-name]-[display-name].tpl.php

[view-name]-[display-name] part can be found like noTxt said or joining with a '-' the id name of the view and the id name of the view display, the -block at the end isn't correct.

6

In Drupal 8

enter image description here

The fields template would be:

views-view-fields--[view-name]--block-1.html.twig

5
  • This question was about D7 ... Why the Drupal 8 in your answer? Commented Aug 20, 2016 at 8:43
  • 1
    @Pierre.Vriens since question is popular in views (not the module), I figured I should add a D8 version answer in here.
    – No Sssweat
    Commented Aug 20, 2016 at 10:18
  • Now that's an interesting idea ... How about doing the same for these +21K questions also then? Commented Aug 20, 2016 at 18:38
  • 1
    @Pierre.Vriens most of those are not popular.
    – No Sssweat
    Commented Aug 20, 2016 at 23:47
  • 1
    8 tag added, nice ;)
    – Codium
    Commented Aug 21, 2016 at 9:21
1

block--views--[view-name]-[display-name].tpl.php should be changed to block--views--[view-name]-[machinename].tpl.php.

You will find the machine name from your views advanced section where it is written other

1

The file name patterns describe above works UNLESS your view name + display name is longer than a certain amount of characters!! I was caught by this and could not find why a template file ending in ...block.tpl.php was working and the following display ...block-1.tpl.php was not working.

It turns out that Views replaces the machine name with an MD5 hash if the name is too long. If you want to be sure of the template name you should be using, add $conf['theme_debug'] = TRUE; in your settings.php and then look at the page source. if you find something like block--views--d131dd02c5e6eec4.... you'd better shorten the machine name of the view (if you manage do to that... It's not the human-readable name). Or else you need to use a template file with that silly name.

see also

Views Block's machine name is being "hashed" (?)

and

https://www.drupal.org/node/346602

0

Check this page https://www.drupal.org/node/1089656, and information about underscores which views uses:

Also one more example with Views, if you have a block created by views with a view name "front_news" and display id "block_1" then the theme hook suggestion would be: block--views--front-news-block-1.tpl.php (notice, when you have underscores in a display id or in a view name - you have to transform them in to a single dash)

0

For blocks inside panel panes, the solution provided by noTxt and other is not working. The syntax would then be (no underscores!):

panels-pane-block--views-[view-name]-[display-machine-name].tpl.php

Thanks to user3074436 for providing the $conf['theme_debug'] setting in order to figure out this exceptional theme overriding case

0

You are on on D7 >= 7.33 you could display theme_suggestion directly in the page as HTML Comments using :

in settings.php

$conf['theme_debug'] = TRUE;

Here is the complete documentation https://www.drupal.org/docs/7/theming/overriding-themable-output/working-with-template-suggestions

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.