Skip to content

Commit

Permalink
allow copying of fields in matrix "block" but only when opened as a s…
Browse files Browse the repository at this point in the history
…tandalone entry
  • Loading branch information
i-just committed Oct 1, 2024
1 parent 027d36e commit 7f99d21
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
12 changes: 1 addition & 11 deletions src/helpers/ElementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use craft\db\Table;
use craft\errors\OperationAbortedException;
use craft\fieldlayoutelements\CustomField;
use craft\fields\Matrix;
use craft\i18n\Locale;
use craft\services\ElementSources;
use craft\web\View;
Expand Down Expand Up @@ -429,22 +428,13 @@ public static function isElementEditable(ElementInterface $element): bool
/**
* Returns whether given element supports having its field values copied from another site.
*
* @param ElementInterface $element
* @param ElementInterface|null $element
* @return bool
*/
public static function supportsFieldCopying(?ElementInterface $element = null): bool
{
// disallow copying fields in an entry that's nested in a matrix field,
// if the matrix field has view mode set to inline-editable blocks
if ($element instanceof NestedElementInterface) {
$field = $element->getField();

return !($field instanceof Matrix && $field->viewMode === Matrix::VIEW_MODE_BLOCKS);
}

return !(!Craft::$app->getIsMultiSite() ||
$element === null ||
//$element->getPrimaryOwnerId() ||
count(static::editableSiteIdsForElement($element)) < 2);
}

Expand Down
3 changes: 3 additions & 0 deletions src/models/FieldLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ public function createForm(?ElementInterface $element = null, bool $static = fal
// Any already-included layout elements?
$visibleElements = ArrayHelper::remove($config, 'visibleElements');

$disableCopying = ArrayHelper::remove($config, 'disableCopying') ?? false;

$form = new FieldLayoutForm($config);
$tabs = $this->getTabs();

Expand Down Expand Up @@ -877,6 +879,7 @@ public function createForm(?ElementInterface $element = null, bool $static = fal
$html = Html::modifyTagAttributes($html, [
'data' => [
'layout-element' => $isConditional ? $layoutElement->uid : true,
'disable-copying' => $disableCopying,
] + ($errorKey ? ['error-key' => $errorKey] : []),
]);

Expand Down
5 changes: 4 additions & 1 deletion src/templates/_components/fieldtypes/Matrix/block.twig
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@
{% endif %}

{% namespace baseInputName %}
{% set form = entry.getFieldLayout().createForm(entry, static, {'errorKeyPrefix' : "#{name}[#{entry.uid}]"}) %}
{% set form = entry.getFieldLayout().createForm(entry, static, {
'errorKeyPrefix' : "#{name}[#{entry.uid}]",
'disableCopying': true,
}) %}
{% set tabs = form.getTabMenu() %}
{% endnamespace %}

Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/web/assets/cp/src/js/CraftTooltip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {arrow, autoUpdate, computePosition, flip, offset, shift} from '@floating-ui/dom';
import {
arrow,
autoUpdate,
computePosition,
flip,
offset,
shift,
} from '@floating-ui/dom';

/**
* Tooltip
Expand Down
10 changes: 10 additions & 0 deletions src/web/assets/cp/src/js/ElementEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,16 @@ Craft.ElementEditor = Garnish.Base.extend(

const $btn = $(ev.currentTarget);

// don't show the hud if we're supposed to disable copying for this field
// this is used by fields inside inline-editable matrix "block"
const $fieldContainer = $btn.parents('.field');
if (
$fieldContainer.length > 0 &&
Garnish.hasAttr($fieldContainer[0], 'data-disable-copying')
) {
return;
}

const $hudContent = $('<div/>', {
class: 'copy-translation-dialogue',
});
Expand Down

0 comments on commit 7f99d21

Please sign in to comment.