Skip to content
Snippets Groups Projects
Commit a62995de authored by catch's avatar catch
Browse files

Issue #3442227 by dww, smustgrave: Use labels in Views argument summaries for entity references

parent 75be7826
No related branches found
Tags 1.0.2
27 merge requests!11131[10.4.x-only-DO-NOT-MERGE]: Issue ##2842525 Ajax attached to Views exposed filter form does not trigger callbacks,!9470[10.3.x-only-DO-NOT-MERGE]: #3331771 Fix file_get_contents(): Passing null to parameter,!8736Update the Documention As per the Function uses.,!8540Issue #3457061: Bootstrap Modal dialog Not closing after 10.3.0 Update,!8528Issue #3456871 by Tim Bozeman: Support NULL services,!8513Issue #3453786: DefaultSelection should document why values for target_bundles NULL and [] behave as they do,!8373Issue #3427374 by danflanagan8, Vighneshh: taxonomy_tid ViewsArgumentDefault...,!8256Issue #3445896 by mstrelan, mondrake: PHPUnit\Runner\ErrorHandler::__construct...,!8126Added escape fucntionality on admintoolbar close icon,!3878Removed unused condition head title for views,!3818Issue #2140179: $entity->original gets stale between updates,!3742Issue #3328429: Create item list field formatter for displaying ordered and unordered lists,!3731Claro: role=button on status report items,!3651Issue #3347736: Create new SDC component for Olivero (header-search),!3531Issue #3336994: StringFormatter always displays links to entity even if the user in context does not have access,!3355Issue #3209129: Scrolling problems when adding a block via layout builder,!3154Fixes #2987987 - CSRF token validation broken on routes with optional parameters.,!3133core/modules/system/css/components/hidden.module.css,!2812Issue #3312049: [Followup] Fix Drupal.Commenting.FunctionComment.MissingReturnType returns for NULL,!2378Issue #2875033: Optimize joins and table selection in SQL entity query implementation,!2062Issue #3246454: Add weekly granularity to views date sort,!1105Issue #3025039: New non translatable field on translatable content throws error,!877Issue #2708101: Default value for link text is not saved,!617Issue #3043725: Provide a Entity Handler for user cancelation,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493
Pipeline #187990 passed
......@@ -61,4 +61,19 @@ public function titleQuery() {
return $titles;
}
/**
* {@inheritdoc}
*/
public function summaryName($data) {
$id = $data->{$this->name_alias};
$entity = $id ? $this->entityTypeManager->getStorage($this->definition['target_entity_type_id'])->load($id) : NULL;
if ($entity) {
return $this->entityRepository->getTranslationFromContext($entity)->label();
}
if (($id === NULL || $id === '') && isset($this->definition['empty field name'])) {
return $this->definition['empty field name'];
}
return $id;
}
}
langcode: en
status: true
dependencies:
config:
- core.entity_view_mode.node.teaser
- node.type.article
module:
- node
- user
id: test_argument_summary
label: test_argument_summary
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
id: default
display_title: Default
display_plugin: default
position: 0
display_options:
title: test_argument_summary
fields:
title:
id: title
table: node_field_data
field: title
relationship: none
group_type: group
admin_label: ''
entity_type: node
entity_field: title
plugin_id: field
label: ''
exclude: false
alter:
alter_text: false
make_link: false
absolute: false
word_boundary: false
ellipsis: false
strip_tags: false
trim: false
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
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: string
settings:
link_to_entity: true
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
pager:
type: none
options:
offset: 0
items_per_page: 0
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
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
empty: { }
sorts:
created:
id: created
table: node_field_data
field: created
relationship: none
group_type: group
admin_label: ''
entity_type: node
entity_field: created
plugin_id: date
order: DESC
expose:
label: ''
field_identifier: ''
exposed: false
granularity: second
arguments:
field_tags_target_id:
id: field_tags_target_id
table: node__field_tags
field: field_tags_target_id
relationship: none
group_type: group
admin_label: ''
plugin_id: entity_target_id
default_action: summary
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options:
base_path: ''
count: true
override: false
items_per_page: 25
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
break_phrase: false
not: false
filters:
status:
id: status
table: node_field_data
field: status
entity_type: node
entity_field: status
plugin_id: boolean
value: '1'
group: 1
expose:
operator: ''
type:
id: type
table: node_field_data
field: type
entity_type: node
entity_field: type
plugin_id: bundle
value:
article: article
style:
type: default
row:
type: 'entity:node'
options:
view_mode: teaser
query:
type: views_query
options:
query_comment: ''
disable_sql_rewrite: false
distinct: false
replica: false
query_tags: { }
relationships: { }
header: { }
footer: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- 'user.node_grants:view'
- user.permissions
tags: { }
page_1:
id: page_1
display_title: Page
display_plugin: page
position: 1
display_options:
display_extenders: { }
path: test-argument-summary
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- 'user.node_grants:view'
- user.permissions
tags: { }
<?php
declare(strict_types=1);
namespace Drupal\Tests\views\Kernel\Handler;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\views\Views;
/**
* Tests the summary of results when an argument is not provided.
*
* @group views
*/
class ArgumentSummaryTest extends ViewsKernelTestBase {
use EntityReferenceFieldCreationTrait;
/**
* {@inheritdoc}
*/
public static $testViews = ['test_argument_summary'];
/**
* {@inheritdoc}
*/
protected static $modules = [
'field',
'node',
'taxonomy',
'text',
'user',
'views',
];
/**
* Node type with an autocomplete tagging field.
*
* @var \Drupal\node\NodeTypeInterface
*/
protected NodeTypeInterface $nodeType;
/**
* The vocabulary used for the test tag field.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
protected VocabularyInterface $tagVocabulary;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE): void {
parent::setUp($import_test_views);
$this->installEntitySchema('node');
$this->installEntitySchema('taxonomy_term');
$this->installEntitySchema('user');
// Create the content type with an autocomplete tagging field.
$this->nodeType = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$this->nodeType->save();
// Create the vocabulary for the tag field.
$this->tagVocabulary = Vocabulary::create([
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
]);
$this->tagVocabulary->save();
// Create the tag field itself.
$handler_settings = [
'target_bundles' => [
$this->tagVocabulary->id() => $this->tagVocabulary->id(),
],
'auto_create' => TRUE,
];
$this->createEntityReferenceField(
'node',
$this->nodeType->id(),
'field_tags',
NULL,
'taxonomy_term',
'default',
$handler_settings,
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
);
}
/**
* Creates a term in the tag vocabulary.
*
* @return \Drupal\taxonomy\TermInterface $term
*/
protected function createTag(): TermInterface {
$tag = Term::create([
'name' => $this->randomMachineName(),
'vid' => $this->tagVocabulary->id(),
]);
$tag->save();
return $tag;
}
/**
* Tests the argument summary feature.
*/
public function testArgumentSummary(): void {
// Setup 2 tags.
$tags = [];
for ($i = 0; $i < 2; $i++) {
$tags[$i] = $this->createTag();
}
// Setup 4 nodes with different tags.
for ($i = 0; $i < 4; $i++) {
Node::create([
'type' => $this->nodeType->id(),
'title' => $this->randomMachineName(),
// For odd numbered nodes, use both tags, even only get 1 tag.
'field_tags' => ($i % 2) ? $tags : [$tags[0]->id()],
])->save();
}
$view = Views::getView('test_argument_summary');
$result = $view->preview('default');
// For the purposes of this test, we don't care about any markup or
// formatting, only that the summary is showing the tag labels and the
// correct counts. So strip all tags and extra whitespace to make the
// assertions more clear.
$renderer = $this->container->get('renderer');
$output = (string) $renderer->renderRoot($result);
$output = trim(preg_replace('/\s+/', ' ', strip_tags($output)));
// Output should show first tag on 4 nodes, the second tag on only 2.
$this->assertStringContainsString($tags[0]->label() . ' (4)', $output);
$this->assertStringContainsString($tags[1]->label() . ' (2)', $output);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment