class ComputedFileUrl

Same name and namespace in other branches
  1. 10 core/modules/file/src/ComputedFileUrl.php \Drupal\file\ComputedFileUrl
  2. 11.x core/modules/file/src/ComputedFileUrl.php \Drupal\file\ComputedFileUrl
  3. 8.9.x core/modules/file/src/ComputedFileUrl.php \Drupal\file\ComputedFileUrl

Computed file URL property class.

Hierarchy

Expanded class hierarchy of ComputedFileUrl

2 files declare their use of ComputedFileUrl
ComputedFileUrlTest.php in core/modules/file/tests/src/Kernel/ComputedFileUrlTest.php
FileUriItem.php in core/modules/file/src/Plugin/Field/FieldType/FileUriItem.php

File

core/modules/file/src/ComputedFileUrl.php, line 10

Namespace

Drupal\file
View source
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);
    }
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.