|
6 | 6 | use Craft;
|
7 | 7 | use craft\base\Element as BaseElement;
|
8 | 8 | use craft\elements\conditions\ElementConditionInterface;
|
| 9 | +use craft\elements\Entry; |
9 | 10 | use craft\elements\Entry as EntryElement;
|
10 | 11 | use craft\errors\ElementNotFoundException;
|
11 | 12 | use craft\feedme\base\Field;
|
12 | 13 | use craft\feedme\base\FieldInterface;
|
13 | 14 | use craft\feedme\helpers\DataHelper;
|
| 15 | +use craft\feedme\helpers\FieldHelper; |
| 16 | +use craft\feedme\models\FeedModel; |
14 | 17 | use craft\feedme\Plugin;
|
| 18 | +use craft\fields\BaseRelationField; |
15 | 19 | use craft\fields\Entries as EntriesField;
|
16 | 20 | use craft\helpers\Db;
|
17 | 21 | use craft\helpers\ElementHelper;
|
18 | 22 | use craft\helpers\Json;
|
19 | 23 | use craft\services\ElementSources;
|
| 24 | +use Illuminate\Support\Collection; |
20 | 25 | use Throwable;
|
21 | 26 | use yii\base\Exception;
|
22 | 27 |
|
@@ -240,6 +245,69 @@ public function parseField(): mixed
|
240 | 245 | return $foundElements;
|
241 | 246 | }
|
242 | 247 |
|
| 248 | + /** |
| 249 | + * Returns an array of custom fields that can be used when querying for matching entries. |
| 250 | + * |
| 251 | + * If a field is passed, use the field layouts linked to the sources allowed by the Entries field. |
| 252 | + * If all the sources are native (sections), then only fields from all those sections entry types field layouts will be returned. |
| 253 | + * If there's at least one custom source in the mix, the above list will be followed by a list of all the fields. |
| 254 | + * If only custom sources are selected, return all the fields in the installation. |
| 255 | + * |
| 256 | + * @param FeedModel $feed |
| 257 | + * @param BaseRelationField|null $field |
| 258 | + * @return array |
| 259 | + */ |
| 260 | + public static function getMatchFields(FeedModel $feed, ?BaseRelationField $field = null): array |
| 261 | + { |
| 262 | + // The field will be null e.g. when importing into a structure section and there's the option to select a parent |
| 263 | + // the parent is serviced by the entries field markup too, but it doesn't tie into a custom field per se; |
| 264 | + if ($field === null) { |
| 265 | + $entryType = Craft::$app->getEntries()->getEntryTypeById($feed->elementGroup[Entry::class]['entryType']); |
| 266 | + if (!$entryType) { |
| 267 | + return FieldHelper::getAllUniqueIdFields(); |
| 268 | + } |
| 269 | + |
| 270 | + $fieldLayout = Craft::$app->getFields()->getLayoutById($entryType->fieldLayoutId); |
| 271 | + if (!$fieldLayout) { |
| 272 | + return FieldHelper::getAllUniqueIdFields(); |
| 273 | + } |
| 274 | + |
| 275 | + return array_filter( |
| 276 | + $fieldLayout->getCustomFields(), |
| 277 | + fn($field) => FieldHelper::fieldCanBeUniqueId($field) |
| 278 | + ); |
| 279 | + } else { |
| 280 | + // if the Entries field has only custom sources - we have no choice but return all the field |
| 281 | + if (FieldHelper::fieldHasOnlyCustomSources($field)) { |
| 282 | + return FieldHelper::getAllUniqueIdFields(); |
| 283 | + } |
| 284 | + |
| 285 | + // deal with the native sources - sections |
| 286 | + $sections = FieldHelper::getEntrySourcesByField($field); |
| 287 | + $entryTypes = []; |
| 288 | + foreach ($sections as $section) { |
| 289 | + $entryTypes = [...$entryTypes, ...$section->getEntryTypes()]; |
| 290 | + } |
| 291 | + |
| 292 | + $allowedFields = []; |
| 293 | + $entryTypes = Collection::make($entryTypes)->keyBy('id'); |
| 294 | + |
| 295 | + foreach ($entryTypes as $entryType) { |
| 296 | + $fieldLayout = Craft::$app->getFields()->getLayoutById($entryType->fieldLayoutId); |
| 297 | + $allowedFields = [...$allowedFields, ...$fieldLayout->getCustomFields()]; |
| 298 | + } |
| 299 | + |
| 300 | + // if there's a custom source in the mix, we should add all the fields too |
| 301 | + $customSources = array_filter($field['sources'], (fn(string $source) => str_starts_with($source, 'custom:'))); |
| 302 | + |
| 303 | + if (!empty($customSources)) { |
| 304 | + $allowedFields = [...$allowedFields, ...Craft::$app->getFields()->getAllFields()]; |
| 305 | + } |
| 306 | + |
| 307 | + return array_filter($allowedFields, fn($field) => FieldHelper::fieldCanBeUniqueId($field)); |
| 308 | + } |
| 309 | + } |
| 310 | + |
243 | 311 |
|
244 | 312 | // Private Methods
|
245 | 313 | // =========================================================================
|
|
0 commit comments