Skip to content

Commit

Permalink
Merge pull request #16255 from craftcms/bugfix/16245-link-field-norma…
Browse files Browse the repository at this point in the history
…lize-element

Bugfix/16245 link field normalize element
  • Loading branch information
brandonkelly authored Dec 3, 2024
2 parents a342d9a + d5174f5 commit c559f1a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Added the `affiliatedSite` and `affiliatedSiteId` user query and GraphQL params. ([#16174](https://github.com/craftcms/cms/pull/16174))
- Added the `affiliatedSiteHandle` and `affiliatedSiteId` user GraphQL field. ([#16174](https://github.com/craftcms/cms/pull/16174))
- It’s now possible to pass nested custom field value keys into element queries’ `orderBy` and `select` params (e.g. `myDateField.tz`). ([#16157](https://github.com/craftcms/cms/discussions/16157))
- It’s now possible to set Link field values to arrays with `value` keys set to element instances or IDs. ([#16255](https://github.com/craftcms/cms/pull/16255))

### Extensibility
- Added `craft\base\conditions\BaseElementSelectConditionRule::allowMultiple()`.
Expand Down
12 changes: 10 additions & 2 deletions src/fields/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,11 @@ public function normalizeValue(mixed $value, ?ElementInterface $element): mixed
'urlSuffix' => $this->showUrlSuffixField ? ($value['urlSuffix'] ?? null) : null,
'target' => $this->showTargetField ? ($value['target'] ?? null) : null,
]);
$value = trim($value['value'] ?? $value[$typeId]['value'] ?? '');
$value = $value['value'] ?? $value[$typeId]['value'] ?? '';

if (is_string($value)) {
$value = trim($value);
}

if (!$value) {
return null;
Expand All @@ -499,7 +503,11 @@ public function normalizeValue(mixed $value, ?ElementInterface $element): mixed
$linkType = Component::createComponent($type, BaseLinkType::class);
}

$value = $linkType->normalizeValue(str_replace(' ', '+', $value));
if (is_string($value)) {
$value = str_replace(' ', '+', $value);
}

$value = $linkType->normalizeValue($value);
} else {
if (!$value) {
return null;
Expand Down
37 changes: 37 additions & 0 deletions src/fields/linktypes/BaseElementLinkType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use craft\helpers\Html;
use craft\services\ElementSources;
use Illuminate\Support\Collection;
use yii\base\InvalidArgumentException;

/**
* Base element link type.
Expand Down Expand Up @@ -198,11 +199,47 @@ protected function selectionCriteria(): array
];
}

/**
* @inheritdoc
*/
public function validateValue(string $value, ?string &$error = null): bool
{
return true;
}

/**
* @inheritdoc
*/
public function normalizeValue(ElementInterface|int|string $value): string
{
if ($value instanceof ElementInterface) {
if (!is_a($value, static::elementType())) {
throw new InvalidArgumentException(sprintf('$value must be an %s instance, ID, or reference tag.', static::elementType()::lowerDisplayName()));
}
$value = sprintf('{%s:%s@%s:url}',
static::elementType()::refHandle(),
$value->id,
$value->siteId,
);
}
if (is_numeric($value)) {
$value = sprintf('{%s:%s@%s:url}',
static::elementType()::refHandle(),
$value,
Craft::$app->getSites()->getCurrentSite()->id,
);
}

return parent::normalizeValue($value);
}

/**
* Returns an Element that the field is supposed to link to.
*
* @param string|null $value
* @return ElementInterface|null
* @throws \craft\errors\SiteNotFoundException
*/
public function element(?string $value): ?ElementInterface
{
if (
Expand Down

0 comments on commit c559f1a

Please sign in to comment.