PHP function in twig - EASY! Ever tried TWIG in PHP function?

I was working on issue queue of views extras module and came across a requirement: Add twig support: For a moment I was helpless, I mean I always had the requirement to use PHP function in twig but never thought in the reverse direction and to resolve that issue I need to know how we can support twig variables in PHP.

Now, this was interesting, so I began my search with finding out how Drupal handles output using twig how it replaces variables and all. I went into the code to find that but soon I changed my mind and found the solution another way.

Problem in brief: how "{{ user.getDisplayName() }}" can produce desired output as twig does and support similar stuff in PHP. 

So, scratch your head and try to find a solution, let me see where you guys end up.

Solution :

To support the use of Twig syntax in PHP we can make use of inline_template render element introduced by Drupal
Reference: New inline_template render element for HTML code in PHP


Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.