Skip to content

Commit

Permalink
Map category/tag/globalset reference tags to entries post-entrification
Browse files Browse the repository at this point in the history
Resolves #13082
  • Loading branch information
brandonkelly committed Apr 21, 2023
1 parent 3103f11 commit 008b382
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- 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))
- `craft\services\Elements::propagateElements()` now returns the element in the target site.
- 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))
- 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))
Expand Down
7 changes: 6 additions & 1 deletion src/elements/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
*/
class Category extends Element
{
/**
* @since 4.4.8
*/
public const REF_HANDLE = 'category';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -80,7 +85,7 @@ public static function pluralLowerDisplayName(): string
*/
public static function refHandle(): ?string
{
return 'category';
return self::REF_HANDLE;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/elements/GlobalSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
*/
class GlobalSet extends Element
{
// Validation scenarios
// -------------------------------------------------------------------------
/**
* @since 4.4.8
*/
public const REF_HANDLE = 'globalset';

/**
* @since 4.4.6
Expand Down Expand Up @@ -72,7 +74,7 @@ public static function pluralLowerDisplayName(): string
*/
public static function refHandle(): ?string
{
return 'globalset';
return self::REF_HANDLE;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/elements/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
*/
class Tag extends Element
{
/**
* @since 4.4.8
*/
public const REF_HANDLE = 'tag';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -66,7 +71,7 @@ public static function pluralLowerDisplayName(): string
*/
public static function refHandle(): ?string
{
return 'tag';
return self::REF_HANDLE;
}

/**
Expand Down
28 changes: 22 additions & 6 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -2338,13 +2338,29 @@ public function createExporter(mixed $config): ElementExporterInterface
public function getElementTypeByRefHandle(string $refHandle): ?string
{
if (!isset($this->_elementTypesByRefHandle[$refHandle])) {
$this->_elementTypesByRefHandle[$refHandle] = $this->elementTypeByRefHandle($refHandle);
$class = $this->elementTypeByRefHandle($refHandle);

// Special cases for categories/tags/globals, if they’ve been entrified
if (
($class === Category::class && empty(Craft::$app->getCategories()->getAllGroups())) ||
($class === Tag::class && empty(Craft::$app->getTags()->getAllTagGroups())) ||
($class === GlobalSet::class && empty(Craft::$app->getGlobals()->getAllSets()))
) {
$class = Entry::class;
}

$this->_elementTypesByRefHandle[$refHandle] = $class;
}

return $this->_elementTypesByRefHandle[$refHandle] ?: null;
}

private function elementTypeByRefHandle(string $refHandle): string|false
{
if (is_subclass_of($refHandle, ElementInterface::class)) {
return $refHandle;
}

foreach ($this->getAllElementTypes() as $class) {
/** @var string|ElementInterface $class */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $class */
Expand Down Expand Up @@ -2389,11 +2405,11 @@ function(array $matches) use (
$fallback = $fullMatch;
}

// Does it already have a full element type class name?
if (
!is_subclass_of($elementType, ElementInterface::class) &&
($elementType = $this->getElementTypeByRefHandle($elementType)) === null
) {
// Swap out the ref handle for the element type
$elementType = $this->getElementTypeByRefHandle($elementType);

// Use the fallback if we couldn't find an element type
if ($elementType === null) {
return $fallback;
}

Expand Down

0 comments on commit 008b382

Please sign in to comment.