0

I'm less familiar with D8, but I need to re-write a views field output using some PHP. I've made a custom field template HTML.twig file, but I'm not sure what I need to do to be able to run a PHP execution on my field to render it out as I want.

edit:

I am trying to run a function on the User ID as I have a module which hashes it before sending it to an external platform, however, now the client wants the understand which user is which, and the only way I can think to do this is by performing the same hashing code on the User ID in a format I can easily cross reference the user with their hashed output

I have a very basic view table showing only the User ID and email address, and I've managed to create a views field override using this page which has overwritten my field successfully. But now I want to run a command I'm going to copy from my contrib module, the below to be specific, so that I can work out which User ID relates to which hashed code

return Crypt::hmacBase64($uid, \Drupal::service('private_key')->get() . Settings::getHashSalt());
4
  • I guess this question needs a bit more detail than "a views field" and "some PHP". What exactly are you trying to achieve? What view? What Views field? Where does it come from? What does it display now? What should it display instead? Please edit your question and add more detail and context. Please try to make this a reproducible issue. Many thanks 🙂
    – leymannx
    Nov 30, 2020 at 12:13
  • @leymannx, I've edited it a bit, I wasn't necessarily looking for someone to do the whole job for me, I was just wondering how it's possible to do it in D8 as I'm more familiar with D7 Nov 30, 2020 at 12:21
  • There is no PHP code in Twig. If you want to to do the D8/Symfony way, I'd recommend to google for "Drupal custom twig filter". e.g. this blog
    – Hudri
    Nov 30, 2020 at 12:44
  • perhaps you can do that in a preprocess function and pass the result as variable to your twig template. Twig is only for presentation and not computation or as suggested create your own twig filter but I guess preprocessing might be easier.
    – GiorgosK
    Nov 30, 2020 at 16:11

1 Answer 1

0

You can write Twig directly into your view with a Custom text field.

Custom text field

Using Twig as an entry point, you can use all of the Twig functionality and filters that ship with Drupal, as well as expanding on these with modules like Twig Tweak and Twig Field Value. You can also write your own Twig extensions, which are written in PHP. Here are two suitable tutorials:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.