Skip to content

Commit

Permalink
Merge pull request #16162 from craftcms/bugfix/16158-creating-new-nes…
Browse files Browse the repository at this point in the history
…ted-entry-in-non-primary-site

Bugfix/16158 creating new nested entry in non primary site
  • Loading branch information
brandonkelly authored Nov 22, 2024
2 parents 2e50e71 + e076949 commit 22e4e9c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 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 @@

- Element indexes now sort by ID by default, for sources that don’t define a default sort option.
- Fixed a bug where element indexes were sorting by the first sortable attribute alphabetically by default, rather than the first sortable attribute defined by the element type.
- Fixed an error that could occur when creating a nested element. ([#16162](https://github.com/craftcms/cms/pull/16162))

## 5.5.2 - 2024-11-19

Expand Down
3 changes: 1 addition & 2 deletions src/base/NestedElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ public function getPrimaryOwner(): ?ElementInterface
return null;
}

$this->_primaryOwner = Craft::$app->getElements()->getElementById($primaryOwnerId, null, null, [
$this->_primaryOwner = Craft::$app->getElements()->getElementById($primaryOwnerId, null, $this->siteId, [
'trashed' => null,
'preferSites' => [$this->siteId],
]) ?? false;
if (!$this->_primaryOwner) {
throw new InvalidConfigException("Invalid owner ID: $primaryOwnerId");
Expand Down
9 changes: 8 additions & 1 deletion src/helpers/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use Illuminate\Support\Collection;
use yii\base\Event;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\helpers\Markdown;
use yii\validators\RequiredValidator;

Expand Down Expand Up @@ -892,6 +893,12 @@ private static function baseElementAttributes(ElementInterface $element, array $
$user = Craft::$app->getUser()->getIdentity();
$editable = $user && $elementsService->canView($element, $user);

$primaryOwner = null;
try {
$primaryOwner = $element instanceof NestedElementInterface ? $element->getPrimaryOwner() : null;
} catch (InvalidConfigException $e) {
}

return ArrayHelper::merge(
Html::normalizeTagAttributes($element->getHtmlAttributes($config['context'])),
[
Expand All @@ -909,7 +916,7 @@ private static function baseElementAttributes(ElementInterface $element, array $
'field-id' => $element instanceof NestedElementInterface ? $element->getField()?->id : null,
'primary-owner-id' => $element instanceof NestedElementInterface ? $element->getPrimaryOwnerId() : null,
'owner-id' => $element instanceof NestedElementInterface ? $element->getOwnerId() : null,
'owner-is-canonical' => $element instanceof NestedElementInterface ? $element->getPrimaryOwner()?->getIsCanonical() : null,
'owner-is-canonical' => $primaryOwner?->getIsCanonical(),
'site-id' => $element->siteId,
'status' => $element->getStatus(),
'label' => (string)$element,
Expand Down

0 comments on commit 22e4e9c

Please sign in to comment.