2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* Administration pages for image settings .
*/
use Drupal\Core\Render\Element ;
/**
* Prepares variables for image style preview templates .
*
* Default template : image - style - preview . html . twig .
*
* @ param array $variables
* An associative array containing :
* - style : \Drupal\image\ImageStyleInterface image style being previewed .
*/
function template_preprocess_image_style_preview ( & $variables ) {
// Style information.
$style = $variables [ 'style' ];
2015-09-04 13:20:09 -07:00
$variables [ 'style_id' ] = $style -> id ();
$variables [ 'style_name' ] = $style -> label ();
2015-08-17 17:00:26 -07:00
// Cache bypass token.
$variables [ 'cache_bypass' ] = REQUEST_TIME ;
// Sample image info.
$sample_width = 160 ;
$sample_height = 160 ;
$image_factory = \Drupal :: service ( 'image.factory' );
// Set up original file information.
$original_path = \Drupal :: config ( 'image.settings' ) -> get ( 'preview_image' );
$original_image = $image_factory -> get ( $original_path );
2017-04-13 15:53:35 +01:00
$variables [ 'original' ] = [
2016-02-03 14:56:31 -08:00
'url' => file_url_transform_relative ( file_create_url ( $original_path )),
2015-08-17 17:00:26 -07:00
'width' => $original_image -> getWidth (),
'height' => $original_image -> getHeight (),
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
if ( $variables [ 'original' ][ 'width' ] > $variables [ 'original' ][ 'height' ]) {
$variables [ 'preview' ][ 'original' ][ 'width' ] = min ( $variables [ 'original' ][ 'width' ], $sample_width );
$variables [ 'preview' ][ 'original' ][ 'height' ] = round ( $variables [ 'preview' ][ 'original' ][ 'width' ] / $variables [ 'original' ][ 'width' ] * $variables [ 'original' ][ 'height' ]);
}
else {
$variables [ 'preview' ][ 'original' ][ 'height' ] = min ( $variables [ 'original' ][ 'height' ], $sample_height );
$variables [ 'preview' ][ 'original' ][ 'width' ] = round ( $variables [ 'preview' ][ 'original' ][ 'height' ] / $variables [ 'original' ][ 'height' ] * $variables [ 'original' ][ 'width' ]);
}
// Set up derivative file information.
$preview_file = $style -> buildUri ( $original_path );
// Create derivative if necessary.
if ( ! file_exists ( $preview_file )) {
$style -> createDerivative ( $original_path , $preview_file );
}
$preview_image = $image_factory -> get ( $preview_file );
2017-04-13 15:53:35 +01:00
$variables [ 'derivative' ] = [
2016-02-03 14:56:31 -08:00
'url' => file_url_transform_relative ( file_create_url ( $preview_file )),
2015-08-17 17:00:26 -07:00
'width' => $preview_image -> getWidth (),
'height' => $preview_image -> getHeight (),
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
if ( $variables [ 'derivative' ][ 'width' ] > $variables [ 'derivative' ][ 'height' ]) {
$variables [ 'preview' ][ 'derivative' ][ 'width' ] = min ( $variables [ 'derivative' ][ 'width' ], $sample_width );
$variables [ 'preview' ][ 'derivative' ][ 'height' ] = round ( $variables [ 'preview' ][ 'derivative' ][ 'width' ] / $variables [ 'derivative' ][ 'width' ] * $variables [ 'derivative' ][ 'height' ]);
}
else {
$variables [ 'preview' ][ 'derivative' ][ 'height' ] = min ( $variables [ 'derivative' ][ 'height' ], $sample_height );
$variables [ 'preview' ][ 'derivative' ][ 'width' ] = round ( $variables [ 'preview' ][ 'derivative' ][ 'height' ] / $variables [ 'derivative' ][ 'height' ] * $variables [ 'derivative' ][ 'width' ]);
}
// Build the preview of the original image.
2017-04-13 15:53:35 +01:00
$variables [ 'original' ][ 'rendered' ] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'image' ,
'#uri' => $original_path ,
'#alt' => t ( 'Sample original image' ),
'#title' => '' ,
2017-04-13 15:53:35 +01:00
'#attributes' => [
2015-08-17 17:00:26 -07:00
'width' => $variables [ 'original' ][ 'width' ],
'height' => $variables [ 'original' ][ 'height' ],
'style' => 'width: ' . $variables [ 'preview' ][ 'original' ][ 'width' ] . 'px; height: ' . $variables [ 'preview' ][ 'original' ][ 'height' ] . 'px;' ,
2017-04-13 15:53:35 +01:00
],
];
2015-08-17 17:00:26 -07:00
// Build the preview of the image style derivative. Timestamps are added
// to prevent caching of images on the client side.
2017-04-13 15:53:35 +01:00
$variables [ 'derivative' ][ 'rendered' ] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'image' ,
'#uri' => $variables [ 'derivative' ][ 'url' ] . '?cache_bypass=' . $variables [ 'cache_bypass' ],
'#alt' => t ( 'Sample modified image' ),
'#title' => '' ,
2017-04-13 15:53:35 +01:00
'#attributes' => [
2015-08-17 17:00:26 -07:00
'width' => $variables [ 'derivative' ][ 'width' ],
'height' => $variables [ 'derivative' ][ 'height' ],
'style' => 'width: ' . $variables [ 'preview' ][ 'derivative' ][ 'width' ] . 'px; height: ' . $variables [ 'preview' ][ 'derivative' ][ 'height' ] . 'px;' ,
2017-04-13 15:53:35 +01:00
],
];
2015-08-17 17:00:26 -07:00
}
/**
* Prepares variables for image anchor templates .
*
* Default template : image - anchor . html . twig .
*
* @ param array $variables
* An associative array containing :
* - element : An associative array containing the image .
*/
function template_preprocess_image_anchor ( & $variables ) {
$element = $variables [ 'element' ];
2017-04-13 15:53:35 +01:00
$rows = [];
$row = [];
2015-08-17 17:00:26 -07:00
foreach ( Element :: children ( $element ) as $n => $key ) {
$element [ $key ][ '#attributes' ][ 'title' ] = $element [ $key ][ '#title' ];
unset ( $element [ $key ][ '#title' ]);
2017-04-13 15:53:35 +01:00
$row [] = [
2015-08-17 17:00:26 -07:00
'data' => $element [ $key ],
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
if ( $n % 3 == 3 - 1 ) {
$rows [] = $row ;
2017-04-13 15:53:35 +01:00
$row = [];
2015-08-17 17:00:26 -07:00
}
}
2017-04-13 15:53:35 +01:00
$variables [ 'table' ] = [
2015-08-17 17:00:26 -07:00
'#type' => 'table' ,
2017-04-13 15:53:35 +01:00
'#header' => [],
2015-08-17 17:00:26 -07:00
'#rows' => $rows ,
2017-04-13 15:53:35 +01:00
'#attributes' => [
'class' => [ 'image-anchor' ],
],
];
2015-08-17 17:00:26 -07:00
}