3 namespace Drupal\file\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * Formatter to render the file URI to its download path.
14 * label = @Translation("File URI"),
20 class FileUriFormatter extends BaseFieldFileFormatterBase {
25 public static function defaultSettings() {
26 $settings = parent::defaultSettings();
28 $settings['file_download_path'] = FALSE;
35 public function settingsForm(array $form, FormStateInterface $form_state) {
36 $form = parent::settingsForm($form, $form_state);
38 $form['file_download_path'] = [
39 '#title' => $this->t('Display the file download URI'),
40 '#type' => 'checkbox',
41 '#default_value' => $this->getSetting('file_download_path'),
50 protected function viewValue(FieldItemInterface $item) {
51 $value = $item->value;
52 if ($this->getSetting('file_download_path')) {
53 // @todo Wrap in file_url_transform_relative(). This is currently
54 // impossible. See BaseFieldFileFormatterBase::viewElements(). Fix in
55 // https://www.drupal.org/node/2646744.
56 $value = file_create_url($value);
64 public static function isApplicable(FieldDefinitionInterface $field_definition) {
65 return parent::isApplicable($field_definition) && $field_definition->getName() === 'uri';