There are myriad applications to need to execute specific PHP functions in Views rows. In many cases, making a custom Plugin is a better option. In other cases, it's better to use a Twig or custom token input filter.

Yet there are unique, specific use cases in which I need to simply talk to custom functions and the database in ways that are outside of a plugin.

I've reviewed this guide - https://enzo.weknowinc.com/how-include-php-code-drupal-8-views/ - but it did not work because Drupal console isn't functional for D9+, as it uses a later version of Symfony. All of the other resources I've found either 404 or cause errors due to being outdated.

Is there a way to accomplish the injection of PHP into views rows like D7?

  • 1
    You don't need Drupal console to create a module. The same can be done with Drush or on your own.
    – Kevin
    Jul 13, 2022 at 23:38
  • I can also do so manually, as I do often - but DC in this tutorial seems to auto-gen dependencies and use statements which i don't know off the top of my head. I'm looking to get a way to easily execute PHP in views. Right now I'm using php in blocks as an alternative, but I'd prefer this wouldn't be so difficult.
    – TripodC
    Jul 14, 2022 at 0:00
  • 1
    The Drupal 8/9/10 architecture is fundamentally different from Drupal 7 and there is no "easy" way to insert PHP anymore. Use a plugin. If you want to avoid plugins at all costs and do it like Drupal 7, then just use Drupal 7. Drupal 8/9/10 are plugin-based; for better or worse, the future of Drupal is plugins. Jul 14, 2022 at 2:54

1 Answer 1


In the current Drush version you can build plugins to run PHP in Views with the following commands:


drush gen plugin:views:field

Style rows:

drush gen plugin:views:style

The first example is a direct replacement for a PHP field in earlier Drupal versions. The second command produces a nice example of a configurable plugin including a views twig template and its preprocess hook.

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.