You are here

public function ExampleNodeLabelFormatted::viewElements in Extra Field Settings Provider 8

Same name and namespace in other branches
  1. 8.2 modules/extra_field_plus_example/src/Plugin/ExtraField/Display/ExampleNodeLabelFormatted.php \Drupal\extra_field_plus_example\Plugin\ExtraField\Display\ExampleNodeLabelFormatted::viewElements()

File

modules/extra_field_plus_example/src/Plugin/ExtraField/Display/ExampleNodeLabelFormatted.php, line 39

Class

ExampleNodeLabelFormatted
Example Node Label Extra field with formatted output.

Namespace

Drupal\extra_field_plus_example\Plugin\ExtraField\Display

Code

public function viewElements(ContentEntityInterface $entity) {
  $settings = $this
    ->getSettings();
  $link_to_entity = $settings['link_to_entity'];
  $wrapper = $settings['wrapper'];
  $label = $entity
    ->label();
  $url = NULL;
  if ($link_to_entity) {
    $url = $entity
      ->toUrl()
      ->setAbsolute();
  }
  if ($url) {
    $element = [
      '#type' => 'html_tag',
      '#tag' => $wrapper,
      [
        '#type' => 'link',
        '#title' => $label,
        '#url' => $url,
      ],
    ];
  }
  else {
    $element = [
      '#type' => 'html_tag',
      '#tag' => $wrapper,
      '#value' => $label,
    ];
  }
  return $element;
}