0

This code in my node template checks a CCK field and prints some text dependent on the value:

<?php if ($node->field_stock[0]['view'] == 'No' ) : ?>
<div id="out-stock-my"><h2>Out of Stock</h2></div>
<?php endif; ?>

How can I do something similar with Views Custom Field's PHP filed type? Thanks

3

If 'm not mistaken you are trying to move this logic out of your tpl.php template file by using views custom field.

You first need to get the name of the accessible fields with this code.

<?php print var_export($data, TRUE); ?>

Then you should be able input in the custom field something similar to this.

<?php if ($data->field_stock == 'No' ) : ?>
<div id="out-stock-my"><h2>Out of Stock</h2></div>
<?php endif; ?>

There is more info here http://drupal.org/node/467190.

1

in templates of views you can seek/use the id of the node (nid).

for example, in this case I will use views-view-unformatted.tpl.php

<?php
$nodeIdItem = $view->result[$id]->nid; //get the id of the current item, in this case using $view->result inside the view results loop. 
$node = node_load($nodeIdItem);
?>    
<?php if ($node->field_stock[0]['view'] == 'No' ) : ?>
<div id="out-stock-my"><h2>Out of Stock</h2></div>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.