I see that in views the Rewrite the output of this field option affects all rows. Is there a way, using php or something, to rewrite the output of row #4 only?
3 Answers
Put this code in your theme's template.php file:
function THEME_preprocess_views_view_fields($vars){
dpm($vars);
if ($vars['view']->name == "VIEWNAME" && $vars['view']->current_display == "DISPLAY" && $vars['id'] == 4){
$vars['fields']['title']->content = 'blahblah';
}
}
Clear the cache.
Replace VIEWNAME, DISPLAY and 4 with your appropriate values.
Then inspect the $vars['fields'] with dpm output and rewrite the output how you want for the field in question. You may use 'raw' value, but remember to sanitize.
-
-
-
"dpm($vars];" causes my page to go blank and I have devel enabled. Feb 19, 2015 at 6:04
-
-
1All the fields are on available in $vars['fields']. If it is not, then add it to the view, or use global $user for user, or load it. Up to you. Feb 19, 2015 at 7:12
Use the view template file and use $id
variable. If you are using $id
variable then you will get id 4 for row 4.
just thought of a solution using the views php module.
I added a view result counter field and then a php field which I put:
<?php
if ($row->counter == 4) {
print 'HELLO THERE!';
}
else {
print $row->title;
}
?>
After reading the accepted answer to this question What are the downsides of using 'custom' PHP code in blocks, nodes, views-args, etc? which was mentioned by f0ns (see comment below)
The biggest reason being:
If your CMS allows execution of PHP then an attacker who finds a security vulnerability of XSS or privilege escalation can now use your server for enormously malicious things (as part of a DDOS, sending spam, hosting malware, hacking into other sites/databases on the server, hacking into other servers on the network that might be behind firewalls). In addition to making small vulnerabilities more painful, this makes the site a more likely target of attack if its known that it can be used to execute php.
Thus, I decided to go with the answer I accepted, which does not use Views PHP
-
1Views PHP module puts your code in DB which is kinda bad (drupal.stackexchange.com/questions/2509/…), might be better to put your code in a hook_view_pre_render function in a custom module. Feb 18, 2015 at 7:32
-