0

I need to render an image from my custom block. So i have to return an renderable array of an image object from my block.

I have written code like this(snippet), and its working fine.

$logo_build = [
       '#theme' => 'image',
       '#width' => $width,
       '#height' => $height,
       '#uri' => $uri,
     ];

     $renderer = \Drupal::service('renderer');
     $renderer->addCacheableDependency($logo_build, $file);

     return [
       'logo' => $logo_build,
     ];

but now, how to set '#cache' value here ?
If it return array like below then i can use '#cache'. But in the above code how to set '#cache' value ?

return array(
     '#cache' => ,
);
  • $logo_build = [ '#theme' => 'image', '#width' => $width, '#height' => $height, '#uri' => $uri, '#cache' => [ 'contexts' => abc, 'tags' => xyz, ],]; Try something like this – Shreya Shetty May 16 '16 at 9:18
  • i have used '#cache' => ['contexts' => ['url.path']] , but its not working in above way. – Jasodeep Chatterjee May 16 '16 at 9:31
  • ['contexts' => ['url.path', 'url.query_args'] u arent passing query arguments – Shreya Shetty May 16 '16 at 9:35
  • I think 'url.path' is fine, because its working for me outside the $logo_build array – Jasodeep Chatterjee May 16 '16 at 10:05
  • What kind of caching are you looking for? When should the cache be invalidated? – Eyal May 16 '16 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.