You are here

public function EntityListTrait::view in Formatter Suite 8

File

src/Plugin/Field/FieldFormatter/EntityListTrait.php, line 234

Class

EntityListTrait
Formats multiple fields as a list.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

public function view(FieldItemListInterface $items, $langcode = NULL) {
  if ($items
    ->isEmpty() === TRUE) {
    return [];
  }

  // Let the parent class set up a render array for all items.
  $this
    ->sanitizeListSettings();
  $build = parent::view($items, $langcode);
  if (empty($build) === TRUE) {
    return [];
  }

  // Replace the 'field' theme with ours, which supports lists.
  $build['#theme'] = 'formatter_suite_field_list';

  // Set the list style.
  $build['#list_style'] = $this
    ->getSetting('listStyle');

  // Set the list separator.
  //
  // Security: The list separator is entered by an administrator.
  // It may legitimately include HTML entities and minor HTML, but
  // it should not include dangerous HTML.
  //
  // Because it may include HTML, we cannot pass it directly to t()
  // or let a TWIG template use {{ }}, both of which will process
  // the text and corrupt any entered HTML or HTML entities.
  //
  // We therefore use an Xss filter to remove any egreggious HTML
  // (such as scripts), and then FormattableMarkup() to mark the
  // resulting text as safe.
  $listSeparator = Xss::filterAdmin($this
    ->getSetting('listSeparator'));
  $build['#list_separator'] = new FormattableMarkup($listSeparator, []);
  return $build;
}