2015-08-18 00:00:26 +00:00
< ? php
/**
* @ file
* Field module functionality for the File module .
*/
use Drupal\Core\Field\FieldDefinitionInterface ;
2015-10-08 18:40:12 +00:00
use Drupal\Core\Field\FieldFilteredMarkup ;
2015-08-18 00:00:26 +00:00
use Drupal\Core\Render\Element ;
/**
* Prepares variables for multi file form widget templates .
*
* Default template : file - widget - multiple . html . twig .
*
* @ param array $variables
* An associative array containing :
* - element : A render element representing the widgets .
*/
function template_preprocess_file_widget_multiple ( & $variables ) {
$element = $variables [ 'element' ];
// Special ID and classes for draggable tables.
$weight_class = $element [ '#id' ] . '-weight' ;
$table_id = $element [ '#id' ] . '-table' ;
// Build up a table of applicable fields.
2017-04-13 14:53:35 +00:00
$headers = [];
2015-08-18 00:00:26 +00:00
$headers [] = t ( 'File information' );
if ( $element [ '#display_field' ]) {
2017-04-13 14:53:35 +00:00
$headers [] = [
2015-08-18 00:00:26 +00:00
'data' => t ( 'Display' ),
2017-04-13 14:53:35 +00:00
'class' => [ 'checkbox' ],
];
2015-08-18 00:00:26 +00:00
}
$headers [] = t ( 'Weight' );
$headers [] = t ( 'Operations' );
// Get our list of widgets in order (needed when the form comes back after
// preview or failed validation).
2017-04-13 14:53:35 +00:00
$widgets = [];
2015-08-18 00:00:26 +00:00
foreach ( Element :: children ( $element ) as $key ) {
$widgets [] = & $element [ $key ];
}
usort ( $widgets , '_field_multiple_value_form_sort_helper' );
2017-04-13 14:53:35 +00:00
$rows = [];
2015-08-18 00:00:26 +00:00
foreach ( $widgets as $key => & $widget ) {
// Save the uploading row for last.
if ( empty ( $widget [ '#files' ])) {
$widget [ '#title' ] = $element [ '#file_upload_title' ];
$widget [ '#description' ] = \Drupal :: service ( 'renderer' ) -> renderPlain ( $element [ '#file_upload_description' ]);
continue ;
}
// Delay rendering of the buttons, so that they can be rendered later in the
// "operations" column.
2017-04-13 14:53:35 +00:00
$operations_elements = [];
2015-08-18 00:00:26 +00:00
foreach ( Element :: children ( $widget ) as $sub_key ) {
if ( isset ( $widget [ $sub_key ][ '#type' ]) && $widget [ $sub_key ][ '#type' ] == 'submit' ) {
hide ( $widget [ $sub_key ]);
$operations_elements [] = & $widget [ $sub_key ];
}
}
// Delay rendering of the "Display" option and the weight selector, so that
// each can be rendered later in its own column.
if ( $element [ '#display_field' ]) {
hide ( $widget [ 'display' ]);
}
hide ( $widget [ '_weight' ]);
// Render everything else together in a column, without the normal wrappers.
2017-04-13 14:53:35 +00:00
$widget [ '#theme_wrappers' ] = [];
2015-08-18 00:00:26 +00:00
$information = drupal_render ( $widget );
$display = '' ;
if ( $element [ '#display_field' ]) {
unset ( $widget [ 'display' ][ '#title' ]);
2017-04-13 14:53:35 +00:00
$display = [
2015-08-18 00:00:26 +00:00
'data' => render ( $widget [ 'display' ]),
2017-04-13 14:53:35 +00:00
'class' => [ 'checkbox' ],
];
2015-08-18 00:00:26 +00:00
}
2017-04-13 14:53:35 +00:00
$widget [ '_weight' ][ '#attributes' ][ 'class' ] = [ $weight_class ];
2015-08-18 00:00:26 +00:00
$weight = render ( $widget [ '_weight' ]);
// Arrange the row with all of the rendered columns.
2017-04-13 14:53:35 +00:00
$row = [];
2015-08-18 00:00:26 +00:00
$row [] = $information ;
if ( $element [ '#display_field' ]) {
$row [] = $display ;
}
$row [] = $weight ;
2015-09-04 20:20:09 +00:00
// Show the buttons that had previously been marked as hidden in this
// preprocess function. We use show() to undo the earlier hide().
foreach ( Element :: children ( $operations_elements ) as $key ) {
show ( $operations_elements [ $key ]);
}
2017-04-13 14:53:35 +00:00
$row [] = [
2015-09-04 20:20:09 +00:00
'data' => $operations_elements ,
2017-04-13 14:53:35 +00:00
];
$rows [] = [
2015-08-18 00:00:26 +00:00
'data' => $row ,
2017-04-13 14:53:35 +00:00
'class' => isset ( $widget [ '#attributes' ][ 'class' ]) ? array_merge ( $widget [ '#attributes' ][ 'class' ], [ 'draggable' ]) : [ 'draggable' ],
];
2015-08-18 00:00:26 +00:00
}
2017-04-13 14:53:35 +00:00
$variables [ 'table' ] = [
2015-08-18 00:00:26 +00:00
'#type' => 'table' ,
'#header' => $headers ,
'#rows' => $rows ,
2017-04-13 14:53:35 +00:00
'#attributes' => [
2015-08-18 00:00:26 +00:00
'id' => $table_id ,
2017-04-13 14:53:35 +00:00
],
'#tabledrag' => [
[
2015-08-18 00:00:26 +00:00
'action' => 'order' ,
'relationship' => 'sibling' ,
'group' => $weight_class ,
2017-04-13 14:53:35 +00:00
],
],
2015-09-04 20:20:09 +00:00
'#access' => ! empty ( $rows ),
2017-04-13 14:53:35 +00:00
];
2015-08-18 00:00:26 +00:00
$variables [ 'element' ] = $element ;
}
/**
* Prepares variables for file upload help text templates .
*
* Default template : file - upload - help . html . twig .
*
* @ param array $variables
* An associative array containing :
* - description : The normal description for this field , specified by the
* user .
* - upload_validators : An array of upload validators as used in
* $element [ '#upload_validators' ] .
*/
function template_preprocess_file_upload_help ( & $variables ) {
$description = $variables [ 'description' ];
$upload_validators = $variables [ 'upload_validators' ];
$cardinality = $variables [ 'cardinality' ];
2017-04-13 14:53:35 +00:00
$descriptions = [];
2015-08-18 00:00:26 +00:00
if ( ! empty ( $description )) {
2015-10-08 18:40:12 +00:00
$descriptions [] = FieldFilteredMarkup :: create ( $description );
2015-08-18 00:00:26 +00:00
}
if ( isset ( $cardinality )) {
if ( $cardinality == - 1 ) {
$descriptions [] = t ( 'Unlimited number of files can be uploaded to this field.' );
}
else {
$descriptions [] = \Drupal :: translation () -> formatPlural ( $cardinality , 'One file only.' , 'Maximum @count files.' );
}
}
if ( isset ( $upload_validators [ 'file_validate_size' ])) {
2017-04-13 14:53:35 +00:00
$descriptions [] = t ( '@size limit.' , [ '@size' => format_size ( $upload_validators [ 'file_validate_size' ][ 0 ])]);
2015-08-18 00:00:26 +00:00
}
if ( isset ( $upload_validators [ 'file_validate_extensions' ])) {
2017-04-13 14:53:35 +00:00
$descriptions [] = t ( 'Allowed types: @extensions.' , [ '@extensions' => $upload_validators [ 'file_validate_extensions' ][ 0 ]]);
2015-08-18 00:00:26 +00:00
}
if ( isset ( $upload_validators [ 'file_validate_image_resolution' ])) {
$max = $upload_validators [ 'file_validate_image_resolution' ][ 0 ];
$min = $upload_validators [ 'file_validate_image_resolution' ][ 1 ];
if ( $min && $max && $min == $max ) {
2017-04-13 14:53:35 +00:00
$descriptions [] = t ( 'Images must be exactly <strong>@size</strong> pixels.' , [ '@size' => $max ]);
2015-08-18 00:00:26 +00:00
}
elseif ( $min && $max ) {
2017-04-13 14:53:35 +00:00
$descriptions [] = t ( 'Images must be larger than <strong>@min</strong> pixels. Images larger than <strong>@max</strong> pixels will be resized.' , [ '@min' => $min , '@max' => $max ]);
2015-08-18 00:00:26 +00:00
}
elseif ( $min ) {
2017-04-13 14:53:35 +00:00
$descriptions [] = t ( 'Images must be larger than <strong>@min</strong> pixels.' , [ '@min' => $min ]);
2015-08-18 00:00:26 +00:00
}
elseif ( $max ) {
2017-04-13 14:53:35 +00:00
$descriptions [] = t ( 'Images larger than <strong>@max</strong> pixels will be resized.' , [ '@max' => $max ]);
2015-08-18 00:00:26 +00:00
}
}
$variables [ 'descriptions' ] = $descriptions ;
}
/**
* Determine whether a field references files stored in { file_managed } .
*
* @ param \Drupal\Core\Field\FieldDefinitionInterface $field
* A field definition .
*
* @ return bool
* The field column if the field references { file_managed } . fid , typically
* fid , FALSE if it does not .
*/
function file_field_find_file_reference_column ( FieldDefinitionInterface $field ) {
$schema = $field -> getFieldStorageDefinition () -> getSchema ();
foreach ( $schema [ 'foreign keys' ] as $data ) {
if ( $data [ 'table' ] == 'file_managed' ) {
foreach ( $data [ 'columns' ] as $field_column => $column ) {
if ( $column == 'fid' ) {
return $field_column ;
}
}
}
}
return FALSE ;
}