2

What should be considered the optimal way of creating a custom field in views?

For example:

if($content_type == 'article'){
  print 'something';
}
else{
  print 'something else';
}

I either create a Custom Text field and write my php code in its template, or, lately, I've started using views_php field.

The disadvantage with the templates is that if your site is kinda big, you end up with tons of template files that are sometimes hard to manage, and also (obviously) cannot be managed from someone without server access. Furthermore it cannot support sorting/filtering of some kind. Apart from that, it's "fail safe", it's robust, it doesn't add to complexity via an extra module.

The (possible) disadvantage of the php field is that it's an extra layer of complexity to the site and thus an extra bug possibility. Apart from that, it's easily manageable and it can even be used for filtering/sorting!

So, what would you guys suggest? What do you usually do in such cases?

8
  • 2
    Don't be fooled into thinking 'easier' is necessarily 'better' here. The PHP filter uses eval() (which everybody knows is evil and a huge security risk; if you haven't come across that truth before Google it - you'll never use the PHP filter again!) Couple that with the inability to revision code held in the database and there's just no argument for using the PHP filter at all IMHO.
    – Clive
    Commented Jan 23, 2013 at 12:16
  • Geez I feel like I have to go back to all my projects and re-do every implementation/usage of php field now :S
    – user5005
    Commented Jan 23, 2013 at 12:28
  • 1
    Sorry that did sound a but harsh, but one needs to be hard-nosed when it comes to security :) If you want to audit what Views you have in the system that use the PHP filter, run this code: $views = db_query("SELECT vv.name, vd.id FROM views_view vv INNER JOIN views_display vd ON vd.vid = vv.vid WHERE display_options LIKE '%php_output%'")->fetchAll();, $views will contain a list of rows from the main views table, check the name column for the view name and go from there
    – Clive
    Commented Jan 23, 2013 at 13:02
  • No need for apologies. Constructive critisism is always welcome. There is learning possibility everywhere ;)
    – user5005
    Commented Jan 23, 2013 at 13:04
  • Also, Clive, I reckon your query wouldn't take into consideration views that live 'in code' i.e. managed/tracked with Features?
    – user5005
    Commented Jan 23, 2013 at 13:14

1 Answer 1

3

Here are some advantages of using templates instead of a custom PHP field.

  1. Templates can be naturally version-controlled just like the rest of your code. (You could version-control the custom php field with Features, but that is an additional layer of complexity.)
  2. Since the code for templates is in files, you can use UNIX commands like grep, etc. to search them. You can also batch edit them easily if necessary.
  3. If you accidentally misconfigure your site permissions and give a role access to Views administration that shouldn't have it, they still won't be able to insert PHP into your site if you're using templates (eliminates a security risk). Consider that one of the security enhancements in Drupal 7 over Drupal 6 was hiding the PHP filter and making it harder to enable.
  4. The Views PHP module is still in dev and thus not subject to audit by the Drupal security team. There may be (are) bugs in the module and since it is not in widespread use they may not be found for some time (avoids another security risk).

Yes, templates might be a little inconvenient, but from a security and maintainability standpoint I think they are clearly superior.

5
  • Good points. So you'd go for php field only if you needed sorting/filtering on that complex-to-produce output if I understand correctly.
    – user5005
    Commented Jan 23, 2013 at 12:16
  • 1
    If you need sorting/filtering, you can add a custom views handler. There is no need to ever use the PHP field. Commented Jan 23, 2013 at 12:19
  • I wish I could upvote that comment 20 times
    – Clive
    Commented Jan 23, 2013 at 12:19
  • Can you provide some info/link to documentation on to how to implement such a feature?
    – user5005
    Commented Jan 23, 2013 at 12:26
  • 1
    Earl Miles, the creator of Views, has a book on Views and Panels development. As for tutorials, here is one on custom filters: metaltoad.com/blog/… and another on exposed filters: metaltoad.com/blog/how-make-range-selector-filter-views Commented Jan 23, 2013 at 12:29

Your Answer

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