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"),
21 class FileUriFormatter extends BaseFieldFileFormatterBase {
26 public static function defaultSettings() {
27 $settings = parent::defaultSettings();
29 $settings['file_download_path'] = FALSE;
36 public function settingsForm(array $form, FormStateInterface $form_state) {
37 $form = parent::settingsForm($form, $form_state);
39 $form['file_download_path'] = [
40 '#title' => $this->t('Display the file download URI'),
41 '#type' => 'checkbox',
42 '#default_value' => $this->getSetting('file_download_path'),
51 protected function viewValue(FieldItemInterface $item) {
52 $value = $item->value;
53 if ($this->getSetting('file_download_path')) {
54 // @todo Wrap in file_url_transform_relative(). This is currently
55 // impossible. See BaseFieldFileFormatterBase::viewElements(). Fix in
56 // https://www.drupal.org/node/2646744.
57 $value = file_create_url($value);
65 public static function isApplicable(FieldDefinitionInterface $field_definition) {
66 return parent::isApplicable($field_definition) && $field_definition->getName() === 'uri';