Tuesday, July 26, 2011

Adding PHP Code in Views - Drupal 6

Thanks to Views Custom Field module which allowed us to write custom php code in Views field.

Comes with three different fields
1) Markup
Field that allows usage of custom text and the input filter system.
2) PHP code
Field that allows usage of custom PHP code (with access to view's database result)
 $data: contains the retrieved record from the database (e.g. $data->nid).
 we can access the fields build in the database query , To know the fields see the build query shown in views preview or add a custom PHP field with <?php print_r($data); ?> will show the available database field, which can be used in our code.
 3) Rownumber
Field containing rownumber (respects pagers).
 Note:
Include <?php ?> delimiters when using PHP code. Available variables:
$static: can be used to store reusable data per row.

Now i am going to show you how Replacement Patterns can be used in Views Custom field.

For example:

<?php

$static = $this->view->display_handler->get_handlers('field');
if (!empty($static['drupal_id']->last_render)) {
echo "success";
}

?>
here drupal_id is the Replacement Pattern
[drupal_id] == Drupal ID

No comments:

Post a Comment