Skip to content

Commit

Permalink
Placeholder callables
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Dec 14, 2024
1 parent 7112349 commit 9bfb2c4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
- `craft\helpers\Cp::elementIndexHtml()` now supports passing `defaultSort` in the `$config` array, when `sources` is `null`. ([#16236](https://github.com/craftcms/cms/discussions/16236))
- `craft\models\Site` now implements `craft\base\Chippable`.
- `craft\services\Revisions::createRevision()` no longer creates the revision if an `EVENT_BEFORE_CREATE_REVISION` event handler sets `$event->handled` to `true` and at least one revision already exists for the element. ([#16260](https://github.com/craftcms/cms/discussions/16260))
- Elements’ `defineCardAttributes()` methods can now return a `placeholder` value set to a callable.
- Deprecated `craft\fields\Color::$presets`. ([#16249](https://github.com/craftcms/cms/pull/16249))
- Deprecated `craft\fields\Link::$showTargetField`.
- `_includes/forms/autosuggest.twig` now supports a `suggestTemplates` variable.
Expand Down
19 changes: 11 additions & 8 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -1571,19 +1571,19 @@ protected static function defineCardAttributes(): array
$attributes = [
'dateCreated' => [
'label' => Craft::t('app', 'Date Created'),
'placeholder' => (new \DateTime())->sub(new \DateInterval('P16D')),
'placeholder' => fn() => (new \DateTime())->sub(new \DateInterval('P16D')),
],
'dateUpdated' => [
'label' => Craft::t('app', 'Date Updated'),
'placeholder' => (new \DateTime())->sub(new \DateInterval('P15D')),
'placeholder' => fn() => (new \DateTime())->sub(new \DateInterval('P15D')),
],
'id' => [
'label' => Craft::t('app', 'ID'),
'placeholder' => 4321,
'placeholder' => fn() => 4321,
],
'uid' => [
'label' => Craft::t('app', 'UID'),
'placeholder' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'placeholder' => fn() => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
],
];

Expand All @@ -1592,15 +1592,15 @@ protected static function defineCardAttributes(): array
'link' => [
'label' => Craft::t('app', 'Link'),
'icon' => 'world',
'placeholder' => ElementHelper::linkAttributeHtml('#'),
'placeholder' => fn() => ElementHelper::linkAttributeHtml('#'),
],
'slug' => [
'label' => Craft::t('app', 'Slug'),
'placeholder' => Craft::t('app', 'Slug'),
'placeholder' => fn() => Craft::t('app', 'Slug'),
],
'uri' => [
'label' => Craft::t('app', 'URI'),
'placeholder' => ElementHelper::uriAttributeHtml(Craft::t('app', 'link/to/something'), '#'),
'placeholder' => fn() => ElementHelper::uriAttributeHtml(Craft::t('app', 'link/to/something'), '#'),
],
]);
}
Expand All @@ -1615,7 +1615,10 @@ public static function attributePreviewHtml(array $attribute): mixed
{
return match ($attribute['value']) {
'link', 'uri' => $attribute['placeholder'],
default => ElementHelper::attributeHtml($attribute['placeholder'] ?? $attribute['label']),
default => ElementHelper::attributeHtml(is_callable($attribute['placeholder'] ?? null)
? $attribute['placeholder']()
: $attribute['placeholder'] ?? $attribute['label']
),
};
}

Expand Down
20 changes: 10 additions & 10 deletions src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,45 +665,45 @@ protected static function defineCardAttributes(): array
],
'filename' => [
'label' => Craft::t('app', 'Filename'),
'placeholder' => Craft::t('app', 'placeholder') . '.png',
'placeholder' => fn() => Craft::t('app', 'placeholder') . '.png',
],
'size' => [
'label' => Craft::t('app', 'File Size'),
'placeholder' => '2KB',
'placeholder' => fn() => '2KB',
],
'kind' => [
'label' => Craft::t('app', 'File Kind'),
'placeholder' => Craft::t('app', 'Image'),
'placeholder' => fn() => Craft::t('app', 'Image'),

],
'imageSize' => [
'label' => Craft::t('app', 'Dimensions'),
'placeholder' => '700x500',
'placeholder' => fn() => '700x500',
],
'width' => [
'label' => Craft::t('app', 'Image Width'),
'placeholder' => '700px',
'placeholder' => fn() => '700px',
],
'height' => [
'label' => Craft::t('app', 'Image Height'),
'placeholder' => '500px',
'placeholder' => fn() => '500px',
],
'location' => [
'label' => Craft::t('app', 'Location'),
'placeholder' => Craft::t('app', 'Volume'),
'placeholder' => fn() => Craft::t('app', 'Volume'),
],
'link' => [
'label' => Craft::t('app', 'Link'),
'icon' => 'world',
'placeholder' => ElementHelper::linkAttributeHtml(null),
'placeholder' => fn() => ElementHelper::linkAttributeHtml(null),
],
'dateModified' => [
'label' => Craft::t('app', 'File Modified Date'),
'placeholder' => (new \DateTime())->sub(new \DateInterval('P14D')),
'placeholder' => fn() => (new \DateTime())->sub(new \DateInterval('P14D')),
],
'uploader' => [
'label' => Craft::t('app', 'Uploaded By'),
'placeholder' => ($uploader = Craft::$app->getUser()->getIdentity()) ? Cp::elementChipHtml($uploader) : '',
'placeholder' => fn() => ($uploader = Craft::$app->getUser()->getIdentity()) ? Cp::elementChipHtml($uploader) : '',
],
]);

