Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / hal / src / Normalizer / FileEntityNormalizer.php
index ec870e9e14c6df6c306e62a73216ca468a24ab3d..5186f8b2559440ae037eeeb82bedb429de19e59b 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Drupal\hal\Normalizer;
 
+use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Entity\EntityManagerInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\hal\LinkManager\LinkManagerInterface;
@@ -9,6 +10,8 @@ use GuzzleHttp\ClientInterface;
 
 /**
  * Converts the Drupal entity object structure to a HAL array structure.
+ *
+ * @deprecated in Drupal 8.5.0, to be removed before Drupal 9.0.0.
  */
 class FileEntityNormalizer extends ContentEntityNormalizer {
 
@@ -26,6 +29,13 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
    */
   protected $httpClient;
 
+  /**
+   * The HAL settings config.
+   *
+   * @var \Drupal\Core\Config\ImmutableConfig
+   */
+  protected $halSettings;
+
   /**
    * Constructs a FileEntityNormalizer object.
    *
@@ -37,11 +47,14 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
    *   The hypermedia link manager.
    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
    *   The module handler.
+   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
+   *   The config factory.
    */
-  public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler) {
+  public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
     parent::__construct($link_manager, $entity_manager, $module_handler);
 
     $this->httpClient = $http_client;
+    $this->halSettings = $config_factory->get('hal.settings');
   }
 
   /**
@@ -49,8 +62,13 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
    */
   public function normalize($entity, $format = NULL, array $context = []) {
     $data = parent::normalize($entity, $format, $context);
-    // Replace the file url with a full url for the file.
-    $data['uri'][0]['value'] = $this->getEntityUri($entity);
+
+    $this->addCacheableDependency($context, $this->halSettings);
+
+    if ($this->halSettings->get('bc_file_uri_as_url_normalizer')) {
+      // Replace the file url with a full url for the file.
+      $data['uri'][0]['value'] = $this->getEntityUri($entity);
+    }
 
     return $data;
   }