Commit 44ffb271 authored by catch's avatar catch

Issue #3023220 by tim.plunkett, andyg5000, Berdir, phenaproxima, bkosborne:...

Issue #3023220 by tim.plunkett, andyg5000, Berdir, phenaproxima, bkosborne: Performance: Prevent extra Layout Builder code from running when rendering fields in isolation (Views results, FieldBlock, etc)
parent c04f963e
......@@ -72,10 +72,9 @@ public function setOverridable($overridable = TRUE) {
* {@inheritdoc}
*/
public function isLayoutBuilderEnabled() {
// To prevent infinite recursion, Layout Builder must not be enabled for the
// '_custom' view mode that is used for on-the-fly rendering of fields in
// isolation from the entity.
if ($this->getOriginalMode() === static::CUSTOM_MODE) {
// Layout Builder must not be enabled for the '_custom' view mode that is
// used for on-the-fly rendering of fields in isolation from the entity.
if ($this->isCustomMode()) {
return FALSE;
}
return (bool) $this->getThirdPartySetting('layout_builder', 'enabled');
......@@ -251,12 +250,28 @@ protected function contextRepository() {
return \Drupal::service('context.repository');
}
/**
* Indicates if this display is using the '_custom' view mode.
*
* @return bool
* TRUE if this display is using the '_custom' view mode, FALSE otherwise.
*/
protected function isCustomMode() {
return $this->getOriginalMode() === static::CUSTOM_MODE;
}
/**
* {@inheritdoc}
*/
public function buildMultiple(array $entities) {
$build_list = parent::buildMultiple($entities);
// Layout Builder can not be enabled for the '_custom' view mode that is
// used for on-the-fly rendering of fields in isolation from the entity.
if ($this->isCustomMode()) {
return $build_list;
}
foreach ($entities as $id => $entity) {
$build_list[$id]['_layout_builder'] = $this->buildSections($entity);
......
......@@ -61,6 +61,39 @@ public function testGetRuntimeSections() {
$this->assertEquals($this->sectionStorage->getSections(), $result);
}
/**
* @dataProvider providerTestIsLayoutBuilderEnabled
*/
public function testIsLayoutBuilderEnabled($expected, $view_mode, $enabled) {
$display = LayoutBuilderEntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => $view_mode,
'status' => TRUE,
'third_party_settings' => [
'layout_builder' => [
'enabled' => $enabled,
],
],
]);
$result = $display->isLayoutBuilderEnabled();
$this->assertSame($expected, $result);
}
/**
* Provides test data for ::testIsLayoutBuilderEnabled().
*/
public function providerTestIsLayoutBuilderEnabled() {
$data = [];
$data['default enabled'] = [TRUE, 'default', TRUE];
$data['default disabled'] = [FALSE, 'default', FALSE];
$data['full enabled'] = [TRUE, 'full', TRUE];
$data['full disabled'] = [FALSE, 'full', FALSE];
$data['_custom enabled'] = [FALSE, '_custom', TRUE];
$data['_custom disabled'] = [FALSE, '_custom', FALSE];
return $data;
}
/**
* Tests that setting overridable enables Layout Builder only when set to TRUE.
*/
......
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