You are here

protected function ContentEntityViewModesExtractor::getPreviewImageUrl in Acquia Content Hub 8

Get preview image URL.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The Content Entity object.

Return value

string Preview image URL.

1 call to ContentEntityViewModesExtractor::getPreviewImageUrl()
ContentEntityViewModesExtractor::getRenderedViewModes in src/Normalizer/ContentEntityViewModesExtractor.php
Renders all the view modes that are configured to be rendered.

File

src/Normalizer/ContentEntityViewModesExtractor.php, line 268

Class

ContentEntityViewModesExtractor
Extracts the rendered view modes from a given ContentEntity Object.

Namespace

Drupal\acquia_contenthub\Normalizer

Code

protected function getPreviewImageUrl(ContentEntityInterface $entity) {
  $entity_type_id = $entity
    ->getEntityTypeId();
  $bundle = $entity
    ->bundle();
  $contenthub_entity_config_id = $this
    ->getContentHubEntityTypeConfigEntity($entity_type_id);

  // Obtaining preview image field and style from the configuration entity.
  $preview_image_field = $contenthub_entity_config_id
    ->getPreviewImageField($bundle);
  $preview_image_style = $contenthub_entity_config_id
    ->getPreviewImageStyle($bundle);

  // Don't set, if no preview image has been configured.
  if (empty($preview_image_field) || empty($preview_image_style)) {
    return '';
  }
  $preview_image_config_array = explode('->', $preview_image_field);
  foreach ($preview_image_config_array as $field_key) {

    // Don't set, if node has no such field or field has no such entity.
    if (empty($entity->{$field_key}->entity) || method_exists($entity->{$field_key}, 'isEmpty') && $entity->{$field_key}
      ->isEmpty()) {
      return '';
    }
    $entity = $entity->{$field_key}->entity;
  }
  if (!in_array($entity
    ->bundle(), [
    'image',
    'file',
  ])) {
    return '';
  }
  $file_uri = $entity
    ->getFileUri();

  // Process Image style.
  $image_style = $this->entityTypeManager
    ->getStorage('image_style')
    ->load($preview_image_style);

  // Return empty if no such image style.
  if (empty($image_style)) {
    return '';
  }

  // Return preview image URL.
  $preview_image_uri = $image_style
    ->buildUrl($file_uri);
  return file_create_url($preview_image_uri);
}