Skip to content

Commit

Permalink
Element::getSource() => ElementHelper::sourceElement()
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Aug 16, 2019
1 parent 43caabc commit efab623
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 43 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

## Unreleased

### Added
- Added `craft\helpers\ElementHelper::sourceElement()`.

### Changed
- Element arrays no longer include `hasDescendants` or `totalDescendants` keys by default.
- Relational fields without a specific target site will now only return related elements from the same site as the source element by default, as they did before Craft 3.2. ([#4751](https://github.com/craftcms/cms/issues/4751))
- `source` is now a reserved field handle. ([#4754](https://github.com/craftcms/cms/issues/4754))
- Improved the performance of element duplication on multi-site installs.
- Edit Entry pages now get updated preview target URLs after saving a draft, in case the URLs have changed.

### Removed
- Removed `craft\base\ElementInterface::getSource()`. ([#4754](https://github.com/craftcms/cms/issues/4754))

### Fixed
- Fixed an error that could occur if garbage collection was run while Craft 3.2 migrations were pending. ([#4720](https://github.com/craftcms/cms/issues/4720))
- Fixed a validation error that occurred when duplicating an entry, if the URI format was based on a custom field value. ([#4759](https://github.com/craftcms/cms/issues/4759))
Expand Down
41 changes: 12 additions & 29 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,23 +1277,6 @@ public function getSourceUid(): string
->scalar();
}

/**
* @inheritdoc
*/
public function getSource(): ElementInterface
{
$sourceId = $this->getSourceId();
if ($sourceId === $this->id) {
return $this;
}
return static::find()
->id($sourceId)
->siteId($this->siteId)
->anyStatus()
->ignorePlaceholders()
->one();
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -1580,7 +1563,7 @@ public function getAncestors(int $dist = null)
{
return static::find()
->structureId($this->structureId)
->ancestorOf($this->getSource())
->ancestorOf(ElementHelper::sourceElement($this))
->siteId($this->siteId)
->ancestorDist($dist);
}
Expand All @@ -1597,7 +1580,7 @@ public function getDescendants(int $dist = null)

return static::find()
->structureId($this->structureId)
->descendantOf($this->getSource())
->descendantOf(ElementHelper::sourceElement($this))
->siteId($this->siteId)
->descendantDist($dist);
}
Expand All @@ -1622,7 +1605,7 @@ public function getSiblings()
{
return static::find()
->structureId($this->structureId)
->siblingOf($this->getSource())
->siblingOf(ElementHelper::sourceElement($this))
->siteId($this->siteId);
}

Expand All @@ -1635,7 +1618,7 @@ public function getPrevSibling()
/** @var ElementQuery $query */
$query = $this->_prevSibling = static::find();
$query->structureId = $this->structureId;
$query->prevSiblingOf = $this->getSource();
$query->prevSiblingOf = ElementHelper::sourceElement($this);
$query->siteId = $this->siteId;
$query->anyStatus();
$this->_prevSibling = $query->one();
Expand All @@ -1657,7 +1640,7 @@ public function getNextSibling()
/** @var ElementQuery $query */
$query = $this->_nextSibling = static::find();
$query->structureId = $this->structureId;
$query->nextSiblingOf = $this->getSource();
$query->nextSiblingOf = ElementHelper::sourceElement($this);
$query->siteId = $this->siteId;
$query->anyStatus();
$this->_nextSibling = $query->one();
Expand Down Expand Up @@ -1700,7 +1683,7 @@ public function getTotalDescendants(): int
public function isAncestorOf(ElementInterface $element): bool
{
/** @var Element $source */
$source = $this->getSource();
$source = ElementHelper::sourceElement($this);
/** @var Element $element */
return ($source->root == $element->root && $source->lft < $element->lft && $source->rgt > $element->rgt);
}
Expand All @@ -1711,7 +1694,7 @@ public function isAncestorOf(ElementInterface $element): bool
public function isDescendantOf(ElementInterface $element): bool
{
/** @var Element $source */
$source = $this->getSource();
$source = ElementHelper::sourceElement($this);
/** @var Element $element */
return ($source->root == $element->root && $source->lft > $element->lft && $source->rgt < $element->rgt);
}
Expand All @@ -1722,7 +1705,7 @@ public function isDescendantOf(ElementInterface $element): bool
public function isParentOf(ElementInterface $element): bool
{
/** @var Element $source */
$source = $this->getSource();
$source = ElementHelper::sourceElement($this);
/** @var Element $element */
return ($source->root == $element->root && $source->level == $element->level - 1 && $source->isAncestorOf($element));
}
Expand All @@ -1733,7 +1716,7 @@ public function isParentOf(ElementInterface $element): bool
public function isChildOf(ElementInterface $element): bool
{
/** @var Element $source */
$source = $this->getSource();
$source = ElementHelper::sourceElement($this);
/** @var Element $element */
return ($source->root == $element->root && $source->level == $element->level + 1 && $source->isDescendantOf($element));
}
Expand All @@ -1744,7 +1727,7 @@ public function isChildOf(ElementInterface $element): bool
public function isSiblingOf(ElementInterface $element): bool
{
/** @var Element $source */
$source = $this->getSource();
$source = ElementHelper::sourceElement($this);
/** @var Element $element */
if ($source->root == $element->root && $source->level !== null && $source->level == $element->level) {
if ($source->level == 1 || $source->isPrevSiblingOf($element) || $source->isNextSiblingOf($element)) {
Expand All @@ -1767,7 +1750,7 @@ public function isSiblingOf(ElementInterface $element): bool
public function isPrevSiblingOf(ElementInterface $element): bool
{
/** @var Element $source */
$source = $this->getSource();
$source = ElementHelper::sourceElement($this);
/** @var Element $element */
return ($source->root == $element->root && $source->level == $element->level && $source->rgt == $element->lft - 1);
}
Expand All @@ -1778,7 +1761,7 @@ public function isPrevSiblingOf(ElementInterface $element): bool
public function isNextSiblingOf(ElementInterface $element): bool
{
/** @var Element $source */
$source = $this->getSource();
$source = ElementHelper::sourceElement($this);
/** @var Element $element */
return ($source->root == $element->root && $source->level == $element->level && $source->lft == $element->rgt + 1);
}
Expand Down
8 changes: 0 additions & 8 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,6 @@ public function getSourceId();
*/
public function getSourceUid(): string;

/**
* Returns the element, or if it’s a draft/revision, the source element.
*
* @return ElementInterface
* @since 3.2.9
*/
public function getSource(): ElementInterface;

/**
* Returns whether the element is an unsaved draft.
*
Expand Down
1 change: 0 additions & 1 deletion src/base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ public function rules()
'site',
'slug',
'sortOrder',
'source',
'status',
'title',
'uid',
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/EntryRevisionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function actionPublishDraft()

// Permission enforcement
/** @var Entry|null $entry */
$entry = $draft->getSource();
$entry = ElementHelper::sourceElement($draft);
$this->enforceEditEntryPermissions($entry);
$section = ($entry)->getSection();

Expand Down Expand Up @@ -429,7 +429,7 @@ public function actionRevertEntryToVersion()

// Permission enforcement
/** @var Entry $entry */
$entry = $revision->getSource();
$entry = ElementHelper::sourceElement($revision);

$this->enforceEditEntryPermissions($entry);
$userId = Craft::$app->getUser()->getId();
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/ElementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,29 @@ public static function isDraftOrRevision(ElementInterface $element): bool
return $root->getIsDraft() || $root->getIsRevision();
}

/**
* Returns the element, or if it’s a draft/revision, the source element.
*
* @param ElementInterface $element
* @return ElementInterface
* @since 3.2.11
*/
public static function sourceElement(ElementInterface $element): ElementInterface
{
/** @var Element $element */
$sourceId = $element->getSourceId();
if ($sourceId === $element->id) {
return $element;
}

return $element::find()
->id($sourceId)
->siteId($element->siteId)
->anyStatus()
->ignorePlaceholders()
->one();
}

/**
* Given an array of elements, will go through and set the appropriate "next"
* and "prev" elements on them.
Expand Down
3 changes: 2 additions & 1 deletion src/services/Drafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use craft\events\DraftEvent;
use craft\helpers\DateTimeHelper;
use craft\helpers\Db;
use craft\helpers\ElementHelper;
use yii\base\Component;
use yii\base\InvalidArgumentException;
use yii\db\Exception as DbException;
Expand Down Expand Up @@ -210,7 +211,7 @@ public function applyDraft(ElementInterface $draft): ElementInterface
/** @var DraftBehavior $behavior */
$behavior = $draft->getBehavior('draft');
/** @var Element $source */
$source = $draft->getSource();
$source = ElementHelper::sourceElement($draft);

// Fire a 'beforeApplyDraft' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_APPLY_DRAFT)) {
Expand Down
3 changes: 2 additions & 1 deletion src/services/Revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use craft\errors\InvalidElementException;
use craft\events\RevisionEvent;
use craft\helpers\ArrayHelper;
use craft\helpers\ElementHelper;
use craft\helpers\Json;
use yii\base\Component;
use yii\base\InvalidArgumentException;
Expand Down Expand Up @@ -219,7 +220,7 @@ public function revertToRevision(ElementInterface $revision, int $creatorId): El
{
/** @var Element|RevisionBehavior $revision */
/** @var Element $source */
$source = $revision->getSource();
$source = ElementHelper::sourceElement($revision);

// Fire a 'beforeRevertToRevision' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_REVERT_TO_REVISION)) {
Expand Down

0 comments on commit efab623

Please sign in to comment.