You are here

public function ListFormatter::viewElements in Text list formatter 8.2

Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().

File

lib/Drupal/textformatter/Plugin/field/formatter/ListFormatter.php, line 195
Definition of Drupal\textformatter\Plugin\field\formatter\List;

Class

ListFormatter
Plugin implementation of the 'text_default' formatter.

Namespace

Drupal\textformatter\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {
  $module = $this->field['module'];
  $field_type = $this->field['type'];
  $textformatter_info = $this
    ->fieldListInfo();
  $elements = $list_items = array();
  if (!empty($textformatter_info[$module]['callback']) && in_array($field_type, $textformatter_info[$module]['fields'])) {
    $function = $textformatter_info[$module]['callback'];
    if (function_exists($function)) {

      // Support existing function implementations.
      $display = array(
        'type' => $this
          ->getPluginId(),
        'settings' => $this
          ->getSettings(),
        'weight' => $this->weight,
        'label' => $this->label,
      );
      $list_items = $function($entity
        ->entityType(), $entity, $this->field, $this->instance, $langcode, $items, $display);
    }
    else {
      drupal_set_message(t('function @function does not exist.', array(
        '@function' => $function,
      )), 'error');
    }
  }
  else {
    foreach ($items as $delta => $item) {
      $list_items = $this
        ->defaultFieldList($entity, $langcode, $items);
    }
  }

  // If there are no list items, return and render nothing.
  if (empty($list_items)) {
    return;
  }
  $type = $this
    ->getSetting('type');

  // CSS classes are checked for validity on submission. drupal_attributes()
  // runs each attribute value through check_plain().
  $classes = explode(' ', $this
    ->getSetting('class'));
  switch ($type) {
    case 'ul':
    case 'ol':

      // Render as one element, item list.
      $elements[] = array(
        '#theme' => 'item_list',
        '#type' => $type,
        '#items' => $list_items,
        '#attributes' => array(
          'class' => $classes,
        ),
      );
      break;
    case 'comma':

      // Render as one element, comma separated list.
      $elements[] = array(
        '#theme' => 'textformatter_comma',
        '#items' => $list_items,
        '#formatter' => $this,
        '#attributes' => array(
          'class' => $classes,
        ),
      );
      break;
  }
  return $elements;
}