I had the need to process an input filter on another views field (a plain text field). So I had the idea to use the markup field with the rewrite option. But I had to recognize that the rewrite option is processed after the markup check was performed. The value field itself has no option to use replacement patterns within it.
So I wrote a patch to extend the markup field with the option to use replacement patterns within the Value field.

I attached the patch and a screenshot.

Hope someone will review this patch.

Comments

johnmullin2003’s picture

I really hope that this feature will be embedded in the next module release!!!

it is so important!!

fabianx’s picture

+1

works great for me.

Best Wishes,

Fabian (LionsAd)

msti’s picture

Excellent!
This worked for me too!
I hope that this feature will make it into the next release.

intyms’s picture

Status: Needs review » Reviewed & tested by the community

It works for me too.

interestingaftermath’s picture

Can you please expand this to the PHP Customfields?? When I use the Markup field and change the input to PHP it prints the raw text if I choose to use replacement patterns.

derhasi’s picture

Do you mean the PHP Input filter or the module's PHP Custom Field?
In the module's custom field, you should be able to use variables for the other fields' values, so I think there is no need to implement the replacement patterns.

bartclarkson’s picture

I had need for phpcode to return a string that can itself include tokens of other fields defined previously in the view.

Putting logic into any .tpl.php file is outside the desired trend, and this should include view override files.

The weakness of the $data variable, of itself, is that it doesn't include the finalized style rendering of anything relating to an FID (various filefield-driven output).

The following revised function in views_customfield_handler_field_phpcode.inc of 6.x-1.0 met the stated need.


  function render($values) {
    $d = $this->view->display[$this->view->current_display];
    $field_handlers = $d->handler->handlers['field'];
    foreach ($field_handlers as $h) {
      // Handle grouped fields
      if (is_a($h, 'content_handler_field_multiple') && $h->defer_query) {
        // content_handler_field_multiple stores its query results in $obj->field_values
        $tmp = array();
        if(!empty($h->field_values[$values->{$h->field_alias}])) {
          foreach ($h->field_values[$values->{$h->field_alias}] as $k => $v) {
            $tmp[$k] = $v;
            // clean up a bit
            unset($tmp[$k]['#delta']);
            unset($tmp[$k]['_nid']);
          }
        }
        $values->{$h->table.'_'.$h->field} = $tmp;
      }
    }

    $alter = array(
      'alter_text' => $this->options['alter_value'],
      'text' => $this->render_phpcode($this->options['value'], $this->static, $values),
    );
    $tokens = $this->get_render_tokens($alter);

    return $this->render_altered($alter, $tokens);
  }

An example of one of my values for a Customfield: PHP code is the following:

if (!empty($data->node_data_field_video_asset_field_video_asset_fid)) { 
   print '[field_video_asset_thumbnail]';
} 
else {
    print '[field_video_ftp_fid]';
} 
derhasi’s picture

I'd suggest to open a new issue for the PHP Custom field, so the above patch in #0 could be commited independently.

@bartclarkson - maybe you could post a patch for the PHP Custom Field?

ekbiddle’s picture

A patch for the PHP custom field to do this would be amazing.
I'm actually running into something that would greatly benefit from it, using the 5 generated thumbnails in flash video to actually show up in a view, but it requires some php and the [fid] to work, at least as far as I can figure out how to do it...

binaryreleases’s picture

If anyone can help me on this one it would be useful.

autoplay controls>
Your browser does not support the audio element.

i'm trying to get HTML 5 audio to work (the code above) with filefield. I have a filefield field one space up from Views Custom field and have installed the patch. What seems to happen though is that some how something strips all of the HTML back to

Your browser does not support the audio element.

