summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/src/ComputedFileUrl.php
blob: 276f2c7d8f744025708c57c91dde9df55dc33c99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

namespace Drupal\file;

use Drupal\Core\TypedData\TypedData;

/**
 * Computed file URL property class.
 */
class ComputedFileUrl extends TypedData {

  /**
   * Computed root-relative file URL.
   *
   * @var string
   */
  protected $url = NULL;

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    if ($this->url !== NULL) {
      return $this->url;
    }

    assert($this->getParent()->getEntity() instanceof FileInterface);

    $uri = $this->getParent()->getEntity()->getFileUri();
    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $this->url = $file_url_generator->generateString($uri);

    return $this->url;
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($value, $notify = TRUE) {
    $this->url = $value;

    // Notify the parent of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  }

}