Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 008b382

Browse files
committedApr 21, 2023
Map category/tag/globalset reference tags to entries post-entrification
Resolves #13082
1 parent 3103f11 commit 008b382

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Category/tag/global set reference tags now map to entries, if no category groups/tag groups/global sets exist. ([#13082](https://github.com/craftcms/cms/issues/13082))
56
- `craft\services\Elements::propagateElements()` now returns the element in the target site.
67
- Fixed a bug where it was possible to select a disallowed volume as the Default Asset Location in Assets field settings. ([#13072](https://github.com/craftcms/cms/issues/13072))
78
- Fixed a bug where it was possible to upload files to Assets fields outside of the allowed volumes, if the Default Upload Location was set to a disallowed volume. ([#13072](https://github.com/craftcms/cms/issues/13072))

‎src/elements/Category.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
*/
4444
class Category extends Element
4545
{
46+
/**
47+
* @since 4.4.8
48+
*/
49+
public const REF_HANDLE = 'category';
50+
4651
/**
4752
* @inheritdoc
4853
*/
@@ -80,7 +85,7 @@ public static function pluralLowerDisplayName(): string
8085
*/
8186
public static function refHandle(): ?string
8287
{
83-
return 'category';
88+
return self::REF_HANDLE;
8489
}
8590

8691
/**

‎src/elements/GlobalSet.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
*/
2828
class GlobalSet extends Element
2929
{
30-
// Validation scenarios
31-
// -------------------------------------------------------------------------
30+
/**
31+
* @since 4.4.8
32+
*/
33+
public const REF_HANDLE = 'globalset';
3234

3335
/**
3436
* @since 4.4.6
@@ -72,7 +74,7 @@ public static function pluralLowerDisplayName(): string
7274
*/
7375
public static function refHandle(): ?string
7476
{
75-
return 'globalset';
77+
return self::REF_HANDLE;
7678
}
7779

7880
/**

‎src/elements/Tag.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
*/
3030
class Tag extends Element
3131
{
32+
/**
33+
* @since 4.4.8
34+
*/
35+
public const REF_HANDLE = 'tag';
36+
3237
/**
3338
* @inheritdoc
3439
*/
@@ -66,7 +71,7 @@ public static function pluralLowerDisplayName(): string
6671
*/
6772
public static function refHandle(): ?string
6873
{
69-
return 'tag';
74+
return self::REF_HANDLE;
7075
}
7176

7277
/**

‎src/services/Elements.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -2338,13 +2338,29 @@ public function createExporter(mixed $config): ElementExporterInterface
23382338
public function getElementTypeByRefHandle(string $refHandle): ?string
23392339
{
23402340
if (!isset($this->_elementTypesByRefHandle[$refHandle])) {
2341-
$this->_elementTypesByRefHandle[$refHandle] = $this->elementTypeByRefHandle($refHandle);
2341+
$class = $this->elementTypeByRefHandle($refHandle);
2342+
2343+
// Special cases for categories/tags/globals, if they’ve been entrified
2344+
if (
2345+
($class === Category::class && empty(Craft::$app->getCategories()->getAllGroups())) ||
2346+
($class === Tag::class && empty(Craft::$app->getTags()->getAllTagGroups())) ||
2347+
($class === GlobalSet::class && empty(Craft::$app->getGlobals()->getAllSets()))
2348+
) {
2349+
$class = Entry::class;
2350+
}
2351+
2352+
$this->_elementTypesByRefHandle[$refHandle] = $class;
23422353
}
2354+
23432355
return $this->_elementTypesByRefHandle[$refHandle] ?: null;
23442356
}
23452357

23462358
private function elementTypeByRefHandle(string $refHandle): string|false
23472359
{
2360+
if (is_subclass_of($refHandle, ElementInterface::class)) {
2361+
return $refHandle;
2362+
}
2363+
23482364
foreach ($this->getAllElementTypes() as $class) {
23492365
/** @var string|ElementInterface $class */
23502366
/** @phpstan-var class-string<ElementInterface>|ElementInterface $class */
@@ -2389,11 +2405,11 @@ function(array $matches) use (
23892405
$fallback = $fullMatch;
23902406
}
23912407

2392-
// Does it already have a full element type class name?
2393-
if (
2394-
!is_subclass_of($elementType, ElementInterface::class) &&
2395-
($elementType = $this->getElementTypeByRefHandle($elementType)) === null
2396-
) {
2408+
// Swap out the ref handle for the element type
2409+
$elementType = $this->getElementTypeByRefHandle($elementType);
2410+
2411+
// Use the fallback if we couldn't find an element type
2412+
if ($elementType === null) {
23972413
return $fallback;
23982414
}
23992415

0 commit comments

Comments
 (0)
Please sign in to comment.