Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,9 @@ public function getCpEditUrl(): ?string
/**
* @inheritdoc
*/
public function getActionMenuItems(): array
public function getActionMenuItems(?ElementInterface $element = null, bool $static = false): array
{
$items = $this->actionMenuItems();
$items = $this->actionMenuItems($element, $static);

// Fire a 'defineActionMenuItems' event
if ($this->hasEventHandlers(self::EVENT_DEFINE_ACTION_MENU_ITEMS)) {
Expand All @@ -558,7 +558,7 @@ public function getActionMenuItems(): array
return $items;
}

protected function actionMenuItems(): array
protected function actionMenuItems(?ElementInterface $element = null, bool $static = false): array
{
$items = [];
$userSessionService = Craft::$app->getUser();
Expand Down
2 changes: 1 addition & 1 deletion src/fieldlayoutelements/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ protected function actionMenuItems(?ElementInterface $element = null, bool $stat
{
if ($this->_field instanceof Actionable) {
$this->_field->static = $static;
$items = $this->_field->getActionMenuItems();
$items = $this->_field->getActionMenuItems($element, $static);
} else {
$items = [];
}
Expand Down
109 changes: 56 additions & 53 deletions src/fields/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,30 +819,32 @@ public function getIsTranslatable(?ElementInterface $element): bool
/**
* @inheritdoc
*/
protected function actionMenuItems(): array
protected function actionMenuItems(?ElementInterface $element = null, bool $static = false): array
{
$items = [];
$view = Craft::$app->getView();

if ($this->viewMode === self::VIEW_MODE_BLOCKS) {
// Expand/Collapse all
$expandAllId = sprintf('expand-all-%s', mt_rand());
$collapseAllId = sprintf('collapse-all-%s', mt_rand());
$items[] = [
'id' => $expandAllId,
'icon' => 'expand',
'label' => StringHelper::upperCaseFirst(Craft::t('app', 'Expand all blocks', [
'type' => Entry::pluralLowerDisplayName(),
])),
];
$items[] = [
'id' => $collapseAllId,
'icon' => 'collapse',
'label' => StringHelper::upperCaseFirst(Craft::t('app', 'Collapse all blocks', [
'type' => Entry::pluralLowerDisplayName(),
])),
];
$view->registerJsWithVars(fn($expandAllId, $collapseAllId, $fieldId) => <<<JS
// actions that should only be available if we have an element
if ($element) {
if ($this->viewMode === self::VIEW_MODE_BLOCKS) {
// Expand/Collapse all
$expandAllId = sprintf('expand-all-%s', mt_rand());
$collapseAllId = sprintf('collapse-all-%s', mt_rand());
$items[] = [
'id' => $expandAllId,
'icon' => 'expand',
'label' => StringHelper::upperCaseFirst(Craft::t('app', 'Expand all blocks', [
'type' => Entry::pluralLowerDisplayName(),
])),
];
$items[] = [
'id' => $collapseAllId,
'icon' => 'collapse',
'label' => StringHelper::upperCaseFirst(Craft::t('app', 'Collapse all blocks', [
'type' => Entry::pluralLowerDisplayName(),
])),
];
$view->registerJsWithVars(fn($expandAllId, $collapseAllId, $fieldId) => <<<JS
(() => {
const expandAllBtn = $('#' + $expandAllId);
const collapseAllBtn = $('#' + $collapseAllId);
Expand Down Expand Up @@ -870,40 +872,40 @@ protected function actionMenuItems(): array
}, 1);
})();
JS, [
$view->namespaceInputId($expandAllId),
$view->namespaceInputId($collapseAllId),
$view->namespaceInputId($this->getInputId()),
]);
}

// Copy all
if ($this->maxEntries !== 1 && $this->viewMode !== self::VIEW_MODE_INDEX) {
if (!empty($items)) {
$items[] = ['type' => 'hr'];
$view->namespaceInputId($expandAllId),
$view->namespaceInputId($collapseAllId),
$view->namespaceInputId($this->getInputId()),
]);
}
$copyAllId = sprintf('action-copy-all-%s', mt_rand());
$items[] = [
'id' => $copyAllId,
'icon' => 'clone-dashed',
'color' => \craft\enums\Color::Fuchsia,
'label' => StringHelper::upperCaseFirst(Craft::t('app', 'Copy all {type}', [
'type' => Entry::pluralLowerDisplayName(),
])),
];

if ($this->viewMode === self::VIEW_MODE_CARDS) {
$view->registerJsWithVars(fn($copyAllId, $fieldId) => <<<JS
// Copy all
if ($this->maxEntries !== 1 && $this->viewMode !== self::VIEW_MODE_INDEX) {
if (!empty($items)) {
$items[] = ['type' => 'hr'];
}
$copyAllId = sprintf('action-copy-all-%s', mt_rand());
$items[] = [
'id' => $copyAllId,
'icon' => 'clone-dashed',
'color' => \craft\enums\Color::Fuchsia,
'label' => StringHelper::upperCaseFirst(Craft::t('app', 'Copy all {type}', [
'type' => Entry::pluralLowerDisplayName(),
])),
];

if ($this->viewMode === self::VIEW_MODE_CARDS) {
$view->registerJsWithVars(fn($copyAllId, $fieldId) => <<<JS
(() => {
$('#' + $copyAllId).on('activate', () => {
Craft.cp.copyElements($('#' + $fieldId + ' > .nested-element-cards > .elements > li > .element'));
});
})();
JS, [
$view->namespaceInputId($copyAllId),
$view->namespaceInputId($this->getInputId()),
]);
} else {
$view->registerJsWithVars(fn($copyAllId, $fieldId, $baseInfo) => <<<JS
$view->namespaceInputId($copyAllId),
$view->namespaceInputId($this->getInputId()),
]);
} else {
$view->registerJsWithVars(fn($copyAllId, $fieldId, $baseInfo) => <<<JS
(() => {
$('#' + $copyAllId).on('activate', () => {
const elementInfo = [];
Expand All @@ -921,13 +923,14 @@ protected function actionMenuItems(): array
});
})();
JS, [
$view->namespaceInputId($copyAllId),
$view->namespaceInputId($this->getInputId()),
[
'type' => Entry::class,
'fieldId' => $this->id,
],
]);
$view->namespaceInputId($copyAllId),
$view->namespaceInputId($this->getInputId()),
[
'type' => Entry::class,
'fieldId' => $this->id,
],
]);
}
}
}

Expand Down
Loading