You are here

public function NivoSliderSliderBlock::build in Nivo Slider 8

Implements \Drupal\block\BlockBase::build().

Overrides BlockPluginInterface::build

File

src/Plugin/Block/NivoSliderSliderBlock.php, line 37
Contains \Drupal\nivo_slider\Plugin\block\block\NivoSliderSliderBlock.

Class

NivoSliderSliderBlock
Provides a 'Slider' block.

Namespace

Drupal\nivo_slider\Plugin\Block

Code

public function build() {

  // Get the current slider settings.
  $config = \Drupal::getContainer()
    ->get('config.factory')
    ->getEditable('nivo_slider.settings');
  $build = [
    '#type' => 'markup',
    '#theme' => 'nivo_slider_wrapper',
    '#attached' => [
      'drupalSettings' => [
        'nivo_slider' => [
          'effect' => $config
            ->get('options.effect') ? Html::escape($config
            ->get('options.effect')) : 'random',
          'slices' => $config
            ->get('options.slices') ? (int) Html::escape($config
            ->get('options.slices')) : 15,
          'boxCols' => $config
            ->get('options.box_columns') ? (int) Html::escape($config
            ->get('options.box_columns')) : 8,
          'boxRows' => $config
            ->get('options.box_rows') ? (int) Html::escape($config
            ->get('options.box_rows')) : 4,
          'animSpeed' => $config
            ->get('options.animation_speed') ? (int) Html::escape($config
            ->get('options.animation_speed')) : 500,
          'pauseTime' => $config
            ->get('options.pause_time') ? (int) Html::escape($config
            ->get('options.pause_time')) : 3000,
          'startSlide' => $config
            ->get('options.start_slide') ? (int) Html::escape($config
            ->get('options.start_slide')) : 0,
          'directionNav' => Html::escape($config
            ->get('options.directional_navigation')) == 1 ? TRUE : FALSE,
          'controlNav' => Html::escape($config
            ->get('options.control_navigation')) == 1 ? TRUE : FALSE,
          'controlNavThumbs' => Html::escape($config
            ->get('options.control_nav_thumbs')) == 1 ? TRUE : FALSE,
          'pauseOnHover' => Html::escape($config
            ->get('options.pause_on_hover')) == 1 ? TRUE : FALSE,
          'manualAdvance' => Html::escape($config
            ->get('options.manual_advance')) == 1 ? TRUE : FALSE,
          'prevText' => $config
            ->get('options.previous_text') ? Html::escape($config
            ->get('options.previous_text')) : t('Prev')
            ->render(),
          'nextText' => $config
            ->get('options.next_text') ? Html::escape($config
            ->get('options.next_text')) : t('Next')
            ->render(),
          'randomStart' => Html::escape($config
            ->get('options.random_start')) == 1 ? TRUE : FALSE,
        ],
      ],
      'library' => [
        'nivo_slider/nivo.slider',
      ],
    ],
  ];

  // Collect all themes.
  $themes = \Drupal::moduleHandler()
    ->invokeAll('nivo_slider_theme_info');

  // Allow theme information to be altered.
  \Drupal::moduleHandler()
    ->alter('nivo_slider_theme_info', $themes);

  // Find the currently selected theme.
  $current_theme = $config
    ->get('options.theme');

  // Get the current theme's settings.
  $theme = $themes[$current_theme];

  // Add the theme's resources.
  foreach ([
    'js',
    'css',
  ] as $type) {
    if (!empty($theme['resources'][$type])) {
      foreach ($theme['resources'][$type] as $file_path) {
        $build['content']['#attached'][$type][] = $file_path;
      }
    }
  }
  return $build;
}