Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

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.

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.

In Drupal 8

enter image description here

The fields template would be:

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

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

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

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)

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top