Skip to content

Commit

Permalink
Merge pull request #16324 from craftcms/feature/cms-1356-displaying-e…
Browse files Browse the repository at this point in the history
…lement-data-in-various-view-modes

Feature/cms 1356 displaying element data in various view modes
  • Loading branch information
brandonkelly authored Dec 14, 2024
2 parents b5683e8 + bb7d0a4 commit e2bb579
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Content Management
- “Related To”, “Not Related To”, “Author”, and relational field condition rules now allow multiple elements to be specified. ([#16121](https://github.com/craftcms/cms/discussions/16121))
- Improved the styling of inline code fragments. ([#16141](https://github.com/craftcms/cms/pull/16141))
- Improved the styling of attribute previews in card view. ([#16324](https://github.com/craftcms/cms/pull/16324))
- Added the “Affiliated Site” user condition rule. ([#16174](https://github.com/craftcms/cms/pull/16174))
- The global sidebar no longer shows “Failed” for queue jobs, for users that don’t have access to the Queue Manager. ([#16184](https://github.com/craftcms/cms/issues/16184))
- Addresses and Matrix fields now show provisional drafts when previewing an owner element. ([#16295](https://github.com/craftcms/cms/issues/16295))
Expand Down Expand Up @@ -52,6 +53,7 @@
### Extensibility
- Added `craft\base\Element::EVENT_DEFINE_ALT_ACTIONS`. ([#16294](https://github.com/craftcms/cms/pull/16294))
- Added `craft\base\ElementInterface::getAltActions()`. ([#16294](https://github.com/craftcms/cms/pull/16294))
- Added `craft\base\ElementTrait::$viewMode`. ([#16324](https://github.com/craftcms/cms/pull/16324))
- Added `craft\base\conditions\BaseElementSelectConditionRule::allowMultiple()`.
- Added `craft\base\conditions\BaseElementSelectConditionRule::getElementIds()`.
- Added `craft\base\conditions\BaseElementSelectConditionRule::setElementIds()`.
Expand Down
11 changes: 10 additions & 1 deletion src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,10 @@ public static function indexHtml(
}
}

foreach ($elements as $element) {
$element->viewMode = $viewState['mode'];
}

$variables['elements'] = $elements;
$template = '_elements/' . $viewState['mode'] . 'view/' . ($includeContainer ? 'container' : 'elements');

Expand Down Expand Up @@ -2617,6 +2621,7 @@ public function attributes(): array
$names['searchScore'],
$names['updateSearchIndexForOwner'],
$names['updatingFromDerivative'],
$names['viewMode'],
);

$names['canonicalId'] = true;
Expand Down Expand Up @@ -3472,6 +3477,8 @@ public function showStatusIndicator(): bool
*/
public function getCardBodyHtml(): ?string
{
$this->viewMode = 'cards';

$previews = array_filter(array_map(
function(BaseField|array $item) {
if ($item instanceof BaseField) {
Expand Down Expand Up @@ -5506,7 +5513,9 @@ public function getAttributeHtml(string $attribute): string
{
// Fire a 'defineAttributeHtml' event
if ($this->hasEventHandlers(self::EVENT_DEFINE_ATTRIBUTE_HTML)) {
$event = new DefineAttributeHtmlEvent(['attribute' => $attribute]);
$event = new DefineAttributeHtmlEvent([
'attribute' => $attribute,
]);
$this->trigger(self::EVENT_DEFINE_ATTRIBUTE_HTML, $event);
if (isset($event->html)) {
return $event->html;
Expand Down
6 changes: 6 additions & 0 deletions src/base/ElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ trait ElementTrait
*/
public bool $previewing = false;

/**
* @var string|null The view mode used to show this element (e.g. `structure`, `table`, `thumbs`, `cards`).
* @since 5.6.0
*/
public ?string $viewMode = null;

/**
* @var bool Whether the element should definitely be saved, if it’s a nested element being considered
* for saving by [[NestedElementManager]].
Expand Down
1 change: 1 addition & 0 deletions src/base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ protected function defineRules(): array
'uri',
'url',
'username', // user-specific
'viewMode',
],
];

Expand Down
6 changes: 5 additions & 1 deletion src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,11 @@ protected function attributeHtml(string $attribute): string
return $section ? Html::encode(Craft::t('site', $section->name)) : '';
case 'type':
try {
return Cp::chipHtml($this->getType());
$config = [];
if ($this->viewMode === 'cards') {
$config['showThumb'] = false;
}
return Cp::chipHtml($this->getType(), $config);
} catch (InvalidConfigException) {
return Craft::t('app', 'Unknown');
}
Expand Down
9 changes: 9 additions & 0 deletions src/fields/Lightswitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use craft\base\MergeableFieldInterface;
use craft\base\SortableFieldInterface;
use craft\elements\Entry;
use craft\enums\Color as ColorEnum;
use craft\fields\conditions\LightswitchFieldConditionRule;
use craft\helpers\ArrayHelper;
use craft\helpers\Cp;
Expand Down Expand Up @@ -235,6 +236,14 @@ public function getContentGqlQueryArgumentType(): Type|array
*/
public function getPreviewHtml(mixed $value, ElementInterface $element): string
{
if ($element->viewMode === 'cards') {
return Cp::statusLabelHtml([
'color' => $value ? ColorEnum::Teal : ColorEnum::Gray,
'label' => $this->getUiLabel(),
'icon' => $value ? 'check' : 'xmark',
]);
}

if (!$value) {
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/css/cp.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/css/cp.css.map

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions src/web/assets/cp/src/css/_main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3725,14 +3725,6 @@ table {
& > .cp-icon {
margin-inline: -2px -4px;
margin-block: 3px 0;

// hide the entry type icon if we're alreayd showing one for the card
+ .card-content
.card-body
.chip[data-type='craft\\\\models\\\\EntryType']
.cp-icon {
display: none;
}
}

.card-content,
Expand Down Expand Up @@ -3775,13 +3767,6 @@ table {
text-wrap: pretty;
}

.checkbox-preview-label {
display: inline;

font-style: italic;
margin-inline-start: 5px;
}

.chip.small {
padding-inline: 0;
}
Expand Down

0 comments on commit e2bb579

Please sign in to comment.