class FileEntityNormalizer in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/hal/src/Normalizer/FileEntityNormalizer.php \Drupal\hal\Normalizer\FileEntityNormalizer
Converts the Drupal entity object structure to a HAL array structure.
Hierarchy
- class \Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer implements SerializerAwareInterface
- class \Drupal\serialization\Normalizer\NormalizerBase implements NormalizerInterface
- class \Drupal\hal\Normalizer\NormalizerBase implements DenormalizerInterface
- class \Drupal\hal\Normalizer\ContentEntityNormalizer
- class \Drupal\hal\Normalizer\FileEntityNormalizer
- class \Drupal\hal\Normalizer\ContentEntityNormalizer
- class \Drupal\hal\Normalizer\NormalizerBase implements DenormalizerInterface
- class \Drupal\serialization\Normalizer\NormalizerBase implements NormalizerInterface
Expanded class hierarchy of FileEntityNormalizer
1 file declares its use of FileEntityNormalizer
- FileNormalizeTest.php in core/
modules/ hal/ src/ Tests/ FileNormalizeTest.php - Contains \Drupal\hal\Tests\FileNormalizeTest.
1 string reference to 'FileEntityNormalizer'
- hal.services.yml in core/
modules/ hal/ hal.services.yml - core/modules/hal/hal.services.yml
1 service uses FileEntityNormalizer
File
- core/
modules/ hal/ src/ Normalizer/ FileEntityNormalizer.php, line 18 - Contains \Drupal\hal\Normalizer\FileEntityNormalizer.
Namespace
Drupal\hal\NormalizerView source
class FileEntityNormalizer extends ContentEntityNormalizer {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = 'Drupal\\file\\FileInterface';
/**
* The HTTP client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Constructs a FileEntityNormalizer object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP Client.
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($link_manager, $entity_manager, $module_handler);
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = array()) {
$data = parent::normalize($entity, $format, $context);
// Replace the file url with a full url for the file.
$data['uri'][0]['value'] = $this
->getEntityUri($entity);
return $data;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
$file_data = (string) $this->httpClient
->get($data['uri'][0]['value'])
->getBody();
$path = 'temporary://' . drupal_basename($data['uri'][0]['value']);
$data['uri'] = file_unmanaged_save_data($file_data, $path);
return $this->entityManager
->getStorage('file')
->create($data);
}
}