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: ...@@ -159,7 +159,6 @@ display:
html: false html: false
hide_empty: false hide_empty: false
empty_zero: false empty_zero: false
link_to_file: false
relationship: none relationship: none
group_type: group group_type: group
admin_label: '' admin_label: ''
...@@ -175,30 +174,45 @@ display: ...@@ -175,30 +174,45 @@ display:
element_default_classes: true element_default_classes: true
empty: '' empty: ''
hide_alter_empty: true hide_alter_empty: true
plugin_id: file plugin_id: field
entity_type: file entity_type: file
entity_field: fid entity_field: fid
filename: filename:
id: filename id: filename
table: file_managed table: file_managed
field: filename field: filename
relationship: none
group_type: group
admin_label: ''
label: Name
exclude: false
alter: alter:
alter_text: false alter_text: false
text: ''
make_link: false make_link: false
path: ''
absolute: false 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 word_boundary: false
ellipsis: false ellipsis: false
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false strip_tags: false
trim: false
preserve_tags: ''
html: false 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_type: ''
element_class: '' element_class: ''
element_label_type: '' element_label_type: ''
...@@ -208,8 +222,22 @@ display: ...@@ -208,8 +222,22 @@ display:
element_wrapper_class: '' element_wrapper_class: ''
element_default_classes: true element_default_classes: true
empty: '' empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true 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_type: file
entity_field: filename entity_field: filename
filemime: filemime:
...@@ -260,9 +288,8 @@ display: ...@@ -260,9 +288,8 @@ display:
hide_empty: false hide_empty: false
empty_zero: false empty_zero: false
hide_alter_empty: true hide_alter_empty: true
link_to_file: false type: file_filemime
filemime_image: false plugin_id: field
plugin_id: file_filemime
entity_type: file entity_type: file
entity_field: filemime entity_field: filemime
filesize: filesize:
...@@ -313,8 +340,8 @@ display: ...@@ -313,8 +340,8 @@ display:
hide_empty: false hide_empty: false
empty_zero: false empty_zero: false
hide_alter_empty: true hide_alter_empty: true
file_size_display: formatted type: file_size
plugin_id: file_size plugin_id: field
entity_type: file entity_type: file
entity_field: filesize entity_field: filesize
status: status:
...@@ -365,7 +392,12 @@ display: ...@@ -365,7 +392,12 @@ display:
hide_empty: false hide_empty: false
empty_zero: false empty_zero: false
hide_alter_empty: true 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_type: file
entity_field: status entity_field: status
created: created:
...@@ -679,6 +711,12 @@ display: ...@@ -679,6 +711,12 @@ display:
arguments: { } arguments: { }
group_by: true group_by: true
show_admin_links: true show_admin_links: true
display_extenders: { }
cache_metadata:
contexts:
- languages
- url
cacheable: false
page_1: page_1:
display_plugin: page display_plugin: page
id: page_1 id: page_1
...@@ -706,6 +744,12 @@ display: ...@@ -706,6 +744,12 @@ display:
group_type: group group_type: group
admin_label: 'File usage' admin_label: 'File usage'
required: false required: false
display_extenders: { }
cache_metadata:
contexts:
- languages
- url
cacheable: false
page_2: page_2:
display_plugin: page display_plugin: page
id: page_2 id: page_2
...@@ -1054,3 +1098,9 @@ display: ...@@ -1054,3 +1098,9 @@ display:
group_type: group group_type: group
admin_label: 'File usage' admin_label: 'File usage'
required: true required: true
display_extenders: { }
cache_metadata:
contexts:
- languages
- url
cacheable: false
...@@ -90,3 +90,34 @@ field.widget.settings.file_generic: ...@@ -90,3 +90,34 @@ field.widget.settings.file_generic:
progress_indicator: progress_indicator:
type: string type: string
label: 'Progress indicator' 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: ...@@ -29,10 +29,6 @@ views.field.file_filemime:
type: boolean type: boolean
label: 'Display an icon representing the file type, instead of the MIME text (such as "image/jpeg")' 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: views.field.file_uri:
type: views.field.file type: views.field.file
label: 'File URI' label: 'File URI'
......
...@@ -25,7 +25,6 @@ public function getViewsData() { ...@@ -25,7 +25,6 @@ public function getViewsData() {
$data['file_managed']['table']['base']['defaults']['field'] = 'filename'; $data['file_managed']['table']['base']['defaults']['field'] = 'filename';
$data['file_managed']['table']['wizard_id'] = 'file_managed'; $data['file_managed']['table']['wizard_id'] = 'file_managed';
$data['file_managed']['fid']['field']['id'] ='file';
$data['file_managed']['fid']['argument'] = array( $data['file_managed']['fid']['argument'] = array(
'id' => 'file_fid', 'id' => 'file_fid',
// The field to display in the summary. // The field to display in the summary.
...@@ -42,25 +41,30 @@ public function getViewsData() { ...@@ -42,25 +41,30 @@ public function getViewsData() {
'label' => t('File usage'), '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']['default_formatter'] = 'file_filemime';
$data['file_managed']['filemime']['field']['id'] = 'file_filemime';
$data['file_managed']['extension'] = array( $data['file_managed']['extension'] = array(
'title' => t('Extension'), 'title' => t('Extension'),
'help' => t('The extension of the file.'), 'help' => t('The extension of the file.'),
'real field' => 'filename', 'real field' => 'filename',
'field' => array( 'field' => array(
'id' => 'file_extension', 'entity_type' => 'file',
'field_name' => 'filename',
'default_formatter' => 'file_extension',
'id' => 'field',
'click sortable' => FALSE, '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']['status']['filter']['id'] = 'file_status';
$data['file_managed']['uid']['relationship']['title'] = t('User who uploaded'); $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)];