0

I have a little module that I want to use to show a slideshow in a block, the module was created with the drupal console.

I added this code for the block :

/**
 * 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' => 'slider',
        '#slides' => $view_builder->viewMultiple($slides, 'full'),
        '#attributes' => ['class' => ['owl-carousel-wrapper'], 'id' => 'owl-carousel-hp'],
        '#attached' => [
            'library' => ['slider/owl-carousel'],
            'drupalSettings' => [
                'block-sliderhp' => [
                    'singleItem' => true,
                ]
            ],
        ]
    ];


    return $build;
}

/**
 * Définition du context de cache du block
 * @return string[] Context de cache
 */
public function getCacheContexts()
{
    return Cache::mergeContexts(parent::getCacheContexts(), array('url.path', 'url.query_args'));
}

public function getCacheTags()
{
    return Cache::mergeTags(parent::getCacheTags(), array('slider'));
}

}

My problem is about the view_builder->viewMultiple I want to render one by one my slider entity (which is called Slide), and then use these render in the slider template. So I have this hook_theme :

  function slider_theme() {
  return [
    'slider' => [
      'render element' => 'slides',
    ],
    'slide' => [
      'render element' => 'slide',
    ]
  ];
}

I can't manage to achieve what I want to do, the slide template is never called, and only the slider template is called. My preprocess function for the two are never called ...

And also if someone know about OwlCarousel, mine is not working well (the parameters are not passed).

  • I can't understand what your question is myself. The explanation of the issue doesn't really seem to contain the full information, and you are showing code and referring to two theme hooks (slider and slide), but I only see you using one of them. – Jaypan Apr 16 at 4:42
  • What I want is to use the slide hook in the view_builder->view, I don't know how to make this, I was thinking that's the default hook used because it's the one that was generated with my entity when I create it. – user93063 Apr 16 at 5:07

Your Answer

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