Issue #2042773 by yched, Berdir, pcambra: Change #items within a theme_field() render array from an *array* to the same $items *object* used throughout the rest of the formatter pipeline.

8.0.x
Nathaniel Catchpole 2014-01-24 12:44:59 +00:00
parent 6a9e3fb02d
commit 098289167d
11 changed files with 107 additions and 65 deletions

View File

@ -89,7 +89,7 @@ abstract class FormatterBase extends PluginSettingsBase implements FormatterInte
'#entity_type' => $entity_type,
'#bundle' => $entity->bundle(),
'#object' => $entity,
'#items' => $items->getValue(TRUE),
'#items' => $items,
'#formatter' => $this->getPluginId(),
'#cache' => array('tags' => array())
);

View File

@ -385,7 +385,7 @@ function hook_field_attach_view_alter(&$output, $context) {
$element = &$output[$field_name];
if ($element['#field_type'] == 'entity_reference' && $element['#formatter'] == 'entity_reference_label') {
foreach ($element['#items'] as $delta => $item) {
$term = $item['entity'];
$term = $item->entity;
if (!empty($term->rdf_mapping['rdftype'])) {
$element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
}

View File

@ -575,7 +575,7 @@ function template_preprocess_field(&$variables, $hook) {
// formatters leave them within $element['#items'][$delta]['_attributes'] to
// be rendered on the item wrappers provided by theme_field().
foreach ($variables['items'] as $delta => $item) {
$variables['item_attributes'][$delta] = !empty($element['#items'][$delta]['_attributes']) ? new Attribute($element['#items'][$delta]['_attributes']) : clone($default_attributes);
$variables['item_attributes'][$delta] = !empty($element['#items'][$delta]->_attributes) ? new Attribute($element['#items'][$delta]->_attributes) : clone($default_attributes);
}
}

View File

@ -248,7 +248,7 @@ function file_field_find_file_reference_column(FieldInterface $field) {
*
* @param $variables
* An associative array containing:
* - items: An array of file attachments.
* - items: field values, as a FileFieldItemList object.
*
* @ingroup themeable
*/

View File

@ -51,40 +51,44 @@ function theme_image_widget($variables) {
*
* @param array $variables
* An associative array containing:
* - item: An array of image data.
* - 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.
* - path: An optional array containing the link 'path' and link 'options'.
*
* @ingroup themeable
*/
function theme_image_formatter($variables) {
if ($variables['image_style']) {
$image = array(
'#theme' => 'image_style',
'#style_name' => $variables['image_style'],
);
}
else {
$image = array(
'#theme' => 'image',
);
}
$image['#attributes'] = $variables['item_attributes'];
$item = $variables['item'];
$image = array();
// Do not output an empty 'title' attribute.
if (isset($item['title']) && drupal_strlen($item['title']) != 0) {
$image['#title'] = $item['title'];
if (drupal_strlen($item->title) != 0) {
$image['#title'] = $item->title;
}
if (isset($item['entity']) && empty($item['uri'])) {
$image['#uri'] = $item['entity']->getFileUri();
if (($entity = $item->entity) && empty($item->uri)) {
$image['#uri'] = $entity->getFileUri();
}
else {
$image['#uri'] = $item['uri'];
$image['#uri'] = $item->uri;
}
foreach (array('width', 'height', 'alt', 'attributes') as $key) {
if (isset($item[$key]) || array_key_exists($key, $item)) {
$image["#$key"] = $item[$key];
}
}
if ($variables['image_style']) {
$image['#theme'] = 'image_style';
$image['#style_name'] = $variables['image_style'];
}
else {
$image['#theme'] = 'image';
foreach (array('width', 'height', 'alt') as $key) {
$image["#$key"] = $item->$key;
}
// The link path and link options are both optional, but for the options to be

View File

@ -199,7 +199,7 @@ function image_theme() {
'file' => 'image.field.inc',
),
'image_formatter' => array(
'variables' => array('item' => NULL, 'path' => NULL, 'image_style' => NULL),
'variables' => array('item' => NULL, 'item_attributes' => NULL, 'path' => NULL, 'image_style' => NULL),
'file' => 'image.field.inc',
),
);

View File

@ -111,20 +111,19 @@ class ImageFormatter extends ImageFormatterBase {
'options' => array(),
);
}
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item_attributes = $item->_attributes;
unset($item->_attributes);
$elements[$delta] = array(
'#theme' => 'image_formatter',
'#item' => $item->getValue(TRUE),
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $image_style_setting,
'#path' => isset($uri) ? $uri : '',
);
// Pass field item attributes to the theme function.
if (isset($item->_attributes)) {
$elements[$delta]['#item'] += array('attributes' => array());
$elements[$delta]['#item']['attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
}
}

View File

@ -160,7 +160,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$article = $this->drupalCreateNode(array('type' => 'article'));
$article_built = node_view($article);
$this->assertEqual(
$article_built[$field_name]['#items'][0]['target_id'],
$article_built[$field_name]['#items'][0]->target_id,
$default_images['instance']->id(),
format_string(
'A new article node without an image has the expected default image file ID of @fid.',
@ -172,7 +172,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$page = $this->drupalCreateNode(array('type' => 'page'));
$page_built = node_view($page);
$this->assertEqual(
$page_built[$field_name]['#items'][0]['target_id'],
$page_built[$field_name]['#items'][0]->target_id,
$default_images['instance2']->id(),
format_string(
'A new page node without an image has the expected default image file ID of @fid.',
@ -199,7 +199,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$article_built = node_view($article = node_load($article->id(), TRUE));
$page_built = node_view($page = node_load($page->id(), TRUE));
$this->assertEqual(
$article_built[$field_name]['#items'][0]['target_id'],
$article_built[$field_name]['#items'][0]->target_id,
$default_images['instance']->id(),
format_string(
'An existing article node without an image has the expected default image file ID of @fid.',
@ -207,7 +207,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
)
);
$this->assertEqual(
$page_built[$field_name]['#items'][0]['target_id'],
$page_built[$field_name]['#items'][0]->target_id,
$default_images['instance2']->id(),
format_string(
'An existing page node without an image has the expected default image file ID of @fid.',
@ -237,7 +237,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Confirm the article uses the new default.
$this->assertEqual(
$article_built[$field_name]['#items'][0]['target_id'],
$article_built[$field_name]['#items'][0]->target_id,
$default_images['instance_new']->id(),
format_string(
'An existing article node without an image has the expected default image file ID of @fid.',
@ -246,7 +246,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
);
// Confirm the page remains unchanged.
$this->assertEqual(
$page_built[$field_name]['#items'][0]['target_id'],
$page_built[$field_name]['#items'][0]->target_id,
$default_images['instance2']->id(),
format_string(
'An existing page node without an image has the expected default image file ID of @fid.',
@ -271,7 +271,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$page_built = node_view($page = node_load($page->id(), TRUE));
// Confirm the article uses the new field (not instance) default.
$this->assertEqual(
$article_built[$field_name]['#items'][0]['target_id'],
$article_built[$field_name]['#items'][0]->target_id,
$default_images['field_new']->id(),
format_string(
'An existing article node without an image has the expected default image file ID of @fid.',
@ -280,7 +280,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
);
// Confirm the page remains unchanged.
$this->assertEqual(
$page_built[$field_name]['#items'][0]['target_id'],
$page_built[$field_name]['#items'][0]->target_id,
$default_images['instance2']->id(),
format_string(
'An existing page node without an image has the expected default image file ID of @fid.',

View File

@ -7,6 +7,7 @@
namespace Drupal\image\Tests;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\simpletest\WebTestBase;
/**
@ -19,7 +20,19 @@ class ImageThemeFunctionTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('image');
public static $modules = array('image', 'entity_test');
/**
* Created file entity.
*
* @var \Drupal\file\Entity\File
*/
protected $image;
/**
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
public static function getInfo() {
return array(
@ -29,6 +42,28 @@ class ImageThemeFunctionTest extends WebTestBase {
);
}
public function setUp() {
parent::setUp();
entity_create('field_entity', array(
'name' => 'image_test',
'entity_type' => 'entity_test',
'type' => 'image',
'cardinality' => FieldDefinitionInterface::CARDINALITY_UNLIMITED,
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test',
'field_name' => 'image_test',
'bundle' => 'entity_test',
))->save();
file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = entity_create('file', array(
'uri' => 'public://example.jpg',
));
$this->image->save();
$this->imageFactory = $this->container->get('image.factory');
}
/**
* Tests usage of the image field formatters.
*/
@ -43,28 +78,33 @@ class ImageThemeFunctionTest extends WebTestBase {
$style->save();
$url = $style->buildUrl($original_uri);
// Create a test entity with the image field set.
$entity = entity_create('entity_test', array());
$entity->image_test->target_id = $this->image->id();
$entity->image_test->alt = NULL;
$entity->image_test->uri = $original_uri;
$image = $this->imageFactory->get('public://example.jpg');
$entity->save();
// Test using theme_image_formatter() with a NULL value for the alt option.
$path = $this->randomName();
$element = array(
'#theme' => 'image_formatter',
'#image_style' => 'test',
'#item' => array(
'uri' => $original_uri,
'alt' => NULL,
),
'#item' => $entity->image_test,
'#path' => array(
'path' => $path,
),
);
$rendered_element = render($element);
$expected_result = '<a href="' . base_path() . $path . '"><img class="image-style-test" src="' . $url . '" /></a>';
$expected_result = '<a href="' . base_path() . $path . '"><img class="image-style-test" src="' . $url . '" width="' . $image->getWidth() . '" height="' . $image->getHeight() . '" /></a>';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders with a NULL value for the alt option.');
// Test using theme_image_formatter() without an image title, alt text, or
// link options.
unset($element['#item']['alt']);
$element['#item']->alt = '';
$rendered_element = render($element);
$expected_result = '<a href="' . base_path() . $path . '"><img class="image-style-test" src="' . $url . '" alt="" /></a>';
$expected_result = '<a href="' . base_path() . $path . '"><img class="image-style-test" src="' . $url . '" width="' . $image->getWidth() . '" height="' . $image->getHeight() . '" alt="" /></a>';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders without title, alt, or path options.');
// Link the image to a fragment on the page, and not a full URL.
@ -75,7 +115,7 @@ class ImageThemeFunctionTest extends WebTestBase {
'fragment' => $fragment,
);
$rendered_element = render($element);
$expected_result = '<a href="#' . $fragment . '"><img class="image-style-test" src="' . $url . '" alt="" /></a>';
$expected_result = '<a href="#' . $fragment . '"><img class="image-style-test" src="' . $url . '" width="' . $image->getWidth() . '" height="' . $image->getHeight() . '" alt="" /></a>';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders a link fragment.');
}

View File

@ -172,7 +172,7 @@ class PictureFormatter extends ImageFormatterBase {
'#attached' => array('library' => array(
array('picture', 'picturefill'),
)),
'#item' => $item->getValue(TRUE),
'#item' => $item,
'#image_style' => $fallback_image_style,
'#breakpoints' => $breakpoint_styles,
'#path' => isset($uri) ? $uri : '',

View File

@ -163,7 +163,7 @@ function picture_theme() {
*
* @param array $variables
* An associative array containing:
* - item: An array of image data.
* - item: An ImageItem object.
* - image_style: An optional image style.
* - path: An optional array containing the link 'path' and link 'options'.
* - breakpoints: An array containing breakpoints.
@ -171,10 +171,11 @@ function picture_theme() {
* @ingroup themeable
*/
function theme_picture_formatter($variables) {
$item = $variables['item'];
if (!isset($variables['breakpoints']) || empty($variables['breakpoints'])) {
$image_formatter = array(
'#theme' => 'image_formatter',
'#item' => $variables['item'],
'#item' => $item,
'#image_style' => $variables['image_style'],
'#path' => $variables['path'],
);
@ -183,23 +184,21 @@ function theme_picture_formatter($variables) {
$picture = array(
'#theme' => 'picture',
'#width' => $variables['item']['width'],
'#height' => $variables['item']['height'],
'#width' => $item->width,
'#height' => $item->height,
'#style_name' => $variables['image_style'],
'#breakpoints' => $variables['breakpoints'],
);
if (isset($variables['item']['uri'])) {
$picture['#uri'] = $variables['item']['uri'];
if (isset($item->uri)) {
$picture['#uri'] = $item->uri;
}
elseif (isset($variables['item']['entity'])) {
$picture['#uri'] = $variables['item']['entity']->getFileUri();
$picture['#entity'] = $variables['item']['entity'];
elseif ($entity = $item->entity) {
$picture['#uri'] = $entity->getFileUri();
$picture['#entity'] = $entity;
}
if (isset($variables['item']['alt'])) {
$picture['#alt'] = $variables['item']['alt'];
}
if (isset($variables['item']['title']) && drupal_strlen($variables['item']['title']) != 0) {
$picture['#title'] = $variables['item']['title'];
$picture['#alt'] = $item->alt;
if (drupal_strlen($item->title) != 0) {
$picture['#title'] = $item->title;
}
if (isset($variables['path']['path'])) {
$path = $variables['path']['path'];