<?php
namespace Drupal\maintenance_2020_01\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'Maintenance Documents' formatter.
*
* @FieldFormatter(
* id = "maintenance_documents",
* label = @Translation("Maintenance Documents"),
* field_types = {
* "file"
* }
* )
*/
class MaintenanceDocumentsFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
$description = $item->getValue()['description'];
$file = $item->entity;
$created_timestamp = $file->get('created')->value;
$element[$delta] = [
'#theme' => 'file_link',
'#file' => $file,
'#description' => $description . ' (' . \Drupal::service('date.formatter')->format($created_timestamp, 'custom', 'Y.m.d') . ')',
'#cache' => [
'tags' => $file->getCacheTags(),
],
];
}
return $element;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter