I've written a patch to allow replacement patterns in PHP code fields. This patch is based on discussions/code snippets from an identical issue relating to markup fields: http://drupal.org/node/810190. Credit to bartclarkson for his original post: http://drupal.org/node/810190#comment-3109330.

Example usage:

if ($data->node_data_field_homepage_media_type_field_homepage_media_type_value == "0") {
    print '[field_video_fid]';
} else {
    print '[field_image_fid]';
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

BWPanda’s picture

I needed this functionality and was going to try this patch, but then found this: http://drupal.org/node/467190#comment-3081790

For example, you can use something like this out-of-the-box:

  print $data->field_image_fid;

Does this help? If so, this patch might not be needed...

Rj-dupe-1’s picture

Status: Needs review » Reviewed & tested by the community

Patch looks good, much less invasive than the one over in http://drupal.org/node/810190#comment-3446480 and works well for me. Drupal 6.18, patched custom field 6.x-1.x-dev.

YK85’s picture

subscribing

deggertsen’s picture

subscribing

HAbbas’s picture

FileSize
17.97 KB

Included the above mentioned patch to the latestly available module "Views Custom Field" and has made the whole module availble for others to use. You could directly use this module instead of adding patch separately since the same patch has been updated with this attached module. Thanks for the provided patch. It helped me to resolve an issue since iam novice in drupal.

demonrage’s picture

hello, the patch worked like a charm, but i have a strange problem.

i have a view that grab latest articles, in every article it have a title, thumbnail and a button to like on facebook or twitter, here is the original code:

<div class="social-buttons">
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php $curr_url = check_plain("http://" .$_SERVER['HTTP_HOST'] .$node_url); echo $curr_url; ?>&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px;" allowTransparency="true"></iframe>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="<?php $curr_url = check_plain("http://" .$_SERVER['HTTP_HOST'] .$node_url); echo $curr_url; ?>" data-text="<?php echo $title; ?>" data-via="jerryjr">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</div>

and here is my code

<div class="social-buttons">
<iframe src="http://www.facebook.com/plugins/like.php?href=[path]&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px;" allowTransparency="true"></iframe>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="[path]" data-text="<?php echo $title; ?>" data-via="3alamtaney">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</div>

if i use [path] replacement pattern in the code, it doesn't display the Facebook button completely, and display the tweet link without any graphic or (like counter), but it provide the link path in a correct way.

what i have been missing, i really tried hard to figure it out with no result

demonrage’s picture

actually this patch can't work with custom markup either

lunk rat’s picture

Works great for me, thanks for this! Patched against 6.x-1.x-dev and I am using all sorts of replacement patterns within my custom field PHP and they work flawlessly. This should be committed as it is very nice to be able to leverage the formatters and other standard views features within your custom field logic.

Anthony Pero’s picture

This worked for me, I was able to find the correct field name to use in the $data object by looking at the view code on the bottom of the Display.

Bartezz’s picture

@BWPanda: I've worked with $data->field_image_fid; before and it works for some fields, but I needed the term IDs returned by a Taxnomy: All terms field. A result like this isn't a simple db field return and therefor isn't available in $data object. Through the patch in #1 it becomes available via [tid]...

Works like a charm!! Please commit, it's been half a year...

EDIT: But still not useful in my case :( problem is that the tokens are rendered after the PHP is rendered, so the value of the tokens can't be used in PHP equations...

Cheers