Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested element manager provider #16167

Draft
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/base/NestedElementManagerProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\base;

use craft\elements\NestedElementManager;

/**
* NestedElementManagerProviderInterface defines the common interface to be implemented by elements that provide
* one or more nested element providers for the elements that can be owned by them.
*
*
* @mixin ElementTrait
* @mixin Component
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 5.x
*/
interface NestedElementManagerProviderInterface extends ElementInterface
{
/**
* Returns the nested element manager by the attribute name.
*
* @param string $attribute The attribute name
* @return ?NestedElementManager
*/
public function getNestedElementManager(string $attribute): ?NestedElementManager;
}
14 changes: 12 additions & 2 deletions src/controllers/NestedElementsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Craft;
use craft\base\ElementInterface;
use craft\base\NestedElementInterface;
use craft\base\NestedElementManagerProviderInterface;
use craft\db\Table;
use craft\elements\db\ElementQueryInterface;
use craft\elements\ElementCollection;
use craft\helpers\Db;
use craft\web\Controller;
use http\Exception\InvalidArgumentException;
use yii\web\BadRequestHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\Response;
Expand Down Expand Up @@ -65,8 +67,16 @@ public function beforeAction($action): bool
throw new ForbiddenHttpException('User is not authorized to perform this action');
}

// Set the nested elements for the action
$this->nestedElements = $this->owner->$attribute;
if ($this->owner instanceof NestedElementManagerProviderInterface) {
$manager = $this->owner->getNestedElementManager($attribute);
if (!$manager) {
throw new InvalidArgumentException('Owner does not provide a nested element manager for the given attribute');
}

$this->nestedElements = $manager->getValue($owner, true);
} else {
$this->nestedElements = $this->owner->$attribute;
}

return true;
}
Expand Down
15 changes: 13 additions & 2 deletions src/elements/NestedElementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ private function nestedElementQuery(ElementInterface $owner): ElementQueryInterf
return call_user_func($this->queryFactory, $owner);
}

private function getValue(ElementInterface $owner, bool $fetchAll = false): ElementQueryInterface|ElementCollection
/**
* @param ElementInterface $owner
* @param bool $fetchAll
* @return ElementQueryInterface|ElementCollection
* @throws \craft\errors\InvalidFieldException
*/
public function getValue(ElementInterface $owner, bool $fetchAll = false): ElementQueryInterface|ElementCollection
{
if (isset($this->valueGetter)) {
return call_user_func($this->valueGetter, $owner, $fetchAll);
Expand All @@ -199,7 +205,12 @@ private function getValue(ElementInterface $owner, bool $fetchAll = false): Elem
return $query;
}

private function setValue(ElementInterface $owner, ElementQueryInterface|ElementCollection $value): void
/**
* @param ElementInterface $owner
* @param ElementQueryInterface|ElementCollection $value
* @return void
*/
public function setValue(ElementInterface $owner, ElementQueryInterface|ElementCollection $value): void
{
if ($this->valueSetter === false) {
return;
Expand Down
14 changes: 13 additions & 1 deletion src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\NameTrait;
use craft\base\NestedElementManagerProviderInterface;
use craft\db\Query;
use craft\db\Table;
use craft\elements\actions\DeleteUsers;
Expand Down Expand Up @@ -85,7 +86,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class User extends Element implements IdentityInterface
class User extends Element implements IdentityInterface, NestedElementManagerProviderInterface
{
use NameTrait;

Expand Down Expand Up @@ -2549,6 +2550,17 @@ public function beforeDelete(): bool
return true;
}

/**
* @inheritdoc
*/
public function getNestedElementManager(string $attribute): ?NestedElementManager
{
return match ($attribute) {
'addresses' => $this->getAddressManager(),
default => null,
};
}

/**
* Validates a cookie's stored user agent against the current request's user agent string,
* if the 'requireMatchingUserAgentForSession' config setting is enabled.
Expand Down
Loading