File.php

Same filename in this branch
  1. 9 core/modules/media/src/Plugin/media/Source/File.php
  2. 9 core/modules/file/src/Entity/File.php
  3. 9 core/modules/file/src/Plugin/views/wizard/File.php
  4. 9 core/modules/file/src/Plugin/migrate/source/d6/File.php
  5. 9 core/modules/file/src/Plugin/migrate/source/d7/File.php
  6. 9 core/lib/Drupal/Core/Render/Element/File.php
Same filename in other branches
  1. 8.9.x core/modules/media/src/Plugin/media/Source/File.php
  2. 8.9.x core/modules/file/src/Entity/File.php
  3. 8.9.x core/modules/file/src/Plugin/views/field/File.php
  4. 8.9.x core/modules/file/src/Plugin/views/wizard/File.php
  5. 8.9.x core/modules/file/src/Plugin/migrate/source/d6/File.php
  6. 8.9.x core/modules/file/src/Plugin/migrate/source/d7/File.php
  7. 8.9.x core/lib/Drupal/Core/Render/Element/File.php
  8. 10 core/modules/media/src/Plugin/media/Source/File.php
  9. 10 core/modules/file/src/Entity/File.php
  10. 10 core/modules/file/src/Plugin/views/field/File.php
  11. 10 core/modules/file/src/Plugin/views/wizard/File.php
  12. 10 core/modules/file/src/Plugin/migrate/source/d6/File.php
  13. 10 core/modules/file/src/Plugin/migrate/source/d7/File.php
  14. 10 core/lib/Drupal/Core/Render/Element/File.php
  15. 11.x core/modules/media/src/Plugin/media/Source/File.php
  16. 11.x core/modules/file/src/Entity/File.php
  17. 11.x core/modules/file/src/Plugin/views/field/File.php
  18. 11.x core/modules/file/src/Plugin/views/wizard/File.php
  19. 11.x core/modules/file/src/Plugin/migrate/source/d6/File.php
  20. 11.x core/modules/file/src/Plugin/migrate/source/d7/File.php
  21. 11.x core/lib/Drupal/Core/Render/Element/File.php

Namespace

Drupal\file\Plugin\views\field

File

core/modules/file/src/Plugin/views/field/File.php

View source
<?php

namespace Drupal\file\Plugin\views\field;

use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Field handler to provide simple renderer that allows linking to a file.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("file")
 */
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 = NULL) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        if (!$file_url_generator) {
            @trigger_error('Calling File::__construct() without the $file_url_generator argument is deprecated in drupal:9.3.0 and the $file_url_generator argument will be required in drupal:10.0.0. See https://www.drupal.org/node/2940031', E_USER_DEPRECATED);
            $file_url_generator = \Drupal::service('file_url_generator');
        }
        $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);
    }

}

Classes

Title Deprecated Summary
File Field handler to provide simple renderer that allows linking to a file.

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