Commit 46e20349 authored by alexpott's avatar alexpott
Browse files

Issue #2496039 by yched: Formatter's #attached assets are not carried over by Views

parent 4af9619a
......@@ -9,7 +9,6 @@
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss as CoreXss;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
......@@ -18,6 +17,7 @@
use Drupal\Core\Form\FormHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
......@@ -832,17 +832,19 @@ public function getItems(ResultRow $values) {
// Render using the formatted data itself.
$items = [];
// Each item is extracted and rendered separately, the top-level formatter
// render array itself is never rendered, so we extract its bubbleable
// metadata and add it to each child individually.
$bubbleable = BubbleableMetadata::createFromRenderArray($build_list);
foreach (Element::children($build_list) as $delta) {
$items[$delta]['rendered'] = $build_list[$delta];
// Merge the cacheability metadata of the top-level render array into
// each child because they will most likely be rendered individually.
if (isset($build_list['#cache'])) {
CacheableMetadata::createFromRenderArray($build_list)
->merge(CacheableMetadata::createFromRenderArray($items[$delta]['rendered']))
->applyTo($items[$delta]['rendered']);
}
// Add the raw field items (for use in tokens).
$items[$delta]['raw'] = $build_list['#items'][$delta];
BubbleableMetadata::createFromRenderArray($build_list[$delta])
->merge($bubbleable)
->applyTo($build_list[$delta]);
$items[$delta] = [
'rendered' => $build_list[$delta],
// Add the raw field items (for use in tokens).
'raw' => $build_list['#items'][$delta],
];
}
return $items;
}
......
......@@ -28,12 +28,12 @@ class FieldFieldTest extends ViewUnitTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['field', 'entity_test', 'user'];
public static $modules = ['field', 'entity_test', 'user', 'views_test_formatter'];
/**
* {@inheritdoc}
*/
public static $testViews = ['test_field_field_test', 'test_field_alias_test', 'test_field_field_complex_test', 'test_field_field_revision_test', 'test_field_field_revision_complex_test'];
public static $testViews = ['test_field_field_test', 'test_field_alias_test', 'test_field_field_complex_test', 'test_field_field_attachment_test', 'test_field_field_revision_test', 'test_field_field_revision_complex_test'];
/**
* The stored test entities.
......@@ -265,6 +265,30 @@ public function testSimpleRender() {
$this->assertEqual(6, $executable->getStyle()->getField(4, 'field_test'));
}
/**
* Tests that formatter's #attached assets are correctly preserved.
*
* @see \Drupal\views_test_formatter\Plugin\Field\FieldFormatter\AttachmentTestFormatter::viewElements()
*/
public function testAttachedRender() {
$executable = Views::getView('test_field_field_attachment_test');
$executable->execute();
// Check that the attachments added by AttachmentTestFormatter have been
// preserved in the render array.
$render = $executable->display_handler->render();
$expected_attachments = [
'library' => [
'views/views.module'
]
];
foreach ($this->entities as $entity) {
$expected_attachments['library'][] = 'foo/fake_library';
$expected_attachments['drupalSettings']['AttachmentIntegerFormatter'][$entity->id()] = $entity->id();
}
$this->assertEqual($expected_attachments, $render['#attached']);
}
/**
* Tests the result of a view with complex field configuration.
*
......
langcode: en
status: true
dependencies:
config:
- field.storage.entity_test.field_test
module:
- entity_test
id: test_field_field_attachment_test
label: test_field_field_attachment_test
module: views
description: ''
tag: ''
base_table: entity_test
base_field: id
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: none
options: { }
cache:
type: none
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: none
options:
offset: 0
style:
type: default
options:
grouping: { }
row_class: ''
default_row_class: true
uses_fields: false
row:
type: fields
options:
inline: { }
separator: ''
hide_empty: false
default_field_elements: true
fields:
field_test:
id: field_test
table: entity_test__field_test
field: field_test
relationship: none
group_type: group
admin_label: ''
label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: number_unformatted_with_attachment
settings: { }
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
plugin_id: field
filters: { }
sorts: { }
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
display_extenders: { }
cache_metadata:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
cacheable: false
<?php
/**
* @file
* Contains \Drupal\views_test_formatter\Field\Plugin\Field\FieldFormatter\AttachmentTestFormatter.
*/
namespace Drupal\views_test_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericUnformattedFormatter;
/**
* Plugin implementation of the 'number_unformatted_with_attachment' formatter.
*
* @FieldFormatter(
* id = "number_unformatted_with_attachment",
* label = @Translation("Unformatted, with attachments"),
* field_types = {
* "integer",
* "decimal",
* "float"
* }
* )
*/
class AttachmentTestFormatter extends NumericUnformattedFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = parent::viewElements($items);
// Add dummy attachments.
$entity_id = $items->getEntity()->id();
$elements['#attached']['library'][] = 'foo/fake_library';
$elements['#attached']['drupalSettings']['AttachmentIntegerFormatter'][$entity_id] = $entity_id;
return $elements;
}
}
name: 'Views Test Formatter'
type: module
description: 'Provides test field formatters.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- views
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment