You are here

public function ShsTermSelect::formatHtmlItem in Webform Simple Hierarchical Select 8

Overrides WebformEntityReferenceTrait::formatHtmlItem

File

src/Plugin/WebformElement/ShsTermSelect.php, line 174

Class

ShsTermSelect
Provides a 'webform_shs_term_select' Webform element.

Namespace

Drupal\webform_shs\Plugin\WebformElement

Code

public function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $entity = $this
    ->getTargetEntity($element, $webform_submission, $options);
  if (!$entity) {
    return '';
  }
  $format = $this
    ->getItemFormat($element);

  // For links, if the user has configured individual depth labels, format
  // links to the whole term tree.
  if ($format === 'link' && !empty($element['#depth_labels'])) {

    /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
    $parents = array_reverse($this
      ->getTermStorage()
      ->loadAllParents($entity
      ->id()));
    $output = [];
    foreach ($parents as $delta => $parent) {
      $output[] = [
        '#type' => 'container',
        'label' => [
          '#markup' => !empty($element['#depth_labels'][$delta]) ? $element['#depth_labels'][$delta] . '<span class="colon">:</span>' : '',
        ],
        'link' => [
          '#type' => 'link',
          '#title' => $parent
            ->label(),
          '#url' => $parent
            ->toUrl()
            ->setAbsolute(TRUE),
        ],
      ];
    }
    return $output;
  }
  else {
    return parent::formatHtmlItem($element, $webform_submission, $options);
  }
}