Skip to content
Snippets Groups Projects

Introduced FileTransformBase class for File field transforms

3 files
+ 88
33
Compare changes
  • Side-by-side
  • Inline
Files
3
<?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;
}
}
Loading