You are here

class File in Drupal 10

Same name in this branch
  1. 10 core/modules/file/src/Entity/File.php \Drupal\file\Entity\File
  2. 10 core/lib/Drupal/Core/Render/Element/File.php \Drupal\Core\Render\Element\File
  3. 10 core/modules/file/src/Plugin/views/wizard/File.php \Drupal\file\Plugin\views\wizard\File
  4. 10 core/modules/file/src/Plugin/views/field/File.php \Drupal\file\Plugin\views\field\File
  5. 10 core/modules/media/src/Plugin/media/Source/File.php \Drupal\media\Plugin\media\Source\File
  6. 10 core/modules/file/src/Plugin/migrate/source/d6/File.php \Drupal\file\Plugin\migrate\source\d6\File
  7. 10 core/modules/file/src/Plugin/migrate/source/d7/File.php \Drupal\file\Plugin\migrate\source\d7\File
Same name and namespace in other branches
  1. 8 core/modules/file/src/Plugin/views/field/File.php \Drupal\file\Plugin\views\field\File
  2. 9 core/modules/file/src/Plugin/views/field/File.php \Drupal\file\Plugin\views\field\File

Field handler to provide simple renderer that allows linking to a file.

Plugin annotation

@ViewsField("file");

Hierarchy

  • class \Drupal\file\Plugin\views\field\File extends \Drupal\views\Plugin\views\field\FieldPluginBase

Expanded class hierarchy of File

19 string references to 'File'
ConfigTest::provideGetMessageText in core/tests/Drupal/Tests/Composer/Plugin/ProjectMessage/ConfigTest.php
DirectoryTest::testFileCheckDirectoryHandling in core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
Tests directory handling functions.
DirectoryTest::testFileCheckLocalDirectoryHandling in core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
Tests local directory handling functions.
drupal6.php in core/modules/migrate_drupal/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
file.info.yml in core/modules/file/file.info.yml
core/modules/file/file.info.yml

... See full list

File

core/modules/file/src/Plugin/views/field/File.php, line 20

Namespace

Drupal\file\Plugin\views\field
View source
class File extends FieldPluginBase {

  /**
   * The file URL generator.
   *
   * @var \Drupal\Core\File\FileUrlGeneratorInterface
   */
  protected $fileUrlGenerator;

  /**
   * Constructs a File object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
   *   The file URL generator.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FileUrlGeneratorInterface $file_url_generator) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->fileUrlGenerator = $file_url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('file_url_generator'));
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    if (!empty($options['link_to_file'])) {
      $this->additional_fields['uri'] = 'uri';
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['link_to_file'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * Provide link to file option.
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $form['link_to_file'] = [
      '#title' => $this
        ->t('Link this field to download the file'),
      '#description' => $this
        ->t("Enable to override this field's links."),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['link_to_file']),
    ];
    parent::buildOptionsForm($form, $form_state);
  }

  /**
   * Prepares link to the file.
   *
   * @param string $data
   *   The XSS safe string for the link text.
   * @param \Drupal\views\ResultRow $values
   *   The values retrieved from a single row of a view's query result.
   *
   * @return string
   *   Returns a string for the link text.
   */
  protected function renderLink($data, ResultRow $values) {
    if (!empty($this->options['link_to_file']) && $data !== NULL && $data !== '') {
      $this->options['alter']['make_link'] = TRUE;
      $this->options['alter']['url'] = $this->fileUrlGenerator
        ->generate($this
        ->getValue($values, 'uri'));
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $value = $this
      ->getValue($values);
    return $this
      ->renderLink($this
      ->sanitizeValue($value), $values);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
File::$fileUrlGenerator protected property The file URL generator.
File::buildOptionsForm public function Provide link to file option.
File::create public static function
File::defineOptions protected function
File::init public function
File::render public function
File::renderLink protected function Prepares link to the file.
File::__construct public function Constructs a File object.