Same filename in this branch
- 10 core/modules/file/src/Entity/File.php
- 10 core/lib/Drupal/Core/Render/Element/File.php
- 10 core/modules/file/src/Plugin/views/field/File.php
- 10 core/modules/file/src/Plugin/views/wizard/File.php
- 10 core/modules/media/src/Plugin/media/Source/File.php
- 10 core/modules/file/src/Plugin/migrate/source/d6/File.php
- 10 core/modules/file/src/Plugin/migrate/source/d7/File.php
Same filename and directory in other branches
- 8.9.x core/modules/file/src/Plugin/views/field/File.php
- 9 core/modules/file/src/Plugin/views/field/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\Attribute\ViewsField;
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;
class File extends FieldPluginBase {
protected $fileUrlGenerator;
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;
}
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'));
}
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';
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_file'] = [
'default' => FALSE,
];
return $options;
}
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);
}
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;
}
public function render(ResultRow $values) {
$value = $this
->getValue($values);
return $this
->renderLink($this
->sanitizeValue($value), $values);
}
}
Classes