Tuesday, December 31, 2013

Embed view block or page in code or block

We can embed a views page or block in code or block.

<?php print view_embed_view('view_name', 'block_page_name'); ?>

Apply row style template in Drupal 7

We can design or apply our own design to drupal views (row style) in Drupal 7.

*) Goto Views -> Block or Page view -> Advanced -> Other -> Theme: Information.
*) Create the template file in your custom theme folder. Possible template file name can be created from "Row style output" in Theme: Information.
*) If required, please apply "Rescan template files" button.
*) Once rescan done, your template will be shown as bold.
*) Then press "Row style output" link, copy the code and paste into your template file.
*) Then you can design or apply your own design.

For Example: 
Create views-view-fields--<view name>--<view machine name>.tpl.php in theme folder
And apply the following code

<div class="ownclass">
    <a href="<?php echo $fields['field_link']->content; ?>">
        <?php echo $fields['field_image']->content; ?>
    </a>
</div>

Programmatically Apply image style in Drupal 7

We can set the image style programmatically in Drupal 7.

1. Configuration -> Media -> Image Styles -> Add Style
2. image_style_url('style_name', 'path');

Ref: https://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_url/7
Example:
print image_style_url('thumbnail', $node->field_image['und'][0]['uri']);

Thursday, December 26, 2013

Get rows count in Drupal 7

<?php

$query = db_select('table1')->fields('users')->execute();
$count = $query->rowCount();

?>