1

I have a Drupal 7 Views query plugin module that pulls in data from a RESTful API for use in Views. I used this series of articles as a starting point to build it: https://www.lullabot.com/articles/building-views-query-plugins. I now need to port it to Drupal 8.

I have made a start on the porting process, with the help of this series of articles: https://www.lullabot.com/articles/building-views-query-plugins-dupal-8-part-1. The Drupal 8 module now successfully pulls in data for the fields that use the 'standard' handler.

I'm currently stuck on trying to get tokens representing one of those fields to work when rewriting results.

The field is called access-url and contains URLs. If I tick 'Override the output of this field with custom text' on that field, and enter the token form, {{ access-url }}, it just returns '0' for every record, whereas if I untick that box it returns the URLs correctly. Meanwhile, the token form of the id field works correctly.

(In real life I will want to use {{ access-url }} under 'Output this field as a custom link', but 'Override the output of this field with custom text' illustrates the problem better.)

How can I get the {{ access-url }} token to work correctly?

I expect it would be helpful if I posted some code, but I'm not sure what code would be most helpful, so please let me know.

Additional debugging info: as far as I can tell, all seems well until it gets to the line in PluginBase::viewsTokenReplace() that runs $this->getRenderer()->renderPlain($build). $build at that point has the value of access-url in it, but the renderer returns zero.

11
  • Do you have addSelfTokens() and documentSelfTokens() in the field plugin?
    – 4uk4
    Commented Jul 25, 2019 at 12:43
  • At the moment I'm using the built-in 'standard' field handler. Do you think I would be better overriding it?
    – Tom Butlin
    Commented Jul 25, 2019 at 12:58
  • Not all built-in fields implement these methods, only entity fields and 2 or 3 other fields.
    – 4uk4
    Commented Jul 25, 2019 at 13:13
  • Okay, I'll have a go at that, and let you know how I get on. Thanks!
    – Tom Butlin
    Commented Jul 25, 2019 at 13:20
  • That didn't work, unfortunately. By the time addSelfTokens() runs, the correct tokens are already in the $tokens array. In fact, all seems well until it gets to the line in PluginBase::viewsTokenReplace() that runs $this->getRenderer()->renderPlain($build). $build at that point has the value of access-url in it, but the renderer returns zero. Any further thoughts on what might be going on would be gratefully received.
    – Tom Butlin
    Commented Jul 25, 2019 at 18:29

1 Answer 1

0

Eventually I realised that it was only tokens containing hyphens that weren't working, and sure enough, it turns out that hyphens are not allowed in Twig tokens. Or, to be more accurate, they are interpreted as minus signs (https://www.drupal.org/project/drupal/issues/2371633). That was why the {{ access-url }} token was returning zero: access minus url equals zero, apparently. So I just replaced all of the hyphens with underscores.

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.