2009-08-29 12:52:32 +00:00
< ? php
/**
* @ file
* Field module functionality for the File module .
*/
2014-02-16 19:15:45 +00:00
use Drupal\Component\Utility\Html ;
Issue #1825952 by Fabianx, joelpittet, bdragon, heddn, chx, xjm, pwolanin, mikey_p, ti2m, bfr, dags, cilefen, scor, mgifford: Turn on twig autoescape by default
2014-07-18 09:05:22 +00:00
use Drupal\Component\Utility\SafeMarkup ;
2014-06-13 02:33:48 +00:00
use Drupal\Component\Utility\String ;
2014-04-11 16:01:24 +00:00
use Drupal\Core\Field\FieldDefinitionInterface ;
2014-03-31 17:37:55 +00:00
use Drupal\Core\Render\Element ;
2012-12-17 21:54:13 +00:00
2009-08-29 12:52:32 +00:00
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for an individual file upload widget .
*
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
* Default template : file - widget . html . twig .
2010-04-13 15:23:03 +00:00
*
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
* @ param array $variables
* An associative array containing :
* - element : A render element representing the file .
2009-08-29 12:52:32 +00:00
*/
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
function template_preprocess_file_widget ( & $variables ) {
2009-10-09 01:00:08 +00:00
$element = $variables [ 'element' ];
2013-04-20 03:34:14 +00:00
if ( ! empty ( $element [ 'fids' ][ '#value' ])) {
2009-08-29 12:52:32 +00:00
// Add the file size after the file name.
2013-04-20 03:34:14 +00:00
$file = reset ( $element [ '#files' ]);
2013-07-26 05:44:30 +00:00
$element [ 'file_' . $file -> id ()][ 'filename' ][ '#suffix' ] = ' <span class="file-size">(' . format_size ( $file -> getSize ()) . ')</span> ' ;
2009-08-29 12:52:32 +00:00
}
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
$variables [ 'element' ] = $element ;
// The "form-managed-file" class is required for proper Ajax functionality.
$variables [ 'attributes' ] = array ( 'class' => array ( 'file-widget' , 'form-managed-file' , 'clearfix' ));
2009-08-29 12:52:32 +00:00
}
/**
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
* Prepares variables for multi file form widget templates .
*
* Default template : file - widget - multiple . html . twig .
2010-04-13 15:23:03 +00:00
*
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
* @ param array $variables
2010-04-13 15:23:03 +00:00
* An associative array containing :
* - element : A render element representing the widgets .
2009-08-29 12:52:32 +00:00
*/
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
function template_preprocess_file_widget_multiple ( & $variables ) {
2009-10-09 01:00:08 +00:00
$element = $variables [ 'element' ];
2009-08-29 12:52:32 +00:00
// Special ID and classes for draggable tables.
$weight_class = $element [ '#id' ] . '-weight' ;
$table_id = $element [ '#id' ] . '-table' ;
// Build up a table of applicable fields.
$headers = array ();
$headers [] = t ( 'File information' );
2010-02-11 15:42:14 +00:00
if ( $element [ '#display_field' ]) {
2009-08-29 12:52:32 +00:00
$headers [] = array (
'data' => t ( 'Display' ),
'class' => array ( 'checkbox' ),
);
}
$headers [] = t ( 'Weight' );
$headers [] = t ( 'Operations' );
2010-11-22 04:42:09 +00:00
// Get our list of widgets in order (needed when the form comes back after
// preview or failed validation).
$widgets = array ();
2014-03-31 17:37:55 +00:00
foreach ( Element :: children ( $element ) as $key ) {
2010-11-22 04:42:09 +00:00
$widgets [] = & $element [ $key ];
}
2014-07-07 12:01:38 +00:00
usort ( $widgets , '_field_multiple_value_form_sort_helper' );
2010-11-22 04:42:09 +00:00
2009-08-29 12:52:32 +00:00
$rows = array ();
2010-11-22 04:42:09 +00:00
foreach ( $widgets as $key => & $widget ) {
2009-08-29 12:52:32 +00:00
// Save the uploading row for last.
2013-04-20 03:34:14 +00:00
if ( empty ( $widget [ '#files' ])) {
2010-11-22 04:42:09 +00:00
$widget [ '#title' ] = $element [ '#file_upload_title' ];
2013-07-26 05:44:30 +00:00
$widget [ '#description' ] = drupal_render ( $element [ '#file_upload_description' ]);
2009-08-29 12:52:32 +00:00
continue ;
}
2010-07-02 12:37:57 +00:00
// Delay rendering of the buttons, so that they can be rendered later in the
// "operations" column.
$operations_elements = array ();
2014-03-31 17:37:55 +00:00
foreach ( Element :: children ( $widget ) as $sub_key ) {
2010-11-22 04:42:09 +00:00
if ( isset ( $widget [ $sub_key ][ '#type' ]) && $widget [ $sub_key ][ '#type' ] == 'submit' ) {
hide ( $widget [ $sub_key ]);
$operations_elements [] = & $widget [ $sub_key ];
2009-08-29 12:52:32 +00:00
}
}
2010-07-02 12:37:57 +00:00
// 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' ]) {
2010-11-22 04:42:09 +00:00
hide ( $widget [ 'display' ]);
2010-07-02 12:37:57 +00:00
}
2010-11-22 04:42:09 +00:00
hide ( $widget [ '_weight' ]);
2010-07-02 12:37:57 +00:00
// Render everything else together in a column, without the normal wrappers.
2010-11-22 04:42:09 +00:00
$widget [ '#theme_wrappers' ] = array ();
$information = drupal_render ( $widget );
2010-07-02 12:37:57 +00:00
// Render the previously hidden elements, using render() instead of
// drupal_render(), to undo the earlier hide().
$operations = '' ;
foreach ( $operations_elements as $operation_element ) {
$operations .= render ( $operation_element );
}
2009-08-29 12:52:32 +00:00
$display = '' ;
2010-02-11 15:42:14 +00:00
if ( $element [ '#display_field' ]) {
2010-11-22 04:42:09 +00:00
unset ( $widget [ 'display' ][ '#title' ]);
2009-08-29 12:52:32 +00:00
$display = array (
2010-11-22 04:42:09 +00:00
'data' => render ( $widget [ 'display' ]),
2009-08-29 12:52:32 +00:00
'class' => array ( 'checkbox' ),
);
}
2010-11-22 04:42:09 +00:00
$widget [ '_weight' ][ '#attributes' ][ 'class' ] = array ( $weight_class );
$weight = render ( $widget [ '_weight' ]);
2009-08-29 12:52:32 +00:00
2010-07-02 12:37:57 +00:00
// Arrange the row with all of the rendered columns.
2009-08-29 12:52:32 +00:00
$row = array ();
$row [] = $information ;
2010-02-11 15:42:14 +00:00
if ( $element [ '#display_field' ]) {
2009-08-29 12:52:32 +00:00
$row [] = $display ;
}
$row [] = $weight ;
Issue #1825952 by Fabianx, joelpittet, bdragon, heddn, chx, xjm, pwolanin, mikey_p, ti2m, bfr, dags, cilefen, scor, mgifford: Turn on twig autoescape by default
2014-07-18 09:05:22 +00:00
$row [] = SafeMarkup :: set ( $operations );
2009-08-29 12:52:32 +00:00
$rows [] = array (
'data' => $row ,
2010-11-22 04:42:09 +00:00
'class' => isset ( $widget [ '#attributes' ][ 'class' ]) ? array_merge ( $widget [ '#attributes' ][ 'class' ], array ( 'draggable' )) : array ( 'draggable' ),
2009-08-29 12:52:32 +00:00
);
}
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
$variables [ 'table' ] = array (
2013-12-20 12:05:47 +00:00
'#type' => 'table' ,
2013-07-26 05:44:30 +00:00
'#header' => $headers ,
'#rows' => $rows ,
'#attributes' => array (
'id' => $table_id ,
),
2013-12-20 12:05:47 +00:00
'#tabledrag' => array (
array (
'action' => 'order' ,
'relationship' => 'sibling' ,
'group' => $weight_class ,
),
),
2013-07-26 05:44:30 +00:00
);
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
$variables [ 'element' ] = $element ;
2009-08-29 12:52:32 +00:00
}
/**
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
* Prepares variables for file upload help text templates .
2009-08-29 12:52:32 +00:00
*
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
* Default template : file - upload - help . html . twig .
*
* @ param array $variables
2009-10-09 01:00:08 +00:00
* 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' ] .
2009-08-29 12:52:32 +00:00
*/
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
function template_preprocess_file_upload_help ( & $variables ) {
2009-10-09 01:00:08 +00:00
$description = $variables [ 'description' ];
$upload_validators = $variables [ 'upload_validators' ];
2013-04-20 03:34:14 +00:00
$cardinality = $variables [ 'cardinality' ];
2009-10-09 01:00:08 +00:00
2009-08-29 12:52:32 +00:00
$descriptions = array ();
2014-02-16 19:15:45 +00:00
if ( ! empty ( $description )) {
$descriptions [] = Html :: normalize ( $description );
2009-08-29 12:52:32 +00:00
}
2013-04-20 03:34:14 +00:00
if ( isset ( $cardinality )) {
if ( $cardinality == - 1 ) {
$descriptions [] = t ( 'Unlimited number of files can be uploaded to this field.' );
}
else {
2013-10-18 06:08:24 +00:00
$descriptions [] = format_plural ( $cardinality , 'One file only.' , 'Maximum @count files.' );
2013-04-20 03:34:14 +00:00
}
}
2013-10-18 06:08:24 +00:00
if ( isset ( $upload_validators [ 'file_validate_size' ])) {
$descriptions [] = t ( '!size limit.' , array ( '!size' => format_size ( $upload_validators [ 'file_validate_size' ][ 0 ])));
}
if ( isset ( $upload_validators [ 'file_validate_extensions' ])) {
2014-06-13 02:33:48 +00:00
$descriptions [] = t ( 'Allowed types: !extensions.' , array ( '!extensions' => String :: checkPlain ( $upload_validators [ 'file_validate_extensions' ][ 0 ])));
2013-10-18 06:08:24 +00:00
}
2009-08-29 12:52:32 +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 ) {
$descriptions [] = t ( 'Images must be exactly !size pixels.' , array ( '!size' => '<strong>' . $max . '</strong>' ));
}
elseif ( $min && $max ) {
2014-06-06 19:48:46 +00:00
$descriptions [] = t ( 'Images must be larger than !min pixels. Images larger than !max pixels will be resized.' , array ( '!min' => '<strong>' . $min . '</strong>' , '!max' => '<strong>' . $max . '</strong>' ));
2009-08-29 12:52:32 +00:00
}
elseif ( $min ) {
$descriptions [] = t ( 'Images must be larger than !min pixels.' , array ( '!min' => '<strong>' . $min . '</strong>' ));
}
elseif ( $max ) {
2014-06-06 19:48:46 +00:00
$descriptions [] = t ( 'Images larger than !max pixels will be resized.' , array ( '!max' => '<strong>' . $max . '</strong>' ));
2009-08-29 12:52:32 +00:00
}
}
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
2014-06-13 03:13:30 +00:00
$variables [ 'descriptions' ] = $descriptions ;
2009-08-29 12:52:32 +00:00
}
2012-10-30 10:41:42 +00:00
/**
* Determine whether a field references files stored in { file_managed } .
*
2014-04-11 16:01:24 +00:00
* @ param \Drupal\Core\Field\FieldDefinitionInterface $field
2013-10-04 07:55:32 +00:00
* A field definition .
2012-10-30 10:41:42 +00:00
*
2014-04-11 16:01:24 +00:00
* @ return bool
2012-10-30 10:41:42 +00:00
* The field column if the field references { file_managed } . fid , typically
2014-04-11 16:01:24 +00:00
* fid , FALSE if it does not .
2012-10-30 10:41:42 +00:00
*/
2014-04-11 16:01:24 +00:00
function file_field_find_file_reference_column ( FieldDefinitionInterface $field ) {
2014-06-20 17:40:27 +00:00
$schema = $field -> getFieldStorageDefinition () -> getSchema ();
2013-10-04 07:55:32 +00:00
foreach ( $schema [ 'foreign keys' ] as $data ) {
2012-10-30 10:41:42 +00:00
if ( $data [ 'table' ] == 'file_managed' ) {
foreach ( $data [ 'columns' ] as $field_column => $column ) {
if ( $column == 'fid' ) {
return $field_column ;
}
}
}
}
return FALSE ;
}