drupaldrupal-7drupal-viewsdrupal-blocks

How to customise HTML structure in Drupal view block


I am currently developing a website in Drupal. I have a load of news articles which are of content type 'news'. I want to create a widget to show at the bottom of several pages listing the latest articles. I have managed to achieve thus far by creating the view with the fields I want and then making the view available as a block.

The problem is the format in which the articles are being listed in the view. Ideally I would like to customise the HTML. I understand that I can create a custom template to target views/blocks but I haven't quite got a full understanding of it yet, and find the Drupal documentation to be quite dry and therefore hard to find what I need from.

Any pointers would be helpful.


Solution

  • Go to your view edit page and select "table" as your view mode.

    Go to "Theme information" and look for the desired (style output) template name.

    Copy this name and create a new file in your theme folder using this name. I.e.: views-view-table--view-name-block.tpl.php.

    In this file you can use the following structure (as an example) to get the values of the fields:

    <?php foreach($rows as $row):?>
    <div>
    <h2><?php print $row['title'];?></h2>
    <p><?php print $row['field_custom'];?></p>
    </div>
    <?php endforeach;?>
    

    Don't forget press rescan theme files after you saved the file in the view theme section.

    You can get as specific as you want. Create a block view next to the default or page view and you will see specific file names in the "Theme information" section.