Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default card view attributes #16083

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@

### Extensibility
- Added `craft\base\Element::EVENT_REGISTER_CARD_ATTRIBUTES`.
- Added `craft\base\Element::EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES`.
- Added `craft\base\Element::defineCardAttributes()`.
- Added `craft\base\Element::defineDefaultCardAttributes()`.
- Added `craft\base\ElementInterface::attributePreviewHtml()`.
- Added `craft\base\ElementInterface::cardAttributes()`.
- Added `craft\base\ElementInterface::defaultCardAttributes()`.
- Added `craft\base\ElementInterface::indexViewModes()`.
- Added `craft\base\NestedElementTrait::saveOwnership()`. ([#15894](https://github.com/craftcms/cms/pull/15894))
- Added `craft\base\PreviewableFieldInterface::previewPlaceholderHtml()`.
Expand All @@ -73,6 +76,7 @@
- Added `craft\events\ApplyFieldSaveEvent`. ([#15872](https://github.com/craftcms/cms/discussions/15872))
- Added `craft\events\DefineAddressCountriesEvent`. ([#15711](https://github.com/craftcms/cms/pull/15711))
- Added `craft\events\RegisterElementCardAttributesEvent`.
- Added `craft\events\RegisterElementDefaultCardAttributesEvent`.
- Added `craft\fieldlayoutelements\Template::$templateMode`. ([#15932](https://github.com/craftcms/cms/pull/15932))
- Added `craft\fields\data\LinkData::$target`.
- Added `craft\fields\data\LinkData::setLabel()`.
Expand Down
39 changes: 39 additions & 0 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use craft\events\ModelEvent;
use craft\events\RegisterElementActionsEvent;
use craft\events\RegisterElementCardAttributesEvent;
use craft\events\RegisterElementDefaultCardAttributesEvent;
use craft\events\RegisterElementDefaultTableAttributesEvent;
use craft\events\RegisterElementExportersEvent;
use craft\events\RegisterElementFieldLayoutsEvent;
Expand Down Expand Up @@ -285,6 +286,12 @@ abstract class Element extends Component implements ElementInterface
*/
public const EVENT_REGISTER_CARD_ATTRIBUTES = 'registerCardAttributes';

/**
* @event RegisterElementCardAttributesEvent The event that is triggered when registering the card attributes for the element type.
* @since 5.5.0
*/
public const EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES = 'registerDefaultCardAttributes';

/**
* @event DefineEagerLoadingMapEvent The event that is triggered when defining an eager-loading map.
*
Expand Down Expand Up @@ -1594,6 +1601,38 @@ public static function attributePreviewHtml(array $attribute): mixed
};
}

/**
* @inheritdoc
*/
public static function defaultCardAttributes(): array
{
$cardAttributes = static::defineDefaultCardAttributes();

// Fire a 'registerDefaultCardAttributes' event
if (Event::hasHandlers(static::class, self::EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES)) {
$event = new RegisterElementDefaultCardAttributesEvent([
'cardAttributes' => $cardAttributes,
]);
Event::trigger(static::class, self::EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES, $event);
return $event->cardAttributes;
}

return $cardAttributes;
}

/**
* Returns the list of card attribute keys that should be shown by default.
*
* @return string[] The card attributes.
* @see defaultCardAttributes()
* @see cardAttributes()
* @since 5.5.0
*/
protected static function defineDefaultCardAttributes(): array
{
return [];
}

// Methods for customizing element queries
// -------------------------------------------------------------------------

Expand Down
11 changes: 11 additions & 0 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,17 @@ public static function defaultTableAttributes(string $source): array;
*/
public static function cardAttributes(): array;

/**
* Returns the list of card attribute keys that should be shown by default, if the field layout hasn't been customised.
*
* This method should return an array where each element in the array maps to one of the keys of the array returned
* by [[cardAttributes()]].
*
* @return string[] The card attribute keys
* @since 5.5.0
*/
public static function defaultCardAttributes(): array;

/**
* Return HTML for the attribute in the card preview.
*
Expand Down
24 changes: 24 additions & 0 deletions src/events/RegisterElementDefaultCardAttributesEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\events;

use yii\base\Event;

/**
* RegisterElementDefaultCardAttributesEvent class.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 5.5.0
*/
class RegisterElementDefaultCardAttributesEvent extends Event
{
/**
* @var string[] List of registered default card attributes for the element type.
*/
public array $cardAttributes = [];
}
6 changes: 5 additions & 1 deletion src/models/FieldLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ public function init(): void
}

if (!isset($this->_cardView)) {
$this->setCardView([]);
if (!$this->type) {
$this->setCardView([]);
} else {
$this->setCardView($this->type::defaultCardAttributes());
}
}
}

Expand Down