When I put the example in from here (https://developer.mozilla.org/En/HTML/Element/Audio) with replacement patterns turned off I can get it to work fine with firefox and safari fails as is expected.

autoplay>
Your browser does not support the audio element.

However if I replace the url with the replacement code both safari and firefox fail with the code some how being stripped back to Your Browser regardless of whether HTML is being filtered by input filters or not.

If anyone has an idea on how to fix this I would love to know what it is.
thanks

binaryreleases’s picture

If anyone can help me on this one it would be useful.

<audio src="[field_mp3_fid]"
       autoplay controls>
  Your browser does not support the <code>audio

element.
/audio>

i'm trying to get HTML 5 audio to work (the code above) with filefield. I have a filefield field one space up from Views Custom field and have installed the patch. What seems to happen though is that some how something strips all of the HTML back to

Your browser does not support the <code>audio element.

When I put the example in from here (https://developer.mozilla.org/En/HTML/Element/Audio) with replacement patterns turned off I can get it to work fine with firefox and safari fails as is expected.

<audio src="http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg"
       autoplay>
  Your browser does not support the <code>audio

element.

However if I replace the url with the replacement code both safari and firefox fail with the code some how being stripped back to Your Browser regardless of whether HTML is being filtered by input filters or not.

If anyone has an idea on how to fix this I would love to know what it is.
thanks

alanom’s picture

---edit after trying out bartclarkson's code snippet

Brilliant! It just works: with this, replacement patterns can be used in PHP fields.

Regarding the importance of being able to do this - as well as what bartclarkson explained about supporting Filefield fields, there are also many Views-generated fields that simply don't show up in $data exports at all. For example, Global Custom Text and Lightbox triggers. Without replacement patterns, we have to re-invent these processes in hard-code PHP, and duplicate this code in every PHP where it's needed customfield.

More importantly, with replacement pattern support, every module that integrates with Views will work with PHP Customfields. Without it, we need to rely on every future module contributor to create appropriate $data variables.

Will White’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.65 KB

This patch is a bit more radical, but it simplifies the code significantly and fixes several bugs by extending the existing views views_handler_field_custom handler.

intyms’s picture

@Will White
Thank you for patch !
I applied it to the dev version. I tried to find the "replacement patterns" but i didn't find them.
Let me know please, how can i help to test the patch from #13. What is the scope (purpose) of that patch.
Once again thanks a lot for your contribution to this module!

steveoliver’s picture

While I can include tokens in php code, I cannot transform them - they stay just as they are - regardless of strtolower(), etc. functions applied to them... Any ideas?

-Steve

inforeto’s picture

As a workaround, can use pairs of markup fields as 'wrappers', exclude from display and use them as tokens.

HippoOnDiet’s picture

BART!!! http://drupal.org/node/810190#comment-3109330 Your code is working on my side :)
Thank you for your code I put it in and it just display the replacment pattern.
Thanks again.

What I did, I changed On line 132-134:
function render($values) {
return $values->{$this->field_alias};
}

To your code.

Thanks again!

gstout’s picture

+1 this is invaluable.

joecanti’s picture

+1 the patch in #13 works great, but for some reason the fields which have a relationship arent displayed and I get an error - would this be a patch related problem or a more general views customfield problem?

Thanks, Joe

interestingaftermath’s picture

subscribe

charles.holtzkampf’s picture

Wondered if someone could assist me, I have no PHP knowledge so im struggling a bit.
I have installed the - To Do List Module - http://drupal.org/project/to_do
I created a view with the following fields:
User: Uid (excluded from display)
Node: Title
To do list: Deadline
To do list: Priority
To do list: Start date
To do list: Status
To do assigned users: Assigned user ID (excluded from display)
To do list: Buttons (excluded from display)
Customfield: PHP code

The following Tokens/Replacement Patterns are available based on the above fields

* [uid] == User: Uid
* [title] == Node: Title
* [deadline] == To do list: Deadline
* [priority] == To do list: Priority
* [start_date] == To do list: Start date
* [item_status] == To do list: Status
* [uid_field] == To do assigned users: Assigned user ID
* [buttons] == To do list: Buttons
* [phpcode] == Customfield: PHP code
* [markup] == Customfield: Markup

What I would like to accomplish is:

As, the button field has been excluded, I am trying to use the Customfield, to display that button. But only display that button if the current user is the author of the To Do node.

What ive aded to Customfield PHP code

global $user;
$data->to_do;
if ($user->uid == $data->uid_field)
{
print '[buttons]';
}

What's happening
Nothing displays using the above code.

I know the [buttons] token works because when I use the following code, it displays the button.

print '[buttons]';

So I assume something needs to be added so that the system can decide whether the current logged in user is the author of the node. Both the [UID] & [UID_FIELD] always display the same value if I ouput it, so I thuogh that would work but it doesn't.

Any help would be very much appreciated.

Charles

charles.holtzkampf’s picture

Worked it out:

global $user;
if ($user->uid == $data->to_do_assigned_users_uid)
{
print '[buttons]';
}
petarb’s picture

I am trying to output a Gmap using this revised module. So far it works as expected (great work!) however I have run into a snag...

Using the above field replacements I can render [latitude] & [longitude] as text within php. For example:

<?php
print '[latitude]';
print '[longitude]';
?>

However, the following doesn't seem to be working:

<?php
print gmap_simple_map('[latitude]', '[longitude], '', '', 7, '200px', '150px', FALSE,'');
?>

A long thin map appears, whatever size I put in on the Gmap, and I cannot see a marker, nor is it centred properly.

I am not a coder, so any help is appreciated. Thanks.

petarb’s picture

I overcame my issue above by using Gmap Field, however, the issue above in itself remains unresolved.

Punk_UnDeaD’s picture

in views_customfield_handler_field_markup.php
change
return check_markup($value, $this->options['format'], FALSE);

return check_markup(html_entity_decode($value), $this->options['format'], FALSE);

gstout’s picture

Could this patch be included in the main release?

iantresman’s picture

Yes please! Using tokens in Markup and/or PHP code would be easier for us novices... as long as you can also provide a list of available tokens.

3dloco’s picture

+1

JesseNahan’s picture

I love using Custom File and appreciate tremendously the module and all related contributions. Bart's patch in #7 is terrific and working well. Thanks for that!

However, I'm having the same issue as steveoliver in #15. I'd like to be able to assign a token to a variable then manipulate that variable or use it in an if statement. A simple example where I'm retrieving a user's role (token is [rid]) in a view:

$role = '[rid]';
print $role."
";
print substr($role,0,5);

The "print $role" results in printing the user's role(s) in the view. However, printing the substring results in "[rid]" being displayed. In this example, the actual value of $role remains "[rid]" no matter what I do to manipulate the variable.

dkingofpa’s picture

Sub

fabianx’s picture

Interesting progress here.

joelstein’s picture

StatusFileSize
new1.29 KB

Here is a two line patch which adds support for both Markup and PHP code fields.

JesseNahan’s picture

Thanks for the patch. Perhaps I am applying the patch incorrectly, but I am not seeing a difference when I try to load a token into a variable and manipulate it. (Please see my note #30 above.) Was that an issue you were trying to address?

I'm grateful for any contribution to this terrific module!

joelstein’s picture

JesseNahan: You're correct. I wasn't thinking about how to manipulate the tokens, but simply read them in. My patch adds the tokens in after the PHP is evaluated. That is because the PHP is evaluated before the rest of the individual fields are rendered, which means the token replacements will be empty. We can't really swap them out without rewriting the Views integration, and I don't understand enough about the module to try to tackle this. Sorry!

YK85’s picture

subscribing

moheshmohan’s picture

hi everyone

i am new to drupal and this is my first post in drupal.org so plz forgive me if this is stupid

i really appreciate the custom field module and its awesome...

i have used the patch #7 Posted by bartclarkson and its working fine.

like jesse in #30 i too wanted the token to be assigned to a php variable so i made some modification to bart's patch as follows

  function render($values) {
    $d = $this->view->display[$this->view->current_display];
    $field_handlers = $d->handler->handlers['field'];
    foreach ($field_handlers as $h) {
      // Handle grouped fields
      if (is_a($h, 'content_handler_field_multiple') && $h->defer_query) {
        // content_handler_field_multiple stores its query results in $obj->field_values
        $tmp = array();
        if(!empty($h->field_values[$values->{$h->field_alias}])) {
          foreach ($h->field_values[$values->{$h->field_alias}] as $k => $v) {
            $tmp[$k] = $v;
            // clean up a bit
            unset($tmp[$k]['#delta']);
            unset($tmp[$k]['_nid']);
          }
        }
        $values->{$h->table.'_'.$h->field} = $tmp;
      }
    }
    
//changed 

  $alter = array(
      'alter_text' => '',
      'text' => '',
    );
    $tokens = $this->get_render_tokens($alter);
	
$values->{'available_tokens'} = $tokens;
 
 return $this->render_phpcode($this->options['value'], $this->static, $values);
  //End of changes
  }

so basically it gives all the possible replacement tokens and its values in $data->available_tokens
provided custom field should be last one in the view's field list

For the first row the output is correct, but when it comes to next row the replacement token for custom field will have replica of first field and this get added up again and again consuming all memory if there are many records.

my custom field has following code


print_r($data->available_tokens);

and see the output

//first row
Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => http://google.com [[phpcode]] => )

//second row
Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => /projects/asi/content/photos [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => http://google.com [[phpcode]] => ) )

//this get incremented as we go further
Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => /projects/asi/content/photos [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => http://google.com [[phpcode]] => ) ) )
Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => Events [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => /projects/asi/content/photos [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => http://google.com [[phpcode]] => ) ) ) )
Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => Events [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => /projects/asi/content/photos [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => http://google.com [[phpcode]] => ) ) ) ) )
Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => Events [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => /projects/asi/content/photos [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => http://google.com [[phpcode]] => ) ) ) ) ) )
Array ( [[field_tab_menu_link_nid]] => Awards and Orations [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => Events [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => /projects/asi/content/photos [[phpcode]] => Array ( [[field_tab_menu_link_nid]] => [[field_tab_menu_adv_link_value]] => [[field_tab_menu_url_url]] => http://google.com [[phpcode]] => ) ) ) ) ) ) )

so when final row is printed its 'phpcode' is too bulky

so if we could prevent the expansion of custom field's replacement token ie 'phpcode' in '$this->get_render_tokens' then all the fields replacement tokens and values will be available in $data

if i made any mistake plz correct me friends

i guess this is what is being discussed in http://drupal.org/node/467190 post #1

fastforward’s picture

#33 - Perfect One(!)line solution for replacement patterns in Value field (markup)! My JavaScript in Views <a href="#" onclick="document.getElementById('aaa').value = '[bbb]'; return false;">[bbb]</a> works like a charm! Thank You, JoelStein!

smira’s picture

confirming #33 works like a charm so far.
using it with the popup filter type.
+1 commit me thinks...

deggertsen’s picture

Wonderful! Exactly what I needed. Patch in #33 works!

karljohann’s picture

+1

vasrush’s picture

#33
This is a lifesaver patch.
It has to be in the next dev.

soulfroys’s picture

+1 for #33

anybody’s picture

+1 for permanent integration of #33

windmaomao’s picture

#33, once again, this has to be in the next release

iantresman’s picture

Any chance that #33 can be rolled into the next nightly dev?

osopolar’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Works for me too.

deggertsen’s picture

It's about time this gets committed...

iantresman’s picture

+1