0

How can we pass this php code as a twig variable in global custom text in views without using views php module?

I have created a view of news content with masonary layout .

<?php
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node /'.$row->nid);
echo $language.$alias;
?>

I just want to render it as a twig variable in custom text field of views without creating template because i have already created a page long ago i dont want to rework by creating template.

1 Answer 1

1

To place PHP code in a field extend a field plugin and override render(). But in your case you should get the result of this code from the existing EntityLink plugin (in UI "Link to Content") and the option output_url_as_text. If you want to use it as Twig variable in another field, hide the entity link field from display and place it in front of the other field.

4
  • actually i am using multilingual website in which link to content field navigate to native language rather than respective language, for this i am trying to write custom code for creating link to content which is language based its working fine in views php but my client restricted me not to use views php
    – user52318
    Sep 12, 2018 at 9:00
  • Then place the PHP code in render() of a field plugin. You can generate one with Drupal Console: drupal generate:plugin:views:field
    – 4uk4
    Sep 12, 2018 at 9:31
  • can you provide some reference link i am not able to understand what you saying
    – user52318
    Sep 12, 2018 at 10:57
  • When you run this console command with the default options (you only need to specify an existing custom module) and clear the cache you'll find a new field with the description "My awesome custom views field plugin." in Views UI. Then put the PHP code in render() of the generated code.
    – 4uk4
    Sep 12, 2018 at 11:11

Your Answer

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