How do I pass a variable to a twig template from a preprocess function of a views style plugin in a custom module?

In alpha.module, I have tried:

function alpha_preprocess_views_view(&$variables) {
  $variables["beta"] = "1";


function alpha_preprocess_page(&$variables) {
  $variables["beta"] = "1";

The template file is alpha-gamma.html.twig and the plugin is Gamma.php:

* Style plugin.
* @ViewsStyle(
*   id = "gamma",
*   title = @Translation("Gamma"),
*   help = @Translation("Displays gamma."),
*   theme = "alpha_gamma",
*   display_types = {"normal"}
* )
class Gamma extends StylePluginBase {

The template file is invoked and renders its output on the view's row results. However, {{ beta }} yields no result in the same file. I know I'm missing something important, but none of the examples I've found are specific to this type of row style template.

Guidance would be appreciated!

1 Answer 1


In below code you preprocess the variables for views-view.html.twig file

function alpha_preprocess_views_view(&$variables) {
  $variables["beta"] = "1";

and, function alpha_preprocess_page(&$variables) will preprocess the variables for page.html.twig

So, if you create a file views-view.html.twig then you will get your variable {{ beta }}

  • Based on the direction provided in your answer, and after a cache clear, the solution is function alpha_preprocess_alpha_gamma(&$variables) {. Thanks!
    – user70961
    Apr 5, 2018 at 14:55

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.