Issue #3341448 by acbramley, fenstrat, Lendude, smustgrave: EntityReference ViewsSelection::stripAdminAndAnchorTagsFromResults() should call Element::children($results)

merge-requests/3789/head
Lee Rowlands 2023-04-06 12:01:35 +10:00
parent f4fcd7b578
commit bd88cad236
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
3 changed files with 86 additions and 3 deletions

View File

@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
@ -283,10 +284,10 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
}
$stripped_results = [];
foreach ($results as $id => $row) {
$entity = $row['#row']->_entity;
foreach (Element::children($results) as $id) {
$entity = $results[$id]['#row']->_entity;
$stripped_results[$entity->bundle()][$id] = ViewsRenderPipelineMarkup::create(
Xss::filter($this->renderer->renderPlain($row), $allowed_tags)
Xss::filter($this->renderer->renderPlain($results[$id]), $allowed_tags)
);
}

View File

@ -5,6 +5,9 @@
* Contains the "views_test_config" module main functionality.
*/
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\views\ViewExecutable;
/**
* Implements hook_ENTITY_TYPE_load().
*/
@ -17,3 +20,15 @@ function views_test_config_view_load(array $views) {
$display['display_options']['fields']['id_broken'] = NULL;
}
}
/**
* Implements hook_views_post_render().
*/
function views_test_config_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
if (\Drupal::state()->get('views_test_config.views_post_render_cache_tag')) {
\Drupal::state()->set('views_test_config.views_post_render_called', TRUE);
// Set a cache key on output to ensure ViewsSelection::stripAdminAndAnchorTagsFromResults
// correctly handles elements that aren't result rows.
$output['#cache']['tags'][] = 'foo';
}
}

View File

@ -0,0 +1,67 @@
<?php
declare(strict_types = 1);
namespace Drupal\Tests\views\Kernel\Entity;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Tests the ViewSelection EntityReferenceSelection plugin.
*
* @group views
*/
class ViewSelectionEntityReferenceTest extends EntityKernelTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_display_entity_reference'];
/**
* {@inheritdoc}
*/
protected static $modules = ['views', 'views_test_config'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
ViewTestData::createTestViews(static::class, ['views_test_config']);
}
/**
* Tests the ViewSelection plugin.
*/
public function testSelectionPlugin(): void {
for ($i = 1; $i <= 5; $i++) {
$entity = EntityTest::create([
'name' => 'Test entity ' . $i,
]);
$entity->save();
}
$selection_options = [
'target_type' => 'entity_test',
'handler' => 'views',
'view' => [
'view_name' => 'test_display_entity_reference',
'display_name' => 'entity_reference_1',
],
];
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);
$state = \Drupal::state();
$this->assertNull($state->get('views_test_config.views_post_render_called'));
$state->set('views_test_config.views_post_render_cache_tag', TRUE);
$result = $handler->getReferenceableEntities();
$this->assertCount(5, $result['entity_test']);
$this->assertTrue($state->get('views_test_config.views_post_render_called'));
}
}