You are here

public function IpAddressDefaultFormatter::viewElements in IP address fields 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/IpAddressDefaultFormatter.php \Drupal\field_ipaddress\Plugin\Field\FieldFormatter\IpAddressDefaultFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/IpAddressDefaultFormatter.php, line 41

Class

IpAddressDefaultFormatter
Plugin implementation of the 'Default' formatter for 'datetime' fields.

Namespace

Drupal\field_ipaddress\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $value = $item
      ->getValue();
    if (!empty($value['ip_start'])) {
      $text = inet_ntop($value['ip_start']);
      if ($value['ip_start'] != $value['ip_end']) {
        $text .= '-' . inet_ntop($value['ip_end']);
      }
    }
    $elements[$delta] = [
      '#plain_text' => $text,
    ];
  }
  return $elements;
}