I have created a Slider entity with the drupal console, with the website interface I create some fields to this entity (field_image, field_title, field_subtitle), but I can't manage to get these fields in my template.

I'm new to Drupal 8 and I don't understand how to get these values, all the answer I saw on the web start with the node object but I don't have a node object :/

So I created a block, in the build function I get all the Slider entity to show them in a carousel :

 * Slider homepage
 * @Block(
 *   id = "slider_block",
 *   admin_label = @Translation("Slider (HP)"),
 * )
class SliderBlock extends BlockBase

     * {@inheritdoc}
    public function build()
        $view_builder = \Drupal::entityTypeManager()->getViewBuilder('slider');

        $ids = \Drupal::entityQuery('slider')
            ->condition('status', 1);
        $pids = $ids->execute();
        $slides = Slider::loadMultiple($pids);

        $build = [
            '#theme' => 'carousel_slider',
            '#slides' => $view_builder->viewMultiple($slides, 'full'),
            '#attributes' => ['class' => ['owl-carousel-wrapper'], 'id' => 'owl-carousel-hp'],
            '#attached' => [
                'library' => ['web_core/owl-carousel'],
                'drupalSettings' => [
                    'owl-carousel-hp' => [
                        'singleItem' => true,

        return $build;


And what I want is to get the values of these fields in my template, this is the template for one slide, and I use this rendered markup in the carousel_slider template after.

<div class="slide">
    <img src="{{ file_url(slide.field_image.entity.field_media_file.entity.fileuri) }}" >
    <h2>{{ slide['slide']['#slider'] }}</h2>

I've tried some way to achieve this but I can't manage to find how to access my values...

  • 1
    Don't reinvent the wheel, there are battle-tested modules for this: If your sliders should be reused accross multiple pages, I'd suggest to use create a "slider" bundle with using core's "Custom content block" module. If your sliders are just hero-banners for a single page (usually not resused), I'd suggest to take a look at the Paragraphs module. – Hudri Apr 16 at 14:10
  • I know there is a lot of modules for this, but I need to do my own, that's my first experience in Drupal 8 and if I can't achieve just get data from my entity fields I can't do anything .. This slider will be reused in multiple pages and with different configurations. So do you have any information about the way to get my fields values ? – Jessydan Apr 16 at 14:15
  • If you put the entities without view builder in the template: '#slides' => $slides, the img src you have and slide.field_title.value should work in a loop {% for slide in slides %}. In this case you need to add the cache tags of the entities to the build array if you want that the slides are updated when you change them in UI. – 4k4 Apr 16 at 15:09

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.