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 3


Put this code in your theme's template.php file:

function THEME_preprocess_views_view_fields($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.

  • I'm not familiar with dpm, do I need to install the devel module?
    – No Sssweat
    Commented Feb 18, 2015 at 6:07
  • Yes you need to install devel. Commented Feb 18, 2015 at 14:41
  • "dpm($vars];" causes my page to go blank and I have devel enabled.
    – No Sssweat
    Commented Feb 19, 2015 at 6:04
  • Small typo: needs to be dpm($vars); Commented Feb 19, 2015 at 6:05
  • 1
    All 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. Commented 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:

    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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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