2009-10-12 05:22:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Implement an image field, based on the file module's file field.
|
|
|
|
*/
|
|
|
|
|
2012-12-17 21:54:13 +00:00
|
|
|
use Drupal\Component\Utility\NestedArray;
|
|
|
|
|
2009-10-12 05:22:57 +00:00
|
|
|
/**
|
2010-04-13 15:23:03 +00:00
|
|
|
* Returns HTML for an image field widget.
|
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* @ingroup themeable
|
2009-10-12 05:22:57 +00:00
|
|
|
*/
|
|
|
|
function theme_image_widget($variables) {
|
|
|
|
$element = $variables['element'];
|
|
|
|
$output = '';
|
|
|
|
$output .= '<div class="image-widget form-managed-file clearfix">';
|
|
|
|
|
|
|
|
if (isset($element['preview'])) {
|
|
|
|
$output .= '<div class="image-preview">';
|
|
|
|
$output .= drupal_render($element['preview']);
|
|
|
|
$output .= '</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= '<div class="image-widget-data">';
|
2013-04-20 03:34:14 +00:00
|
|
|
if (!empty($element['fids']['#value'])) {
|
|
|
|
$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-10-12 05:22:57 +00:00
|
|
|
}
|
|
|
|
$output .= drupal_render_children($element);
|
|
|
|
$output .= '</div>';
|
|
|
|
$output .= '</div>';
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-13 15:23:03 +00:00
|
|
|
* Returns HTML for an image field formatter.
|
|
|
|
*
|
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.
|
2011-11-25 03:09:40 +00:00
|
|
|
* - path: An optional array containing the link 'path' and link 'options'.
|
2010-04-13 15:23:03 +00:00
|
|
|
*
|
|
|
|
* @ingroup themeable
|
2009-10-12 05:22:57 +00:00
|
|
|
*/
|
2009-12-11 16:49:40 +00:00
|
|
|
function theme_image_formatter($variables) {
|
2014-01-24 12:44:59 +00:00
|
|
|
if ($variables['image_style']) {
|
|
|
|
$image = array(
|
|
|
|
'#theme' => 'image_style',
|
|
|
|
'#style_name' => $variables['image_style'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$image = array(
|
|
|
|
'#theme' => 'image',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$image['#attributes'] = $variables['item_attributes'];
|
|
|
|
|
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.
|
2014-01-24 12:44:59 +00:00
|
|
|
if (drupal_strlen($item->title) != 0) {
|
|
|
|
$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)) {
|
|
|
|
$image['#uri'] = $entity->getFileUri();
|
2013-07-20 01:09:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-01-24 12:44:59 +00:00
|
|
|
$image['#uri'] = $item->uri;
|
2013-07-20 01:09:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 12:44:59 +00:00
|
|
|
foreach (array('width', 'height', 'alt') as $key) {
|
|
|
|
$image["#$key"] = $item->$key;
|
2009-12-11 16:49:40 +00:00
|
|
|
}
|
|
|
|
|
2012-07-07 18:52:47 +00:00
|
|
|
// The link path and link options are both optional, but for the options to be
|
|
|
|
// processed, the link path must at least be an empty string.
|
2014-01-31 15:08:43 +00:00
|
|
|
// @todo Add support for route names.
|
2012-07-07 18:52:47 +00:00
|
|
|
if (isset($variables['path']['path'])) {
|
2010-02-11 15:52:13 +00:00
|
|
|
$path = $variables['path']['path'];
|
2012-07-07 18:52:47 +00:00
|
|
|
$options = isset($variables['path']['options']) ? $variables['path']['options'] : array();
|
2010-02-11 15:52:13 +00:00
|
|
|
// When displaying an image inside a link, the html option must be TRUE.
|
|
|
|
$options['html'] = TRUE;
|
2013-07-20 01:09:04 +00:00
|
|
|
$output = l($image, $path, $options);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$output = drupal_render($image);
|
2009-12-11 16:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
2010-01-30 07:59:26 +00:00
|
|
|
}
|