Skip to content

Commit 664c4db

Browse files
committed
Merge branch '5.x' into 6.x
2 parents f41cdd9 + 39e78ac commit 664c4db

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
## Unreleased
44

5+
- Added a `Process::EVENT_COMPARE_CONTENT` to give plugins a chance to manipulate feed data before any content comparison is done. ([#1623](https://github.com/craftcms/feed-me/pull/1623))
56
- Fixed an error that could occur when running a feed with nothing to process. ([#1611](https://github.com/craftcms/feed-me/pull/1611))
67
- Fixed a bug where duplicate users could get created when importing entry authors with a matching email address. ([#1612](https://github.com/craftcms/feed-me/pull/1612))
78
- Fixed a bug where importing into a Matrix field with “Use default value” selected, subfields could use the default value in certain scenarios. ([#1613](https://github.com/craftcms/feed-me/pull/1613))
89
- Fixed a bug that could occur on PostgreSQL when using the `setEmptyValues` feed setting. ([#1620](https://github.com/craftcms/feed-me/pull/1620))
910
- Fixed a bug that could occur when saving empty ("") values if `compareContent` and “Set Empty Values” were both enabled. ([#1621](https://github.com/craftcms/feed-me/pull/1621))
1011
- Fixed a bug that could occur when importing into a Matrix field with no blocks. ([#1622](https://github.com/craftcms/feed-me/pull/1622))
12+
- Fixed a regression that happened in 5.3.0 where imports into relational field would not remove existing relations if it was required. ([#1623](https://github.com/craftcms/feed-me/pull/1623))
1113

1214
## 6.8.0 - 2025-03-14
1315
-

src/events/CompareContentEvent.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace craft\feedme\events;
4+
5+
use craft\base\ElementInterface;
6+
use yii\base\Event;
7+
8+
/**
9+
* Compare content event class.
10+
*
11+
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
12+
* @since 5.12.0
13+
*/
14+
class CompareContentEvent extends Event
15+
{
16+
// Properties
17+
// =========================================================================
18+
19+
/**
20+
* @var array
21+
*/
22+
public array $content;
23+
24+
/**
25+
* @var ElementInterface
26+
*/
27+
public ElementInterface $element;
28+
29+
/**
30+
* @var string
31+
*/
32+
public string $handle;
33+
34+
/**
35+
* @var mixed
36+
*/
37+
public mixed $existingValue;
38+
39+
/**
40+
* @var mixed
41+
*/
42+
public mixed $newValue;
43+
}

src/helpers/DataHelper.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,7 @@ public static function compareElementContent($content, $element): ?bool
264264
foreach ($content as $key => $newValue) {
265265
$existingValue = Hash::get($fields, $key);
266266

267-
[$existingValue, $newValue] = self::prepDatesForComparison($existingValue, $newValue);
268-
269-
// If array key & values are already within the existing array
270-
if (is_array($newValue) && is_array($existingValue) && Hash::contains($existingValue,$newValue)) {
271-
unset($trackedChanges[$key]);
272-
continue;
273-
}
267+
[$existingValue, $newValue] = Plugin::$plugin->process->onCompareContent($content, $element, $key, $existingValue, $newValue);
274268

275269
// Check for simple fields first
276270
if (self::_compareSimpleValues($fields, $key, $existingValue, $newValue)) {

src/services/Process.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use Cake\Utility\Hash;
66
use Craft;
77
use craft\base\Component;
8+
use craft\base\ElementInterface as CraftElementInterface;
89
use craft\elements\User;
910
use craft\errors\ShellCommandException;
1011
use craft\feedme\base\ElementInterface;
12+
use craft\feedme\events\CompareContentEvent;
1113
use craft\feedme\events\FeedProcessEvent;
1214
use craft\feedme\helpers\DataHelper;
1315
use craft\feedme\helpers\DuplicateHelper;
@@ -31,6 +33,12 @@ class Process extends Component
3133
public const EVENT_STEP_AFTER_ELEMENT_SAVE = 'onStepElementSave';
3234
public const EVENT_AFTER_PROCESS_FEED = 'onAfterProcessFeed';
3335

36+
/**
37+
* @event CompareContentEvent The event that is triggered before content is compared to check for changes.
38+
* @since 5.12.0
39+
*/
40+
public const EVENT_COMPARE_CONTENT = 'onCompareContent';
41+
3442

3543
// Properties
3644
// =========================================================================
@@ -618,6 +626,40 @@ public function debugFeed($feed, $limit, $offset, $processedElementIds): void
618626
}
619627
}
620628

629+
/**
630+
* Prepare data for content comparison and allow plugins to do so via an event.
631+
*
632+
* @param array $content
633+
* @param CraftElementInterface $element
634+
* @param string $key
635+
* @param mixed $existingValue
636+
* @param mixed $newValue
637+
* @return mixed
638+
* @since 5.12.0
639+
*/
640+
public function onCompareContent(array $content, CraftElementInterface $element, string $key, mixed $existingValue, mixed $newValue): mixed
641+
{
642+
[$existingValue, $newValue] = DataHelper::prepDatesForComparison($existingValue, $newValue);
643+
644+
if ($this->hasEventHandlers(self::EVENT_COMPARE_CONTENT)) {
645+
$event = new CompareContentEvent([
646+
'content' => $content,
647+
'element' => $element,
648+
'handle' => $key,
649+
'existingValue' => $existingValue,
650+
'newValue' => $newValue,
651+
]);
652+
653+
$this->trigger(self::EVENT_COMPARE_CONTENT, $event);
654+
655+
// Allow event to overwrite existing and new value to be used for comparison
656+
return [$event->existingValue, $event->newValue];
657+
}
658+
659+
// Allow event to overwrite existing and new value to be used for comparison
660+
return [$existingValue, $newValue];
661+
}
662+
621663

622664

623665
// Private Methods

0 commit comments

Comments
 (0)