2009-10-12 05:22:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Implement an image field, based on the file module's file field.
|
|
|
|
*/
|
|
|
|
|
2014-06-13 02:52:19 +00:00
|
|
|
use Drupal\Core\Render\Element;
|
2012-12-17 21:54:13 +00:00
|
|
|
|
2009-10-12 05:22:57 +00:00
|
|
|
/**
|
2014-06-13 02:52:19 +00:00
|
|
|
* Prepares variables for image widget templates.
|
|
|
|
*
|
|
|
|
* Default template: image-widget.html.twig.
|
2010-04-13 15:23:03 +00:00
|
|
|
*
|
2012-07-07 20:09:35 +00:00
|
|
|
* @param array $variables
|
2010-04-13 15:23:03 +00:00
|
|
|
* An associative array containing:
|
|
|
|
* - element: A render element representing the image field widget.
|
2009-10-12 05:22:57 +00:00
|
|
|
*/
|
2014-06-13 02:52:19 +00:00
|
|
|
function template_preprocess_image_widget(&$variables) {
|
2009-10-12 05:22:57 +00:00
|
|
|
$element = $variables['element'];
|
|
|
|
|
2017-03-04 01:20:24 +00:00
|
|
|
$variables['attributes'] = ['class' => ['image-widget', 'js-form-managed-file', 'form-managed-file', 'clearfix']];
|
2009-10-12 05:22:57 +00:00
|
|
|
|
2017-03-04 01:20:24 +00:00
|
|
|
$variables['data'] = [];
|
2014-06-13 02:52:19 +00:00
|
|
|
foreach (Element::children($element) as $child) {
|
|
|
|
$variables['data'][$child] = $element[$child];
|
|
|
|
}
|
|
|
|
|
2009-10-12 05:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-13 02:52:19 +00:00
|
|
|
* Prepares variables for image formatter templates.
|
|
|
|
*
|
|
|
|
* Default template: image-formatter.html.twig.
|
2010-04-13 15:23:03 +00:00
|
|
|
*
|
2012-07-07 20:09:35 +00:00
|
|
|
* @param array $variables
|
2010-04-13 15:23:03 +00:00
|
|
|
* An associative array containing:
|
2014-01-24 12:44:59 +00:00
|
|
|
* - item: An ImageItem object.
|
|
|
|
* - item_attributes: An optional associative array of html attributes to be
|
|
|
|
* placed in the img tag.
|
2010-04-13 15:23:03 +00:00
|
|
|
* - image_style: An optional image style.
|
2015-01-17 09:13:34 +00:00
|
|
|
* - url: An optional \Drupal\Core\Url object.
|
2009-10-12 05:22:57 +00:00
|
|
|
*/
|
2014-06-13 02:52:19 +00:00
|
|
|
function template_preprocess_image_formatter(&$variables) {
|
2014-01-24 12:44:59 +00:00
|
|
|
if ($variables['image_style']) {
|
2017-03-04 01:20:24 +00:00
|
|
|
$variables['image'] = [
|
2014-01-24 12:44:59 +00:00
|
|
|
'#theme' => 'image_style',
|
|
|
|
'#style_name' => $variables['image_style'],
|
2017-03-04 01:20:24 +00:00
|
|
|
];
|
2014-01-24 12:44:59 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-03-04 01:20:24 +00:00
|
|
|
$variables['image'] = [
|
2014-01-24 12:44:59 +00:00
|
|
|
'#theme' => 'image',
|
2017-03-04 01:20:24 +00:00
|
|
|
];
|
2014-01-24 12:44:59 +00:00
|
|
|
}
|
2014-06-13 02:52:19 +00:00
|
|
|
$variables['image']['#attributes'] = $variables['item_attributes'];
|
2014-01-24 12:44:59 +00:00
|
|
|
|
2009-12-11 16:49:40 +00:00
|
|
|
$item = $variables['item'];
|
2011-10-17 16:34:18 +00:00
|
|
|
|
2010-04-30 12:53:47 +00:00
|
|
|
// Do not output an empty 'title' attribute.
|
2021-10-06 23:14:55 +00:00
|
|
|
if (!is_null($item->title) && mb_strlen($item->title) != 0) {
|
2014-06-13 02:52:19 +00:00
|
|
|
$variables['image']['#title'] = $item->title;
|
2010-04-30 12:53:47 +00:00
|
|
|
}
|
2009-12-11 16:49:40 +00:00
|
|
|
|
2014-01-24 12:44:59 +00:00
|
|
|
if (($entity = $item->entity) && empty($item->uri)) {
|
2014-06-13 02:52:19 +00:00
|
|
|
$variables['image']['#uri'] = $entity->getFileUri();
|
2013-07-20 01:09:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-06-13 02:52:19 +00:00
|
|
|
$variables['image']['#uri'] = $item->uri;
|
2013-07-20 01:09:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 01:20:24 +00:00
|
|
|
foreach (['width', 'height', 'alt'] as $key) {
|
2014-06-13 02:52:19 +00:00
|
|
|
$variables['image']["#$key"] = $item->$key;
|
2009-12-11 16:49:40 +00:00
|
|
|
}
|
2010-01-30 07:59:26 +00:00
|
|
|
}
|