Commit 7f90ee63 authored by alexpott's avatar alexpott

Issue #2456709 by dawehner, rteijeiro: File views handlers need to be replaced...

Issue #2456709 by dawehner, rteijeiro: File views handlers need to be replaced with entity-aware formatters
parent a4e51c82
......@@ -159,7 +159,6 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_file: false
relationship: none
group_type: group
admin_label: ''
......@@ -175,30 +174,45 @@ display:
element_default_classes: true
empty: ''
hide_alter_empty: true
plugin_id: file
plugin_id: field
entity_type: file
entity_field: fid
filename:
id: filename
table: file_managed
field: filename
relationship: none
group_type: group
admin_label: ''
label: Name
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
trim: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: false
ellipsis: false
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
hide_empty: false
empty_zero: false
link_to_file: true
relationship: none
group_type: group
admin_label: ''
label: Name
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
......@@ -208,8 +222,22 @@ display:
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
plugin_id: file
click_sort_column: value
type: file_link
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: 0
plugin_id: field
entity_type: file
entity_field: filename
filemime:
......@@ -260,9 +288,8 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
link_to_file: false
filemime_image: false
plugin_id: file_filemime
type: file_filemime
plugin_id: field
entity_type: file
entity_field: filemime
filesize:
......@@ -313,8 +340,8 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
file_size_display: formatted
plugin_id: file_size
type: file_size
plugin_id: field
entity_type: file
entity_field: filesize
status:
......@@ -365,7 +392,12 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
plugin_id: file_status
type: boolean
settings:
format: custom
format_custom_false: 'Temporary'
format_custom_true: 'Permanent'
plugin_id: field
entity_type: file
entity_field: status
created:
......@@ -679,6 +711,12 @@ display:
arguments: { }
group_by: true
show_admin_links: true
display_extenders: { }
cache_metadata:
contexts:
- languages
- url
cacheable: false
page_1:
display_plugin: page
id: page_1
......@@ -706,6 +744,12 @@ display:
group_type: group
admin_label: 'File usage'
required: false
display_extenders: { }
cache_metadata:
contexts:
- languages
- url
cacheable: false
page_2:
display_plugin: page
id: page_2
......@@ -1054,3 +1098,9 @@ display:
group_type: group
admin_label: 'File usage'
required: true
display_extenders: { }
cache_metadata:
contexts:
- languages
- url
cacheable: false
......@@ -90,3 +90,34 @@ field.widget.settings.file_generic:
progress_indicator:
type: string
label: 'Progress indicator'
field_formatter_settings_base_file:
type: mapping
mapping:
link_to_file:
type: boolean
label: 'Link to file'
field.formatter.settings.file_link:
type: field_formatter_settings_base_file
field.formatter.settings.file_uri:
type: field_formatter_settings_base_file
mapping:
file_download_path:
type: boolean
label: 'Display download path'
field.formatter.settings.file_filemime:
type: field_formatter_settings_base_file
mapping:
filemime_image:
type: boolean
label: 'Display the filemime as icon'
field.formatter.settings.file_extension:
type: field_formatter_settings_base_file
mapping:
extension_detect_tar:
type: boolean
label: 'Detect tar'
......@@ -29,10 +29,6 @@ views.field.file_filemime:
type: boolean
label: 'Display an icon representing the file type, instead of the MIME text (such as "image/jpeg")'
views.field.file_status:
type: views_field
label: 'File status'
views.field.file_uri:
type: views.field.file
label: 'File URI'
......
......@@ -25,7 +25,6 @@ public function getViewsData() {
$data['file_managed']['table']['base']['defaults']['field'] = 'filename';
$data['file_managed']['table']['wizard_id'] = 'file_managed';
$data['file_managed']['fid']['field']['id'] ='file';
$data['file_managed']['fid']['argument'] = array(
'id' => 'file_fid',
// The field to display in the summary.
......@@ -42,25 +41,30 @@ public function getViewsData() {
'label' => t('File usage'),
);
$data['file_managed']['filename']['field']['id'] = 'file';
$data['file_managed']['uri']['field']['default_formatter'] = 'file_uri';
$data['file_managed']['uri']['field']['id'] = 'file_uri';
$data['file_managed']['filemime']['field']['id'] = 'file_filemime';
$data['file_managed']['filemime']['field']['default_formatter'] = 'file_filemime';
$data['file_managed']['extension'] = array(
'title' => t('Extension'),
'help' => t('The extension of the file.'),
'real field' => 'filename',
'field' => array(
'id' => 'file_extension',
'entity_type' => 'file',
'field_name' => 'filename',
'default_formatter' => 'file_extension',
'id' => 'field',
'click sortable' => FALSE,
),
);
$data['file_managed']['filesize']['field']['id'] = 'file_size';
$data['file_managed']['filesize']['field']['default_formatter'] = 'file_size';
$data['file_managed']['status']['field']['id'] = 'file_status';
$data['file_managed']['status']['field']['default_formatter_settings'] = [
'format' => 'custom',
'format_custom_false' => $this->t('Temporary'),
'format_custom_true' => t('Permanent'),
];
$data['file_managed']['status']['filter']['id'] = 'file_status';
$data['file_managed']['uid']['relationship']['title'] = t('User who uploaded');
......
<?php
/**
* @file
* Contains \Drupal\file\Plugin\Field\FieldFormatter\BaseFieldFileFormatterBase.
*/
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Base class for file formatters, which allow to link to the file download URL.
*/
abstract class BaseFieldFileFormatterBase extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings['link_to_file'] = FALSE;
return $settings;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['link_to_file'] = [
'#title' => $this->t('Link this field to the file download URL'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('link_to_file'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = [];
$url = NULL;
// Add support to link to the entity itself.
if ($this->getSetting('link_to_file')) {
$url = file_create_url($items->getEntity()->uri->value);
}
foreach ($items as $delta => $item) {
$string = $this->viewValue($item);
if ($url) {
$elements[$delta] = [
'#type' => 'link',
'#title' => $string,
'#url' => Url::fromUri($url),
];
}
else {
$elements[$delta] = is_array($string) ? $string : ['#markup' => $string];
}
}
return $elements;
}
/**
* Generate the output appropriate for one field item.
*
* @param \Drupal\Core\Field\FieldItemInterface $item
* One field item.
*
* @return mixed
* The textual output generated.
*/
abstract protected function viewValue(FieldItemInterface $item);
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition->getTargetEntityTypeId() === 'file';
}
}
<?php
/**
* @file
* Contains \Drupal\file\Plugin\Field\FieldFormatter\DefaultFileFormatter.
*/
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Formatter for a text field on a file entity that links the field to the file.
*
* @FieldFormatter(
* id = "file_link",
* label = @Translation("File link"),
* field_types = {
* "string"
* }
* )
*/
class DefaultFileFormatter extends BaseFieldFileFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['link_to_file'] = TRUE;
return $settings;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
// We don't call the parent in order to bypass the link to file form.
return $form;
}
/**
* {@inheritdoc}
*/
protected function viewValue(FieldItemInterface $item) {
return $item->value;
}
}
<?php
/**
* @file
* Contains \Drupal\file\Plugin\Field\FieldFormatter\FileExtensionFormatter.
*/
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Formatter to render a filename as file extension.
*
* @FieldFormatter(
* id = "file_extension",
* label = @Translation("File extension"),
* field_types = {
* "string"
* }
* )
*/
class FileExtensionFormatter extends BaseFieldFileFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['extension_detect_tar'] = FALSE;
return $settings;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['extension_detect_tar'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Include tar in extension'),
'#description' => $this->t("If the part of the filename just before the extension is '.tar', include this in the extension output."),
'#default_value' => $this->getSetting('extension_detect_tar'),
);
return $form;
}
/**
* {@inheritdoc}
*/
protected function viewValue(FieldItemInterface $item) {
$filename = $item->value;
if (!$this->getSetting('extension_detect_tar')) {
return pathinfo($filename, PATHINFO_EXTENSION);
}
else {
$file_parts = explode('.', basename($filename));
if (count($file_parts) > 1) {
$extension = array_pop($file_parts);
$last_part_in_name = array_pop($file_parts);
if ($last_part_in_name === 'tar') {
$extension = 'tar.' . $extension;
}
return $extension;
}
}
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
// Just show this file extension formatter on the filename field.
return parent::isApplicable($field_definition) && $field_definition->getName() === 'filename';
}
}
<?php
/**
* @file
* Contains \Drupal\file\Plugin\Field\FieldFormatter\FileSize.
*/
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Formatter that shows the file size in a human readable way.
*
* @FieldFormatter(
* id = "file_size",
* label = @Translation("File size"),
* field_types = {
* "integer"
* }
* )
*/
class FileSize extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return parent::isApplicable($field_definition) && $field_definition->getName() === 'filesize';
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = ['#markup' => format_size($item->value)];
}
return $elements;
}
}
<?php
/**
* @file
* Contains \Drupal\file\Plugin\Field\FieldFormatter\FileUriFormatter.
*/
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Formatter to render the file URI to its download path.
*
* @FieldFormatter(
* id = "file_uri",
* label = @Translation("File URI"),
* field_types = {
* "uri"
* }
* )
*/
class FileUriFormatter extends BaseFieldFileFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['file_download_path'] = FALSE;
return $settings;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['file_download_path'] = [
'#title' => $this->t('Display the file download URI'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('file_download_path'),
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function viewValue(FieldItemInterface $item) {
$value = $item->value;
if ($this->getSetting('file_download_path')) {
$value = file_create_url($value);
}
return $value;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return parent::isApplicable($field_definition) && $field_definition->getName() === 'uri';
}