View source
<?php
namespace Drupal\swagger_ui_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\file\Entity\File;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SwaggerUIFileFormatter extends FileFormatterBase implements ContainerFactoryPluginInterface {
use SwaggerUIFormatterTrait;
private $fileEntityCache = [];
private $logger;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, TranslationInterface $string_translation, LoggerInterface $logger) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->stringTranslation = $string_translation;
$this->logger = $logger;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('string_translation'), $container
->get('logger.channel.swagger_ui_formatter'));
}
public static function defaultSettings() {
$settings = parent::defaultSettings();
static::addDefaultSettings($settings);
return $settings;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$this
->addSettingsSummary($summary, $this);
return $summary;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$this
->alterSettingsForm($form, $form_state, $this, $this->fieldDefinition);
return $form;
}
protected function getSwaggerFileUrlFromField(FieldItemInterface $field_item, array $context = []) {
if (!isset($this->fileEntityCache[$context['field_items']
->getEntity()
->id()])) {
$this->fileEntityCache[$context['field_items']
->getEntity()
->id()] = array_reduce($this
->getEntitiesToView($context['field_items'], $context['lang_code']), function (array $carry, File $entity) {
$carry[$entity
->id()] = $entity;
return $carry;
}, []);
}
if (isset($this->fileEntityCache[$context['field_items']
->getEntity()
->id()][$field_item
->getValue()['target_id']])) {
$file = $this->fileEntityCache[$context['field_items']
->getEntity()
->id()][$field_item
->getValue()['target_id']];
$url = file_create_url($file
->getFileUri());
if ($url === FALSE) {
$this->logger
->error('URL could not be created for %file file.', [
'%file' => $file
->label(),
'link' => $context['field_items']
->getEntity()
->toLink($this
->t('view'))
->toString(),
]);
return NULL;
}
return $url;
}
return NULL;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
return $this
->buildRenderArray($items, $this, $this->fieldDefinition, [
'lang_code' => $langcode,
]);
}
}