You are here

public function SliderBlock::build in Image sliders 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/SliderBlock.php, line 75

Class

SliderBlock
Provides a block.

Namespace

Drupal\image_slider\Plugin\Block

Code

public function build() {
  $blok_id = $this
    ->getDerivativeId();
  $slider_data = [];
  $sql = $this->database
    ->select('slider', 's')
    ->fields('s')
    ->condition('s.id', $blok_id, '=')
    ->execute()
    ->FetchAll();
  if (is_array($sql) && count($sql) > 0) {
    $slider_data['id'] = $sql[0]->id;
    $slider_data['name'] = $sql[0]->name;
    $slider_data['slide_type'] = $sql[0]->slide_type;
    $slider_data['description'] = check_markup($sql[0]->description__value, 'full_html');
    $slider_data['langcode'] = $sql[0]->langcode;
    $image_sql = $this->database
      ->select('image_slider__image', 's')
      ->fields('s')
      ->condition('s.entity_id', $slider_data['id'], '=')
      ->execute()
      ->FetchAll();
    if (is_array($image_sql) && count($image_sql) > 0) {
      $i = 0;
      foreach ($image_sql as $val) {
        $slider_data['image'][$i]['image_alt'] = $val->image_alt;
        $file = $this->fileStorage
          ->load($val->image_target_id);
        $image_url = Url::fromUri(file_create_url($file
          ->getFileUri()))
          ->toString();
        $slider_data['image'][$i]['image_url'] = $image_url;
        $i++;
      }
    }
  }
  $build = [
    '#theme' => 'image_slider',
    '#data' => $slider_data,
    '#attached' => [
      'library' => [
        'image_slider/image_slider_data',
      ],
      'drupalSettings' => [
        'image_slider' => [
          'slider_tyle' => $slider_data['slide_type'],
        ],
      ],
    ],
  ];
  return $build;
}