2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* Implement an image field , based on the file module ' s file field .
*/
use Drupal\Core\Render\Element ;
/**
* Prepares variables for image widget templates .
*
* Default template : image - widget . html . twig .
*
* @ param array $variables
* An associative array containing :
* - element : A render element representing the image field widget .
*/
function template_preprocess_image_widget ( & $variables ) {
$element = $variables [ 'element' ];
2017-04-13 15:53:35 +01:00
$variables [ 'attributes' ] = [ 'class' => [ 'image-widget' , 'js-form-managed-file' , 'form-managed-file' , 'clearfix' ]];
2015-08-17 17:00:26 -07:00
if ( ! empty ( $element [ 'fids' ][ '#value' ])) {
$file = reset ( $element [ '#files' ]);
$element [ 'file_' . $file -> id ()][ 'filename' ][ '#suffix' ] = ' <span class="file-size">(' . format_size ( $file -> getSize ()) . ')</span> ' ;
}
2017-04-13 15:53:35 +01:00
$variables [ 'data' ] = [];
2015-08-17 17:00:26 -07:00
foreach ( Element :: children ( $element ) as $child ) {
$variables [ 'data' ][ $child ] = $element [ $child ];
}
}
/**
* Prepares variables for image formatter templates .
*
* Default template : image - formatter . html . twig .
*
* @ param array $variables
* An associative array containing :
* - item : An ImageItem object .
* - item_attributes : An optional associative array of html attributes to be
* placed in the img tag .
* - image_style : An optional image style .
* - url : An optional \Drupal\Core\Url object .
*/
function template_preprocess_image_formatter ( & $variables ) {
if ( $variables [ 'image_style' ]) {
2017-04-13 15:53:35 +01:00
$variables [ 'image' ] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'image_style' ,
'#style_name' => $variables [ 'image_style' ],
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
}
else {
2017-04-13 15:53:35 +01:00
$variables [ 'image' ] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'image' ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
}
$variables [ 'image' ][ '#attributes' ] = $variables [ 'item_attributes' ];
$item = $variables [ 'item' ];
// Do not output an empty 'title' attribute.
2018-11-23 12:29:20 +00:00
if ( mb_strlen ( $item -> title ) != 0 ) {
2015-08-17 17:00:26 -07:00
$variables [ 'image' ][ '#title' ] = $item -> title ;
}
if (( $entity = $item -> entity ) && empty ( $item -> uri )) {
$variables [ 'image' ][ '#uri' ] = $entity -> getFileUri ();
}
else {
$variables [ 'image' ][ '#uri' ] = $item -> uri ;
}
2017-04-13 15:53:35 +01:00
foreach ([ 'width' , 'height' , 'alt' ] as $key ) {
2015-08-17 17:00:26 -07:00
$variables [ 'image' ][ " # $key " ] = $item -> $key ;
}
}