Expand Down
2 changes: 1 addition & 1 deletion src/elements/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected static function defineCardAttributes(): array
return array_merge(parent::defineCardAttributes(), [
'parent' => [
'label' => Craft::t('app', 'Parent'),
'placeholder' => Html::tag(
'placeholder' => fn() => Html::tag(
'span',
Craft::t('app', 'Parent {type} Title', ['type' => self::displayName()]),
['class' => 'card-placeholder'],
Expand Down
18 changes: 9 additions & 9 deletions src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,43 +637,43 @@ protected static function defineCardAttributes(): array
$attributes = array_merge(parent::defineCardAttributes(), [
'section' => [
'label' => Craft::t('app', 'Section'),
'placeholder' => Craft::t('app', 'Section'),
'placeholder' => fn() => Craft::t('app', 'Section'),
],
'type' => [
'label' => Craft::t('app', 'Entry Type'),
'placeholder' => Craft::t('app', 'Entry Type'),
'placeholder' => fn() => Craft::t('app', 'Entry Type'),
],
'authors' => [
'label' => Craft::t('app', 'Authors'),
'placeholder' => $currentUser ? Cp::elementChipHtml($currentUser) : '',
'placeholder' => fn() => $currentUser ? Cp::elementChipHtml($currentUser) : '',
],
'parent' => [
'label' => Craft::t('app', 'Parent'),
'placeholder' => Html::tag(
'placeholder' => fn() => Html::tag(
'span',
Craft::t('app', 'Parent {type} Title', ['type' => self::displayName()]),
['class' => 'card-placeholder'],
),
],
'postDate' => [
'label' => Craft::t('app', 'Post Date'),
'placeholder' => (new \DateTime())->sub(new \DateInterval('P15D')),
'placeholder' => fn() => (new \DateTime())->sub(new \DateInterval('P15D')),
],
'expiryDate' => [
'label' => Craft::t('app', 'Expiry Date'),
'placeholder' => (new \DateTime())->add(new \DateInterval('P15D')),
'placeholder' => fn() => (new \DateTime())->add(new \DateInterval('P15D')),
],
'revisionNotes' => [
'label' => Craft::t('app', 'Revision Notes'),
'placeholder' => Craft::t('app', 'Revision Notes'),
'placeholder' => fn() => Craft::t('app', 'Revision Notes'),
],
'revisionCreator' => [
'label' => Craft::t('app', 'Last Edited By'),
'placeholder' => $currentUser ? Cp::elementChipHtml($currentUser) : '',
'placeholder' => fn() => $currentUser ? Cp::elementChipHtml($currentUser) : '',
],
'drafts' => [
'label' => Craft::t('app', 'Drafts'),
'placeholder' => Html::tag(
'placeholder' => fn() => Html::tag(
'span',
Craft::t('app', 'Draft {num}', ['num' => 1]),
['class' => 'card-placeholder'],
Expand Down
20 changes: 10 additions & 10 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,47 +511,47 @@ protected static function defineCardAttributes(): array
return array_merge(parent::defineCardAttributes(), [
'email' => [
'label' => Craft::t('app', 'Email'),
'placeholder' => '[email protected]',
'placeholder' => fn() => '[email protected]',
],
'username' => [
'label' => Craft::t('app', 'Username'),
'placeholder' => Craft::t('app', 'Username'),
'placeholder' => fn() => Craft::t('app', 'Username'),
],
'firstName' => [
'label' => Craft::t('app', 'First Name'),
'placeholder' => Craft::t('app', 'First Name'),
'placeholder' => fn() => Craft::t('app', 'First Name'),
],
'lastName' => [
'label' => Craft::t('app', 'Last Name'),
'placeholder' => Craft::t('app', 'Last Name'),
'placeholder' => fn() => Craft::t('app', 'Last Name'),
],
'groups' => [
'label' => Craft::t('app', 'Groups'),
'placeholder' => Craft::t('app', 'Group Name'),
'placeholder' => fn() => Craft::t('app', 'Group Name'),
],
'affiliatedSite' => [
'label' => Craft::t('app', 'Affiliated Site'),
'placeholder' => Craft::t('app', 'Site Name'),
'placeholder' => fn() => Craft::t('app', 'Site Name'),
],
'preferredLanguage' => [
'label' => Craft::t('app', 'Preferred Language'),
'placeholder' => $i18n->getLocaleById('en')->getDisplayName(Craft::$app->language),
'placeholder' => fn() => $i18n->getLocaleById('en')->getDisplayName(Craft::$app->language),
],
'preferredLocale' => [
'label' => Craft::t('app', 'Preferred Locale'),
'placeholder' => $i18n->getLocaleById('en-US')->getDisplayName(Craft::$app->language),
'placeholder' => fn() => $i18n->getLocaleById('en-US')->getDisplayName(Craft::$app->language),
],
'isCredentialed' => [
'label' => Craft::t('app', 'Credentialed'),
'placeholder' => Template::raw(Cp::statusLabelHtml([
'placeholder' => fn() => Template::raw(Cp::statusLabelHtml([
'color' => Color::Teal,
'label' => Craft::t('app', 'Credentialed'),
'icon' => 'check',
])),
],
'lastLoginDate' => [
'label' => Craft::t('app', 'Last Login'),
'placeholder' => (new \DateTime())->sub(new \DateInterval('P14D')),
'placeholder' => fn() => (new \DateTime())->sub(new \DateInterval('P14D')),
],
]);
}
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2651,7 +2651,11 @@ public static function cardPreviewHtml(FieldLayout $fieldLayout, array $cardElem
} elseif ($cardElement instanceof BaseField) {
$previewHtml .= Html::tag('div', $cardElement->previewPlaceholderHtml(null, null));
} else {
$previewHtml .= Html::tag('div', $elementType::attributePreviewHtml($cardElement));
$html = $elementType::attributePreviewHtml($cardElement);
if (is_callable($html)) {
$html = $html();
}
$previewHtml .= Html::tag('div', $html);
}
}

Expand Down

0 comments on commit 9bfb2c4

Please sign in to comment.