You are here

protected function Base::prepareItems in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/Base.php \Drupal\double_field\Plugin\Field\FieldFormatter\Base::prepareItems()

Prepare field items.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: List of field items.

1 call to Base::prepareItems()
Base::view in src/Plugin/Field/FieldFormatter/Base.php
Builds a renderable array for a fully themed field.

File

src/Plugin/Field/FieldFormatter/Base.php, line 239

Class

Base
Base class for Double field formatters.

Namespace

Drupal\double_field\Plugin\Field\FieldFormatter

Code

protected function prepareItems(FieldItemListInterface $items) : void {
  $field_settings = $this
    ->getFieldSettings();
  $settings = $this
    ->getSettings();
  foreach ($items as $delta => $item) {
    foreach ([
      'first',
      'second',
    ] as $subfield) {
      if ($settings[$subfield]['hidden']) {
        $item->{$subfield} = NULL;
      }
      else {
        $type = $field_settings['storage'][$subfield]['type'];
        if ($type == 'boolean') {
          $item->{$subfield} = $field_settings[$subfield][$item->{$subfield} ? 'on_label' : 'off_label'];
        }

        // Empty string should already be converted into NULL.
        // @see Drupal\double_field\Plugin\Field\FieldWidget\DoubleField::massageFormValues()
        if ($item->{$subfield} === NULL) {
          continue;
        }
        if ($type == 'numeric' || $type == 'float' || $type == 'integer') {
          $item->{$subfield} = $this
            ->numberFormat($subfield, $item->{$subfield});
        }
        if ($type == 'datetime_iso8601' && $item->{$subfield} && !$field_settings[$subfield]['list']) {

          // We follow the same principles as Drupal Core.
          // In the case of a datetime subfield, the date must be parsed using
          // the storage time zone and converted to the user's time zone while
          // a date-only field should have no timezone conversion performed.
          $timezone = $field_settings['storage'][$subfield]['datetime_type'] === 'datetime' ? date_default_timezone_get() : DoubleFieldItem::DATETIME_STORAGE_TIMEZONE;
          $timestamp = $items[$delta]
            ->createDate($subfield)
            ->getTimestamp();
          $date_formatter = static::getDateFormatter();
          $item->{$subfield} = [
            '#theme' => 'time',
            '#text' => $date_formatter
              ->format($timestamp, $settings[$subfield]['format_type'], '', $timezone),
            '#html' => FALSE,
            '#attributes' => [
              'datetime' => $date_formatter
                ->format($timestamp, 'custom', 'Y-m-d\\TH:i:s') . 'Z',
            ],
            '#cache' => [
              'contexts' => [
                'timezone',
              ],
            ],
          ];
        }
        $original_value[$subfield] = $item->{$subfield};
        if ($field_settings[$subfield]['list']) {

          // @todo Remove the fallback in 5.x.
          $display_key = $settings[$subfield]['key'] ?? FALSE;
          if (!$display_key) {

            // Replace the value with its label if possible.
            $item->{$subfield} = $field_settings[$subfield]['allowed_values'][$item->{$subfield}] ?? NULL;
          }
        }
        if (!empty($settings[$subfield]['link'])) {
          $value = isset($original_value) ? $original_value[$subfield] : $item->{$subfield};
          switch ($type) {
            case 'email':
              $item->{$subfield} = [
                '#type' => 'link',
                '#title' => $item->{$subfield},
                '#url' => Url::fromUri('mailto:' . $value),
              ];
              break;
            case 'telephone':
              $item->{$subfield} = [
                '#type' => 'link',
                '#title' => $item->{$subfield},
                '#url' => Url::fromUri('tel:' . rawurlencode(preg_replace('/\\s+/', '', $value))),
                '#options' => [
                  'external' => TRUE,
                ],
              ];
              break;
            case 'uri':
              $item->{$subfield} = [
                '#type' => 'link',
                '#title' => $item->{$subfield},
                '#url' => Url::fromUri($value),
                '#options' => [
                  'external' => TRUE,
                ],
              ];
              break;
          }
        }
      }
    }
    $items[$delta] = $item;
  }
}