1

This question already has an answer here:

I created a custom view field with the hook _views_data and a custom Class who extends FieldPluginBase.

With the render method, I can return a sample string :

class MyCustomClassViewsField extends FieldPluginBase {
...
    public function render(ResultRow $values) {
      $node = $values->_entity;
      return $node->id(); // My view field shows the nid of the current node
    }
...

Now, I want shows a link with my field. I tried :

class MyCustomClassViewsField extends FieldPluginBase {
...
    public function render(ResultRow $values) {
      $node = $values->_entity;
      return "<a href='http://www.test.com'>custom link</a>"
    }
...

But my field in my view shows :

<a href='http://www.test.com'>custom link</a>

and not

custom link

How can I show the link as an html element, not a sample text ?

Thank you =)

UP

This code work :

return array('#markup' => \Drupal::l("test",\Drupal\Core\Url::fromUri("http://www.example.com")));

ut the l() function is deprecated. If I use Link::fromTextAndUrl instead :

return array('#markup' => \Drupal\Core\Link::fromTextAndUrl("test",\Drupal\Core\Url::fromUri("http://www.example.com")));

I get the error :

[Sun Sep 18 22:17:59.444188 2016] [:error] [pid 2143] [client 192.168.1.48:53322] Recoverable fatal error: Object of class Drupal\Core\Link could not be converted to string in /var/www/xxxxx/core/lib/Drupal/Component/Utility/Xss.php on line 67, referer: https://192.168.1.45/admin/structure/views/view/customview

[Sun Sep 18 22:17:59.446098 2016] [:error] [pid 2143] [client 192.168.1.48:53322] PHP Fatal error: Uncaught Error: Call to a member function get() on null in /var/www/xxxxx/core/lib/Drupal/Core/Session/SessionHandler.php:76\nStack trace:\n#0 /var/www/xxxxx/vendor/symfony/http-foundation/Session/Storage/Handler/WriteCheckSessionHandler.php(89): Drupal\Core\Session\SessionHandler->write('BTaHaVF0t-bMk2Z...', '_sf2_attributes...')\n#1 /var/www/xxxxx/core/lib/Drupal/Core/Session/WriteSafeSessionHandler.php(75): Symfony\Component\HttpFoundation\Session\Storage\Handler\WriteCheckSessionHandler->write('BTaHaVF0t-bMk2Z...', '_sf2_attributes...')\n#2 /var/www/xxxxx/vendor/symfony/http-foundation/Session/Storage/Proxy/SessionHandlerProxy.php(77): Drupal\Core\Session\WriteSafeSessionHandler->write('BTaHaVF0t-bMk2Z...', '_sf2_attributes...')\n#3 [internal function]: Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy->write('BTaHaVF0t-bMk2Z...', '_sf2_attributes...')\n#4 [internal function]: session_write_close()\n#5 {main}\n thrown in /var/www/xxxxx/core/lib/Drupal/Core/Session/SessionHandler.php on line 76, referer: https://1xxxxx/admin/structure/views/view/customview

How can I replace the l deprecated function ?

marked as duplicate by Adrian Cid Almaguer, Krishna Mohan, Pierre.Vriens, googletorp Sep 19 '16 at 21:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

You need to convert array to string to show it as link. May this works for you.

class MyCustomClassViewsField extends FieldPluginBase {
...
    public function render(ResultRow $values) {
      $node = $values->_entity;
$title = 'Custom Link';
$url = \Drupal\Core\Url::fromUri('http://www.test.com');
$link = \Drupal\Core\Link::fromTextAndUrl($title, $url)->toString();

      return $link;
    }

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