You are here

protected function WebformEntityReferenceTrait::formatHtmlItem in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/WebformEntityReferenceTrait.php \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::formatHtmlItem()

File

src/Plugin/WebformElement/WebformEntityReferenceTrait.php, line 46

Class

WebformEntityReferenceTrait
Provides an 'entity_reference' trait.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $entity = $this
    ->getTargetEntity($element, $webform_submission, $options);
  if (empty($entity)) {
    return '';
  }
  $format = $this
    ->getItemFormat($element);
  switch ($format) {
    case 'raw':
    case 'value':
    case 'id':
    case 'label':
    case 'text':
    case 'breadcrumb':
      return $this
        ->formatTextItem($element, $webform_submission, $options);
    case 'link':
      if ($entity
        ->hasLinkTemplate('canonical')) {
        return [
          '#type' => 'link',
          '#title' => $entity
            ->label(),
          '#url' => $entity
            ->toUrl()
            ->setAbsolute(TRUE),
        ];
      }
      else {
        switch ($entity
          ->getEntityTypeId()) {
          case 'file':

            /** @var \Drupal\file\FileInterface $entity */
            if ($entity
              ->access('download')) {
              return [
                '#type' => 'link',
                '#title' => $entity
                  ->label(),
                '#url' => UrlGenerator::fromUri(file_create_url($entity
                  ->getFileUri())),
              ];
            }
            else {
              return $this
                ->formatTextItem($element, $webform_submission, $options);
            }
          default:
            return $this
              ->formatTextItem($element, $webform_submission, $options);
        }
      }
    default:
      return \Drupal::entityTypeManager()
        ->getViewBuilder($entity
        ->getEntityTypeId())
        ->view($entity, $format);
  }
}