ComputedFileUrl.php 858 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Drupal\file;
  3. use Drupal\Core\TypedData\TypedData;
  4. /**
  5. * Computed file URL property class.
  6. */
  7. class ComputedFileUrl extends TypedData {
  8. /**
  9. * Computed root-relative file URL.
  10. *
  11. * @var string
  12. */
  13. protected $url = NULL;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function getValue() {
  18. if ($this->url !== NULL) {
  19. return $this->url;
  20. }
  21. assert($this->getParent()->getEntity() instanceof FileInterface);
  22. $uri = $this->getParent()->getEntity()->getFileUri();
  23. $this->url = file_url_transform_relative(file_create_url($uri));
  24. return $this->url;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function setValue($value, $notify = TRUE) {
  30. $this->url = $value;
  31. // Notify the parent of any changes.
  32. if ($notify && isset($this->parent)) {
  33. $this->parent->onChange($this->name);
  34. }
  35. }
  36. }