Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Render Custom field with Drupal Renderer

By editor , 20 May, 2016

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',
];

 

Tags

  • Drupal 8
  • Code
  • Twig
  • Images

Comments

Plain text

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

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer