Last active
November 17, 2019 14:21
-
-
Save cyberlex404/55dd5cb91b1f55516b0ef2d6014e4a3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\arti_custom\Plugin\ExtraField\Display; | |
use Drupal\Component\Render\FormattableMarkup; | |
use Drupal\Core\Entity\ContentEntityInterface; | |
use Drupal\Core\Link; | |
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
use Drupal\Core\StringTranslation\StringTranslationTrait; | |
use Drupal\Core\Url; | |
use Drupal\extra_field\Plugin\ExtraFieldDisplayBase; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpFoundation\RequestStack; | |
/** | |
* Document links Extra field Display. | |
* | |
* @ExtraFieldDisplay( | |
* id = "document_links", | |
* label = @Translation("Document links"), | |
* bundles = { | |
* "node.document", | |
* } | |
* ) | |
*/ | |
class DocumentLinks extends ExtraFieldDisplayBase implements ContainerFactoryPluginInterface { | |
use StringTranslationTrait; | |
/** | |
* The current request object. | |
* | |
* @var \Symfony\Component\HttpFoundation\Request | |
*/ | |
protected $currentRequest; | |
/** | |
* Constructs a ExtraFieldDisplayFormattedBase object. | |
* | |
* @param array $configuration | |
* A configuration array containing information about the plugin instance. | |
* @param string $plugin_id | |
* The plugin_id for the plugin instance. | |
* @param mixed $plugin_definition | |
* The plugin implementation definition. | |
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack | |
* The request stack. | |
*/ | |
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack) { | |
parent::__construct($configuration, $plugin_id, $plugin_definition); | |
$this->currentRequest = $request_stack->getCurrentRequest(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
return new static( | |
$configuration, $plugin_id, $plugin_definition, | |
$container->get('request_stack') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function view(ContentEntityInterface $entity) { | |
$elements['#theme'] = 'document_links'; | |
/** @var \Drupal\Core\Field\EntityReferenceFieldItemList $mediaFiles */ | |
$mediaFiles = $entity->get('field_files'); | |
foreach ($mediaFiles->getIterator() as $item) { | |
/** @var \Drupal\media\MediaInterface $media */ | |
$media = $item->entity; | |
/** @var $file \Drupal\file\FileInterface */ | |
$file = $media->get('field_media_file')->entity; | |
$mimeType = $file->getMimeType(); | |
$url = Url::fromUri(file_create_url($file->getFileUri())); | |
if ($mimeType == 'application/pdf') { | |
$elements['#is_pdf'] = TRUE; | |
$elements['#pdf'] = Link::fromTextAndUrl(t('Read @doctype', ['@doctype' => '.pdf']), $url)->toRenderable(); | |
} | |
else { | |
$elements['#is_word'] = TRUE; | |
$elements['#word'] = Link::fromTextAndUrl(t('Read @doctype', ['@doctype' => '.doc(x)']), $url)->toRenderable(); | |
\Drupal::logger('arti')->debug('Mime type: ' . $mimeType . ' ' . $file->getFileUri()); | |
} | |
} | |
if (empty($elements['#pdf']) || $elements['#is_pdf'] == FALSE) { | |
$elements['#is_pdf'] = FALSE; | |
$elements['#pdf'] = new FormattableMarkup('Read @doctype', ['@doctype' => '.pdf']); | |
} | |
if (empty($elements['#word']) || $elements['#is_word'] == FALSE) { | |
$elements['#is_word'] = FALSE; | |
$elements['#word'] = new FormattableMarkup('Read @doctype', ['@doctype' => '.doc(x)']); | |
} | |
return $elements; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment