Skip to content

Commit

Permalink
Improved field layout designer performance
Browse files Browse the repository at this point in the history
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 #11241
  • Loading branch information
brandonkelly committed May 24, 2022
1 parent 8a00441 commit 1a8f227
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

### Fixed
- 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))
Expand Down
39 changes: 37 additions & 2 deletions src/base/FieldLayoutComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@
*/
abstract class FieldLayoutComponent extends Model
{
/**
* @var UserCondition
*/
private static UserCondition $defaultUserCondition;

/**
* @var ElementConditionInterface[]
*/
private static array $defaultElementConditions = [];

/**
* @return UserCondition
*/
private static function defaultUserCondition(): UserCondition
{
if (!isset(self::$defaultUserCondition)) {
self::$defaultUserCondition = User::createCondition();
}
return self::$defaultUserCondition;
}

/**
* @param string $elementType
* @phpstan-param class-string<ElementInterface>
* @return ElementConditionInterface
*/
private static function defaultElementCondition(string $elementType): ElementConditionInterface
{
if (!isset(self::$defaultElementConditions[$elementType])) {
/** @var string|ElementInterface $elementType */
self::$defaultElementConditions[$elementType] = $elementType::createCondition();
}
return self::$defaultElementConditions[$elementType];
}

/**
* @var string|null The UUID of the layout element.
*/
Expand Down Expand Up @@ -193,7 +228,7 @@ public function getSettingsHtml(): string
$html .= '<hr>';
}

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

if ($elementType && is_subclass_of($elementType, ElementInterface::class)) {
$elementCondition = $this->_elementCondition ?? $elementType::createCondition();
$elementCondition = $this->_elementCondition ?? self::defaultElementCondition($elementType);
$elementCondition->mainTag = 'div';
$elementCondition->id = 'element-condition';
$elementCondition->name = 'elementCondition';
Expand Down

0 comments on commit 1a8f227

Please sign in to comment.