You are here

private function DibaCarousel::composeSlide in Diba carousel slider 8

Return slide fields from entity.

1 call to DibaCarousel::composeSlide()
DibaCarousel::getItems in src/Plugin/Block/DibaCarousel.php
Get the carousel items.

File

src/Plugin/Block/DibaCarousel.php, line 761

Class

DibaCarousel
Provides a Diba carousel Block.

Namespace

Drupal\diba_carousel\Plugin\Block

Code

private function composeSlide($config, $entity) {

  // Slide title.
  $title = '';
  if (!empty($config['title'])) {
    $title = strip_tags($entity->{$config['title']}->value);
  }

  // Slide captation.
  $description = '';
  if (!empty($config['description']) && isset($entity->{$config['description']})) {

    // Referenced entities field as description, use referenced entity label.
    if (!empty($entity->{$config['description']}->target_id)) {
      $referencedEntities = $entity
        ->get($config['description'])
        ->referencedEntities();
      $labels = [];
      foreach ($referencedEntities as $referencedEntity) {
        $labels[] = $referencedEntity
          ->label();
      }
      $description = implode(', ', $labels);
    }
    elseif (!empty($entity->{$config['description']}->value)) {
      $description = $entity->{$config['description']}->value;
    }
    if ($description) {

      // If not allow HTML strip tags and convert some html entities.
      if (!$config['description_allow_html']) {
        $description = str_replace(' ', ' ', strip_tags($description));
      }

      // Truncate captation in a safe mode.
      if (!empty($config['description_truncate']) && $config['description_truncate'] > 0) {
        $description = Unicode::truncate($description, $config['description_truncate'], TRUE, TRUE);
        if ($config['description_allow_html']) {
          $description = Html::normalize($description);
        }
      }

      // Trim spaces, tabs and other special chars.
      $description = trim($description);
      $description = filter_var($description, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);

      // Invalid utf-8 chars breaks big_pipe so force UTF-8.
      $description = Unicode::convertToUtf8($description, 'UTF-8');

      // Add see more link to entity.
      if ($config['description_see_more_link']) {

        // Ensure that entity has a canonical link.
        if ($see_more_target = $entity
          ->toUrl('canonical')) {
          $description .= new FormattableMarkup(' <a href="@link" class="see-more-description">@label.</a>', [
            '@link' => $see_more_target
              ->toString(),
            '@label' => $this
              ->t('See more'),
          ]);
        }
      }
    }
  }

  // Slide url.
  $url = '';
  if ('canonical' === $config['url'] || 'nid' === $config['url']) {
    $url = $entity
      ->toUrl('canonical');
  }
  elseif (!empty($config['url']) && isset($entity->{$config['url']}) && !$entity->{$config['url']}
    ->isEmpty()) {
    $url = $entity->{$config['url']}
      ->first()
      ->getUrl();
  }

  // Slide image.
  $image_width = $image_height = $image_uri = '';
  if (!empty($config['image']) && isset($entity->{$config['image']})) {
    $image_obj = $entity->{$config['image']}->entity;
    if (!empty($image_obj)) {
      $image_uri = $image_obj
        ->getFileUri();
    }
    else {

      // Image not found, try the default image.
      $default_image = $entity->{$config['image']}
        ->getSetting('default_image');
      if (!empty($default_image) && isset($default_image['uuid'])) {
        $default_entity = $this->entityRepository
          ->loadEntityByUuid('file', $default_image['uuid']);
        if (!empty($default_entity)) {
          $image_uri = $default_entity
            ->getFileUri();
        }
      }
    }
    if (!empty($image_uri)) {

      // Use an image style instead of the original file.
      if (!empty($config['image_style'])) {
        $style = ImageStyle::load($config['image_style']);
        $image_derivative = $style
          ->buildUri($image_uri);

        // Create derivative if necessary.
        if (!file_exists($image_derivative)) {
          $style
            ->createDerivative($image_uri, $image_derivative);
        }
        $image_uri = $image_derivative;
      }

      // Check if the image is valid.
      $image = $this->imageFactory
        ->get($image_uri);
      if ($image
        ->isValid()) {
        $image_width = $image
          ->getWidth();
        $image_height = $image
          ->getHeight();
      }
    }
  }
  return [
    'image' => $image_uri,
    'image_width' => $image_width,
    'image_height' => $image_height,
    'title' => $title,
    'url' => $url,
    'description' => $description,
  ];
}