I have a global php field in a view and it has a output field. However, I want to add / edit the template file instead which looks like this:


 * @file
 * This template is used to print a single field in a view.
 * It is not actually used in default Views, as this is registered as a theme
 * function which has better performance. For single overrides, the template is
 * perfectly okay.
 * Variables available:
 * - $view: The view object
 * - $field: The field handler object that can process the input
 * - $row: The raw SQL result that can be used
 * - $output: The processed output that will normally be used.
 * When fetching output from the $row, this construct should be used:
 * $data = $row->{$field->field_alias}
 * The above will guarantee that you'll always get the correct data,
 * regardless of any changes in the aliasing that might happen if
 * the view is modified.

<?php print $output; ?>

However, when I put anything here, like for example: echo "hello world"; nothing gets displayed.

If I put this php code in the output field on the view, it works just fine, but I am tasked with removing php from fields and putting them in either modules or template files so they do not risk exposure.

How come my template file will not display whatever php code I insert, but it will on the field itself? I'm confused.

  • Is this the template file of the custom php field? Why cant you just add a custom text field and update the template?
    – Bhuvana
    Apr 1, 2016 at 5:53
  • Is this the template file of the custom php field? Yes. Why cant you just add a custom text field and update the template? I'm not sure what you mean. My issue is I have a rendered table that has dynamic information triggered from a custom module another user made. He basically just set the Global PHP output to a single function call in the code that is linked to a custom module, but my company considers this PHP exposed, so I need to still trigger the function, but hide it away in a template file, but still working so that the dynamic information is rendered unto the table.
    – Shawn
    Apr 1, 2016 at 6:19
  • 1
    Just fyi: its not a good practice to use PHP field, at least because you break views caching and you are saving executable code as configuration.
    – skorzh
    Apr 1, 2016 at 7:23

2 Answers 2


1) Remove Global PHP field from the view

2) Add a new field of type "Global: Custom text"

3) Click on Theme: Information. You will be able to see the suggestion for template file

4)Create the template file with the required name.

5)Invoke the function which was being invoked from your Global: PHP field.

  • Wow, this is very interesting. It works, but ONLY after viewing the page. The changes do not occur in the "preview" section of the view. Is there a reason for that?
    – Shawn
    Apr 1, 2016 at 6:48
  • As far as I know template files dont get applied during preview.
    – Bhuvana
    Apr 1, 2016 at 6:57
  • I see, thanks for your help. I'm glad this got solved, I been on it for hours.
    – Shawn
    Apr 1, 2016 at 7:02

This article can be helpful for you: How to properly use PHP on Drupal views fields.

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.