Render Custom field with Drupal Renderer | Drupal 8

Render Custom field with Drupal Renderer

Submitted by editor on Fri, 05/20/2016 - 12:50

Example 1, with FieldPluginBase (\Drupal\views\Plugin\views\field\FieldPluginBase)

public function render(ResultRow $values) {
    $url = $this->getValue($values);      //$url is an external image like : "https://www.drupal.org/files/druplicon.png";
    $output[] = array(
      '#theme' => 'imagecache_external',
      '#uri' => $url,
      '#style_name' => 'thumbnail',
      '#alt' => 'Test alt',
      '#title' => 'Test title',
      '#width' => NULL,
      '#height' => NULL,
      '#attributes' => array(),
    );

    $renderer = $this->getRenderer();
    return $renderer->render($output);
}

Example 2: Internal / External image from URL or URI

$output[] = [
    '#theme' => 'image_style',
    '#style_name' => 'thumbnail',
    '#uri' => 'public://my-image.png',
    // optional parameters
];
$output[] = [
    '#theme' => 'image_style',
    '#style_name' => 'thumbnail',
    '#url' => 'https://mysite.com/my-image.png',
    // optional parameters
];

$output[] = [
    '#theme' => 'image',
    '#uri' => 'public://my-image.png',
];

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.