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

Site-specific custom sources #13368

Merged
merged 1 commit into from
Jun 28, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Improved the contrast of asset file type icons. ([#13262](https://github.com/craftcms/cms/pull/13262))

### Administration
- Custom element sources can now be configured to only appear for certain sites. ([#13344](https://github.com/craftcms/cms/discussions/13344))
- The “My Account” page no longer shows a “Require a password reset on next login” checkbox.
- The Asset Indexes utility no longer shows the “Cache remote images” option on ephemeral environments. ([#13202](https://github.com/craftcms/cms/issues/13202))

Expand Down
4 changes: 2 additions & 2 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ public static function statuses(): array;
* - **`status`** – The status color that should be shown beside the source label. Possible values include `green`,
* `orange`, `red`, `yellow`, `pink`, `purple`, `blue`, `turquoise`, `light`, `grey`, `black`, and `white`. (Optional)
* - **`badgeCount`** – The badge count that should be displayed alongside the label. (Optional)
* - **`sites`** – An array of site IDs that the source should be shown for, on multi-site element indexes. (Optional;
* by default the source will be shown for all sites.)
* - **`sites`** – An array of site IDs or UUIDs that the source should be shown for, on multi-site element indexes.
* (Optional; by default the source will be shown for all sites.)
* - **`criteria`** – An array of element criteria parameters that the source should use when the source is selected.
* (Optional)
* - **`data`** – An array of `data-X` attributes that should be set on the source’s `<a>` tag in the source list’s,
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/ElementIndexSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public function actionGetCustomizeSourcesModalData(): Response
$source += compact('conditionBuilderHtml', 'conditionBuilderJs');
}

if (isset($source['sites']) && $source['sites'] === false) {
$source['sites'] = [];
}

if (isset($source['userGroups']) && $source['userGroups'] === false) {
$source['userGroups'] = [];
}
Expand Down Expand Up @@ -258,6 +262,10 @@ public function actionSaveCustomizeSourcesModalSettings(): Response
'condition' => $conditionsService->createCondition($postedSettings['condition'])->getConfig(),
];

if (isset($postedSettings['sites']) && $postedSettings['sites'] !== '*') {
$sourceConfig['sites'] = is_array($postedSettings['sites']) ? $postedSettings['sites'] : false;
}

if (isset($postedSettings['userGroups']) && $postedSettings['userGroups'] !== '*') {
$sourceConfig['userGroups'] = is_array($postedSettings['userGroups']) ? $postedSettings['userGroups'] : false;
}
Expand Down
1 change: 1 addition & 0 deletions src/translations/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
'Choose which filesystem assets should be stored in.' => 'Choose which filesystem assets should be stored in.',
'Choose which filesystem image transforms should be stored in.' => 'Choose which filesystem image transforms should be stored in.',
'Choose which sites this section should be available in, and configure the site-specific settings.' => 'Choose which sites this section should be available in, and configure the site-specific settings.',
'Choose which sites this source should be visible for.' => 'Choose which sites this source should be visible for.',
'Choose which table columns should be visible for this source by default.' => 'Choose which table columns should be visible for this source by default.',
'Choose which user groups should have access to this source.' => 'Choose which user groups should have access to this source.',
'Choose' => 'Choose',
Expand Down
2 changes: 2 additions & 0 deletions src/web/assets/cp/CpAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private function _registerTranslations(View $view): void
'Buy {name}',
'Cancel',
'Choose a user',
'Choose which sites this source should be visible for.',
'Choose which table columns should be visible for this source by default.',
'Choose which user groups should have access to this source.',
'Clear',
Expand Down Expand Up @@ -304,6 +305,7 @@ private function _registerTranslations(View $view): void
'Showing your unsaved changes.',
'Sign in',
'Sign out now',
'Sites',
'Skip to {title}',
'Sort ascending',
'Sort attribute',
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.

17 changes: 16 additions & 1 deletion src/web/assets/cp/src/js/BaseElementIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2586,7 +2586,22 @@ Craft.BaseElementIndex = Garnish.Base.extend(
.data('sites')
.toString()
.split(',')
.indexOf(this.siteId.toString()) !== -1)
.some((siteId) => {
if (siteId == this.siteId) {
return true;
}
// maybe UUIDs were used
if (siteId != parseInt(siteId)) {
const site = Craft.sites.find(
(site) => site.id == this.siteId
);
if (site && siteId == site.uid) {
return true;
}
}

return false;
}))
) {
$source.parent().removeClass('hidden');
this.$visibleSources = this.$visibleSources.add($source);
Expand Down
20 changes: 20 additions & 0 deletions src/web/assets/cp/src/js/CustomizeSourcesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Craft.CustomizeSourcesModal = Garnish.Modal.extend({
this.elementTypeName = response.elementTypeName;
this.conditionBuilderHtml = response.conditionBuilderHtml;
this.conditionBuilderJs = response.conditionBuilderJs;
this.sites = response.sites;
this.userGroups = response.userGroups;

if (response.headHtml) {
Expand Down Expand Up @@ -847,6 +848,25 @@ Craft.CustomizeSourcesModal.CustomSource =
this.createSortField($container);
this.createTableAttributesField($container);

if (Craft.sites.length > 1) {
Craft.ui
.createCheckboxSelectField({
label: Craft.t('app', 'Sites'),
instructions: Craft.t(
'app',
'Choose which sites this source should be visible for.'
),
name: `sources[${this.sourceData.key}][sites]`,
options: Craft.sites.map((site) => ({
label: site.name,
value: site.uid,
})),
values: this.sourceData.sites || '*',
showAllOption: true,
})
.appendTo($container);
}

if (this.modal.userGroups.length) {
Craft.ui
.createCheckboxSelectField({
Expand Down