Skip to content

Instantly share code, notes, and snippets.

@leymannx
Last active August 2, 2018 15:42
Show Gist options
  • Save leymannx/1171c407fe76e10a04fff6f6e6651c5b to your computer and use it in GitHub Desktop.
Save leymannx/1171c407fe76e10a04fff6f6e6651c5b to your computer and use it in GitHub Desktop.
Field formatter snippet for the fully working sample module on https://github.com/leymannx/mffe
<?php
namespace Drupal\mffe\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\media\Entity\Media;
use Drupal\file\Entity\File;
use Drupal\Core\Url;
/**
* Plugin implementation of the 'mffe_download_slides' formatter.
*
* @FieldFormatter(
* id = "mffe_download_slides",
* label = @Translation("Download Slides"),
* field_types = {
* "entity_reference"
* }
* )
*/
class DownloadSlidesFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
// Get the media item.
$media_id = $item->getValue()['target_id'];
$media_item = Media::load($media_id);
// Get the file.
$file_id = $media_item->field_media_file->getValue()[0]['target_id'];
$file = File::load($file_id);
// Get the URL.
$uri = $file->getFileUri();
$url = Url::fromUri(file_create_url($uri))->toString();
// Output.
$elements[$delta] = [
'#type' => 'inline_template',
'#template' => '<a href="{{ url }}" target="_blank">Download Slides</a>',
'#context' => [
'url' => $url,
],
];
//$elements[$delta] = [
// '#theme' => 'mffe_download_slides',
// '#url' => $url,
//];
}
return $elements;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment