You are here

public function DrupalSliderViewsField::render in Drupal Slider 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/field/DrupalSliderViewsField.php \Drupal\drupal_slider\Plugin\views\field\DrupalSliderViewsField::render()

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/DrupalSliderViewsField.php, line 116

Class

DrupalSliderViewsField
Field handler for Drupal Slider.

Namespace

Drupal\drupal_slider\Plugin\views\field

Code

public function render(ResultRow $values) {
  if (!empty($values)) {
    $layers = [];
    $slide_options = $this->options['slide_options'];
    $slide_grouping = \Drupal::entityTypeManager()
      ->getStorage('drupal_slider_slide_grouping')
      ->load($slide_options);
    $slides_list = $slide_grouping
      ->get('slides');
    $view_result = $this->view->result;
    $views_count = count($view_result);
    $slides_list_count = count($slides_list);
    $slides_list_keys = array_keys($slides_list);
    for ($i = 0; $i < $views_count; $i++) {
      if ($slides_list_count >= $i + 1) {
        $slide_key = array_slice($slides_list_keys, $i, 1);
        $layers_name[$view_result[$i]->nid] = $slides_list[$slide_key[0]]['slide_list'];
      }
    }
    if (isset($layers_name[$values->nid])) {
      $single_layer_name = $layers_name[$values->nid];
      $layer_load = \Drupal::entityTypeManager()
        ->getStorage('drupal_slider_slide')
        ->load($single_layer_name);
      $layers_attr = $layer_load
        ->get('layers');
      if ($layers_attr) {
        $i = 1;
        foreach ($layers_attr as $key => $layer_attr) {
          $layers['layer_' . $i] = $layer_attr;
          $i++;
        }
      }
    }
    else {
      $layers_attr = [
        'attributes' => [
          'horizontal' => '100',
          'vertical' => '100',
          'show_delay' => '0',
          'hide_delay' => '0',
          'show_transition' => 'left',
          'hide_transition' => 'left',
        ],
      ];
    }
    $background_img = $this->options['background_img'];
    $fields = $this->view->display_handler
      ->getHandlers('field');
    $labels = $this->view->display_handler
      ->getFieldLabels();
    foreach ($labels as $key => $var) {

      // If we find a replacement variable, replace it.
      if (strpos($background_img, "{{ {$key} }}") !== FALSE) {
        $field = $this
          ->cleanVar($fields[$key]);
        $background_img = str_replace("{{ {$key} }}", $field, $background_img);
      }
    }
    $config = \Drupal::config('drupal_slider.settings');
    $layers_count = $config
      ->get('ds_layers_count');
    for ($i = 1; $i <= $layers_count; $i++) {
      $no_replacements = TRUE;
      foreach ($labels as $key => $var) {
        $single_layer = $this->options['layer_' . $i];
        if (!empty(trim($single_layer))) {
          if (strpos($single_layer, "{{ {$key} }}") !== FALSE) {
            $field = $this
              ->cleanVar($fields[$key]);
            if (isset($layers['layer_' . $i]['value'])) {
              $single_layer = $layers['layer_' . $i]['value'];
            }
            $layers['layer_' . $i]['value'] = str_replace("{{ {$key} }}", $field, $single_layer);
            $no_replacements = FALSE;
          }
        }
      }
      if ($no_replacements) {
        $layers['layer_' . $i]['value'] = $single_layer;
      }
    }
    return [
      '#theme' => 'drupal_slider_layers',
      '#background_img' => $background_img,
      '#layers_attributes' => $layers_attr,
      '#layers' => $layers,
    ];
  }
}