5aaed3cdcbb12869e4afb3d95e4c54f5cc7322d5
[yaffs-website] / web / core / modules / hal / src / Normalizer / FileEntityNormalizer.php
1 <?php
2
3 namespace Drupal\hal\Normalizer;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\hal\LinkManager\LinkManagerInterface;
9
10 /**
11  * Converts the Drupal entity object structure to a HAL array structure.
12  *
13  * @deprecated in Drupal 8.5.0, to be removed before Drupal 9.0.0.
14  */
15 class FileEntityNormalizer extends ContentEntityNormalizer {
16
17   /**
18    * The interface or class that this Normalizer supports.
19    *
20    * @var string
21    */
22   protected $supportedInterfaceOrClass = 'Drupal\file\FileInterface';
23
24   /**
25    * The HTTP client.
26    *
27    * @var \GuzzleHttp\ClientInterface
28    */
29   protected $httpClient;
30
31   /**
32    * The HAL settings config.
33    *
34    * @var \Drupal\Core\Config\ImmutableConfig
35    */
36   protected $halSettings;
37
38   /**
39    * Constructs a FileEntityNormalizer object.
40    *
41    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
42    *   The entity manager.
43    * @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
44    *   The hypermedia link manager.
45    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
46    *   The module handler.
47    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
48    *   The config factory.
49    */
50   public function __construct(EntityManagerInterface $entity_manager, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
51     parent::__construct($link_manager, $entity_manager, $module_handler);
52
53     $this->halSettings = $config_factory->get('hal.settings');
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function normalize($entity, $format = NULL, array $context = []) {
60     $data = parent::normalize($entity, $format, $context);
61
62     $this->addCacheableDependency($context, $this->halSettings);
63
64     if ($this->halSettings->get('bc_file_uri_as_url_normalizer')) {
65       // Replace the file url with a full url for the file.
66       $data['uri'][0]['value'] = $this->getEntityUri($entity);
67     }
68
69     return $data;
70   }
71
72 }