You are here

public function FeedsItemImportedFormatter::viewElements in Feeds 8.3

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 TimestampFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/FeedsItemImportedFormatter.php, line 24

Class

FeedsItemImportedFormatter
Plugin implementation of the 'feeds_item_imported' formatter.

Namespace

Drupal\feeds\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $date_format = $this
    ->getSetting('date_format');
  $custom_date_format = '';
  $timezone = $this
    ->getSetting('timezone') ?: NULL;
  $langcode = NULL;

  // If an RFC2822 date format is requested, then the month and day have to
  // be in English. @see http://www.faqs.org/rfcs/rfc2822.html
  if ($date_format === 'custom' && ($custom_date_format = $this
    ->getSetting('custom_date_format')) === 'r') {
    $langcode = 'en';
  }
  foreach ($items as $delta => $item) {
    if (!empty($item->imported)) {
      $elements[$delta] = [
        '#cache' => [
          'contexts' => [
            'timezone',
          ],
        ],
        '#markup' => $this->dateFormatter
          ->format($item
          ->get('imported')
          ->getValue(), $date_format, $custom_date_format, $timezone, $langcode),
      ];
    }
  }
  return $elements;
}