protected function SwaggerUIFileFormatter::getSwaggerFileUrlFromField in Swagger UI Field Formatter 8.2
Same name and namespace in other branches
- 8.3 src/Plugin/Field/FieldFormatter/SwaggerUIFileFormatter.php \Drupal\swagger_ui_formatter\Plugin\Field\FieldFormatter\SwaggerUIFileFormatter::getSwaggerFileUrlFromField()
Creates a web-accessible URL to a Swagger file from the field item.
Parameters
\Drupal\Core\Field\FieldItemInterface $field_item: The field item.
array $context: Additional context for creating the URL to the Swagger file.
Return value
string|null URL to the Swagger file or null if the URL could not be created.
Overrides SwaggerUIFormatterTrait::getSwaggerFileUrlFromField
File
- src/
Plugin/ Field/ FieldFormatter/ SwaggerUIFileFormatter.php, line 124
Class
- SwaggerUIFileFormatter
- Plugin implementation of Swagger UI file field formatter.
Namespace
Drupal\swagger_ui_formatter\Plugin\Field\FieldFormatterCode
protected function getSwaggerFileUrlFromField(FieldItemInterface $field_item, array $context = []) {
if (!isset($this->fileEntityCache[$context['field_items']
->getEntity()
->id()])) {
// Store file entities keyed by their 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;
}, []);
}
// This is only set if the file entity exists and the current user has
// access to the entity.
if (isset($this->fileEntityCache[$context['field_items']
->getEntity()
->id()][$field_item
->getValue()['target_id']])) {
/** @var \Drupal\file\Entity\File $file */
$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;
}