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

remove relations for fields that are not part of the layout anymore #14368

Merged
merged 8 commits into from
Feb 13, 2024
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@

### Development
- The `{% js %}` and `{% css %}` tags now support `.js.gz` and `.css.gz` URLs. ([#14243](https://github.com/craftcms/cms/issues/14243))

### Extensibility
- Added `craft\services\Relations::deleteLeftoverRelations()`. ([#13956](https://github.com/craftcms/cms/issues/13956))

### System
- Relations for fields that are no longer included in an element’s field layout are now deleted after element save. ([#13956](https://github.com/craftcms/cms/issues/13956))
5 changes: 5 additions & 0 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -5182,6 +5182,11 @@ public function afterPropagate(bool $isNew): void
$field->afterElementPropagate($this, $isNew);
}

// Delete relations that don’t belong to a relational field on the element's field layout
if (!ElementHelper::isDraftOrRevision($this)) {
Craft::$app->getRelations()->deleteLeftoverRelations($this);
}

// Trigger an 'afterPropagate' event
if ($this->hasEventHandlers(self::EVENT_AFTER_PROPAGATE)) {
$this->trigger(self::EVENT_AFTER_PROPAGATE, new ModelEvent([
Expand Down
48 changes: 48 additions & 0 deletions src/services/Relations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use craft\db\Command;
use craft\db\Query;
use craft\db\Table;
use craft\fieldlayoutelements\CustomField;
use craft\fields\BaseRelationField;
use craft\helpers\Db;
use Throwable;
Expand Down Expand Up @@ -125,4 +126,51 @@ public function saveRelations(BaseRelationField $field, ElementInterface $source
}
}
}

/**
* Deletes relations that don’t belong to a relational field on the given element’s field layout.
*
* @param ElementInterface $element
* @since 4.8.0
*/
public function deleteLeftoverRelations(ElementInterface $element): void
{
if (!$element->id) {
return;
}

$fieldLayout = $element->getFieldLayout();
if (!$fieldLayout) {
return;
}

$relationFieldIds = [];
foreach ($fieldLayout->getTabs() as $tab) {
foreach ($tab->getElements() as $layoutElement) {
if ($layoutElement instanceof CustomField) {
$field = $layoutElement->getField();
if ($field instanceof BaseRelationField) {
$relationFieldIds[] = $field->id;
}
}
}
}

// get those relations for the element that don't belong to any relational fields that are in the layout
$query = (new Query())
->select(['id'])
->from(Table::RELATIONS)
->where(['sourceId' => $element->id]);

if (!empty($relationFieldIds)) {
$query->andWhere(['not', ['fieldId' => $relationFieldIds]]);
}

$leftoverRelationIds = $query->column();

// if relations were returned - delete them
if (!empty($leftoverRelationIds)) {
Db::delete(Table::RELATIONS, ['id' => $leftoverRelationIds]);
}
}
}
Loading