Skip to content

Commit 8723387

Browse files
committed
Improved field layout designer performance
No longer creating new user & element conditions for every single field layout component; just a single default instance for each, plus any actual conditions that are saved to the component. (Tried memoizing the builder HTML instead, but that gets pretty complicated with namespaces + JS.) Resolves craftcms#11298
1 parent 8a00441 commit 8723387

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Changed
66
- `temp` is now a reserved volume handle.
7+
- Improved the performance of field layout designers. ([#11298](https://github.com/craftcms/cms/issues/11298))
78

89
### Fixed
910
- Fixed a bug where it wasn’t possible to disable all table columns for an element source. ([#11291](https://github.com/craftcms/cms/issues/11291))

src/base/FieldLayoutComponent.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@
2626
*/
2727
abstract class FieldLayoutComponent extends Model
2828
{
29+
/**
30+
* @var UserCondition
31+
*/
32+
private static UserCondition $defaultUserCondition;
33+
34+
/**
35+
* @var ElementConditionInterface[]
36+
*/
37+
private static array $defaultElementConditions = [];
38+
39+
/**
40+
* @return UserCondition
41+
*/
42+
private static function defaultUserCondition(): UserCondition
43+
{
44+
if (!isset(self::$defaultUserCondition)) {
45+
self::$defaultUserCondition = User::createCondition();
46+
}
47+
return self::$defaultUserCondition;
48+
}
49+
50+
/**
51+
* @param string $elementType
52+
* @phpstan-param class-string<ElementInterface>
53+
* @return ElementConditionInterface
54+
*/
55+
private static function defaultElementCondition(string $elementType): ElementConditionInterface
56+
{
57+
if (!isset(self::$defaultElementConditions[$elementType])) {
58+
/** @var string|ElementInterface $elementType */
59+
self::$defaultElementConditions[$elementType] = $elementType::createCondition();
60+
}
61+
return self::$defaultElementConditions[$elementType];
62+
}
63+
2964
/**
3065
* @var string|null The UUID of the layout element.
3166
*/
@@ -193,7 +228,7 @@ public function getSettingsHtml(): string
193228
$html .= '<hr>';
194229
}
195230

196-
$userCondition = $this->_userCondition ?? User::createCondition();
231+
$userCondition = $this->_userCondition ?? self::defaultUserCondition();
197232
$userCondition->mainTag = 'div';
198233
$userCondition->id = 'user-condition';
199234
$userCondition->name = 'userCondition';
@@ -209,7 +244,7 @@ public function getSettingsHtml(): string
209244
$elementType = $this->getLayout()->type;
210245

211246
if ($elementType && is_subclass_of($elementType, ElementInterface::class)) {
212-
$elementCondition = $this->_elementCondition ?? $elementType::createCondition();
247+
$elementCondition = $this->_elementCondition ?? self::defaultElementCondition($elementType);
213248
$elementCondition->mainTag = 'div';
214249
$elementCondition->id = 'element-condition';
215250
$elementCondition->name = 'elementCondition';

0 commit comments

Comments
 (0)