diff --git a/src/base/Field.php b/src/base/Field.php index 9d00328bcca..eea905fef8b 100644 --- a/src/base/Field.php +++ b/src/base/Field.php @@ -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)) { @@ -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(); diff --git a/src/fieldlayoutelements/CustomField.php b/src/fieldlayoutelements/CustomField.php index 48c551499b8..8c98b933eeb 100644 --- a/src/fieldlayoutelements/CustomField.php +++ b/src/fieldlayoutelements/CustomField.php @@ -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 = []; } diff --git a/src/fields/Matrix.php b/src/fields/Matrix.php index ecfd1ba9bb6..d6a497da074 100644 --- a/src/fields/Matrix.php +++ b/src/fields/Matrix.php @@ -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) => <<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) => << { const expandAllBtn = $('#' + $expandAllId); const collapseAllBtn = $('#' + $collapseAllId); @@ -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) => <<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) => << { $('#' + $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) => <<namespaceInputId($copyAllId), + $view->namespaceInputId($this->getInputId()), + ]); + } else { + $view->registerJsWithVars(fn($copyAllId, $fieldId, $baseInfo) => << { $('#' + $copyAllId).on('activate', () => { const elementInfo = []; @@ -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, + ], + ]); + } } }