Skip to content
Snippets Groups Projects
Commit 1d9a7459 authored by Erik Petra's avatar Erik Petra Committed by Martin Giessing
Browse files

Issue #3413192 by erik_petra: Create FileTransformBase class

parent 20dd2ebd
No related branches found
No related tags found
1 merge request!6Introduced FileTransformBase class for File field transforms
<?php
namespace Drupal\transform_api\Plugin\Transform\Field;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\transform_api\FieldTransformBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for File field transform plugins.
*/
abstract class FileTransformBase extends FieldTransformBase {
/**
* File url generator.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
protected $fileUrlGenerator;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a FieldTransformBase object.
*
* @param string $plugin_id
* The plugin_id for the transform.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the transform is associated.
* @param array $settings
* The transform settings.
* @param string $label
* The transform label display setting.
* @param string $transform_mode
* The transform mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
* The file url generator.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $transform_mode, array $third_party_settings, FileUrlGeneratorInterface $file_url_generator, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $transform_mode, $third_party_settings);
$this->fileUrlGenerator = $file_url_generator;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
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['transform_mode'], $configuration['third_party_settings'], $container->get('file_url_generator'), $container->get('entity_type.manager'));
}
/**
* Loads file entity.
*
* @param $fid
*
* @return \Drupal\file\FileInterface|null
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function loadFile($fid) {
/** @var \Drupal\file\FileInterface|null $file */
$file = $this->entityTypeManager->getStorage('file')->load($fid);
return $file;
}
}
...@@ -17,7 +17,7 @@ use Drupal\transform_api\FieldTransformBase; ...@@ -17,7 +17,7 @@ use Drupal\transform_api\FieldTransformBase;
* } * }
* ) * )
*/ */
class FileURLTransform extends FieldTransformBase { class FileURLTransform extends FileTransformBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -30,10 +30,10 @@ class FileURLTransform extends FieldTransformBase { ...@@ -30,10 +30,10 @@ class FileURLTransform extends FieldTransformBase {
// File ID. // File ID.
$fid = $item->getValue()['target_id']; $fid = $item->getValue()['target_id'];
// Load file. // Load file.
$file = File::load($fid); $file = $this->loadFile($fid);
$file_uri = $file->getFileUri(); $file_uri = $file->getFileUri();
$values[] = \Drupal::service('file_url_generator')->generateString($file_uri); $values[] = $this->fileUrlGenerator->generateString($file_uri);
} }
else { else {
$values[] = NULL; $values[] = NULL;
......
...@@ -2,12 +2,7 @@ ...@@ -2,12 +2,7 @@
namespace Drupal\transform_api\Plugin\Transform\Field; namespace Drupal\transform_api\Plugin\Transform\Field;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileUrlGenerator;
use Drupal\file\Entity\File;
use Drupal\transform_api\FieldTransformBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Transform field plugin for file field types as Urls. * Transform field plugin for file field types as Urls.
...@@ -20,30 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -20,30 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* } * }
* ) * )
*/ */
class FileUrlSizeTransform extends FieldTransformBase { class FileUrlSizeTransform extends FileTransformBase {
/**
* File url generator.
*
* @var \Drupal\Core\File\FileUrlGenerator
*/
protected $fileUrlGenerator;
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $transform_mode, array $third_party_settings, FileUrlGenerator $file_url_generator) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $transform_mode, $third_party_settings);
$this->fileUrlGenerator = $file_url_generator;
}
/**
* {@inheritdoc}
*/
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['transform_mode'], $configuration['third_party_settings'], $container->get('file_url_generator'));
}
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -56,7 +28,7 @@ class FileUrlSizeTransform extends FieldTransformBase { ...@@ -56,7 +28,7 @@ class FileUrlSizeTransform extends FieldTransformBase {
// File ID. // File ID.
$fid = $item->getValue()['target_id']; $fid = $item->getValue()['target_id'];
// Load file. // Load file.
$file = File::load($fid); $file = $this->loadFile($fid);
$file_uri = $file->getFileUri(); $file_uri = $file->getFileUri();
$values[] = [ $values[] = [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment