Skip to content

Commit

Permalink
Make it possible to disable native sources
Browse files Browse the repository at this point in the history
Resolves #10676
  • Loading branch information
brandonkelly committed Apr 5, 2022
1 parent d33e2b2 commit 3cad4da
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 84 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Element slideouts now support provisional drafts and autosaving, for element types that support them. ([#10467](https://github.com/craftcms/cms/pull/10467))
- Element indexes can now be filtered by element attributes and custom field values. ([#9192](https://github.com/craftcms/cms/discussions/9192), [#9450](https://github.com/craftcms/cms/discussions/9450), [#9462](https://github.com/craftcms/cms/discussions/9462), [#9483](https://github.com/craftcms/cms/discussions/9483))
- Admins can now create custom element sources from the Customize Sources modal. ([#8423](https://github.com/craftcms/cms/discussions/8423))
- It’s now possible to disable native element sources from the Customize Sources modal. ([#10676](https://github.com/craftcms/cms/discussions/10676))
- Field layout tabs, fields, and UI elements can now be conditionally shown based on properties of the current user and/or element being edited. ([#8099](https://github.com/craftcms/cms/discussions/8099), [#8154](https://github.com/craftcms/cms/discussions/8154))
- Assets, Entries, and Users fields have new condition settings that can be used to further limit which elements should be relatable, beyond the existing field settings. ([#10393](https://github.com/craftcms/cms/pull/10393))
- Assets, Entries, and Users fields have new “Min Relations” settings, and their former “Limit” settings have been renamed to “Max Relations”. ([#8621](https://github.com/craftcms/cms/discussions/8621))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Added
- It’s now possible to disable native element sources from the Customize Sources modal. ([#10676](https://github.com/craftcms/cms/discussions/10676))
- GraphQL schemas now include settings that determine which sites elements can be queried from. ([#10610](https://github.com/craftcms/cms/issues/10610))
- Added `craft\base\FsInterface::read()`.
- Added `craft\base\FsInterface::write()`.
Expand Down
15 changes: 13 additions & 2 deletions src/controllers/ElementIndexSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function actionGetCustomizeSourcesModalData(): Response

// Get the source info
$sourcesService = Craft::$app->getElementSources();
$sources = $sourcesService->getSources($elementType, ElementSources::CONTEXT_INDEX);
$sources = $sourcesService->getSources($elementType, ElementSources::CONTEXT_INDEX, true);

foreach ($sources as &$source) {
if ($source['type'] === ElementSources::TYPE_HEADING) {
Expand Down Expand Up @@ -153,6 +153,7 @@ public function actionSaveCustomizeSourcesModalSettings(): Response
$sourceOrder = $this->request->getBodyParam('sourceOrder', []);
$sourceSettings = $this->request->getBodyParam('sources', []);
$newSourceConfigs = [];
$disabledSourceKeys = [];

// Normalize to the way it's stored in the DB
foreach ($sourceOrder as $source) {
Expand Down Expand Up @@ -182,9 +183,17 @@ public function actionSaveCustomizeSourcesModalSettings(): Response
if (isset($postedSettings['userGroups']) && $postedSettings['userGroups'] !== '*') {
$sourceConfig['userGroups'] = is_array($postedSettings['userGroups']) ? $postedSettings['userGroups'] : false;
}
} elseif (isset($postedSettings['enabled'])) {
$sourceConfig['disabled'] = !$postedSettings['enabled'];
if ($sourceConfig['disabled']) {
$disabledSourceKeys[] = $source['key'];
}
}
} elseif (isset($oldSourceConfigs[$source['key']])) {
$sourceConfig += $oldSourceConfigs[$source['key']];
if (!empty($sourceConfig['disabled'])) {
$disabledSourceKeys[] = $source['key'];
}
} elseif ($isCustom) {
// Ignore it
continue;
Expand All @@ -195,6 +204,8 @@ public function actionSaveCustomizeSourcesModalSettings(): Response
}

$projectConfig->set(ProjectConfig::PATH_ELEMENT_SOURCES . ".$elementType", $newSourceConfigs);
return $this->asSuccess();
return $this->asSuccess(data: [
'disabledSourceKeys' => $disabledSourceKeys,
]);
}
}
18 changes: 14 additions & 4 deletions src/services/ElementSources.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ public static function filterExtraHeadings(array $sources): array
* @param string $elementType The element type class
* @phpstan-param class-string<ElementInterface> $elementType
* @param string $context The context
* @param bool $withDisabled Whether disabled sources should be included
* @return array[]
*/
public function getSources(string $elementType, string $context = self::CONTEXT_INDEX): array
public function getSources(string $elementType, string $context = self::CONTEXT_INDEX, bool $withDisabled = false): array
{
$nativeSources = $this->_nativeSources($elementType, $context);
$sourceConfigs = $this->_sourceConfigs($elementType);
Expand All @@ -83,8 +84,12 @@ public function getSources(string $elementType, string $context = self::CONTEXT_
foreach ($sourceConfigs as $source) {
if ($source['type'] === self::TYPE_NATIVE) {
if (isset($indexedNativeSources[$source['key']])) {
$sources[] = $source + $indexedNativeSources[$source['key']];
$nativeSourceKeys[$source['key']] = true;
if ($withDisabled || !($source['disabled'] ?? false)) {
$sources[] = $source + $indexedNativeSources[$source['key']];
$nativeSourceKeys[$source['key']] = true;
} else {
unset($indexedNativeSources[$source['key']]);
}
}
} else {
if ($source['type'] === self::TYPE_CUSTOM && !$this->_showCustomSource($source)) {
Expand All @@ -95,7 +100,12 @@ public function getSources(string $elementType, string $context = self::CONTEXT_
}

// Make sure all native sources are accounted for
$missingSources = array_filter($nativeSources, fn($s) => $s['type'] === self::TYPE_NATIVE && !isset($nativeSourceKeys[$s['key']]));
$missingSources = array_filter($nativeSources, fn($s) => (
$s['type'] === self::TYPE_NATIVE &&
isset($indexedNativeSources[$s['key']]) &&
!isset($nativeSourceKeys[$s['key']])
));

if (!empty($missingSources)) {
if (!empty($sources)) {
$sources[] = [
Expand Down
9 changes: 7 additions & 2 deletions src/templates/_elements/sources.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
: false,
sites: (source.sites ?? false) ? source.sites|join(',') : false,
'override-status': (source.criteria.status ?? false) ? true : false,
disabled: source.disabled ?? false,
}|merge(source.data ?? {}),
html: sourceInnerHtml(source)
}) }}
Expand Down Expand Up @@ -49,7 +50,11 @@
<li class="heading"><span>{{ source.heading|t('site') }}</span></li>
{% else %}
{% set key = source.keyPath ?? (keyPrefix ~ source.key) %}
<li>
{% tag 'li' with {
class: [
(source.disabled ?? false) ? 'hidden' : null,
]|filter,
} %}
{{ sourceLink(key, source, not keyPrefix, elementType) }}
{% if source.nested is defined and source.nested is not empty %}
<button class="toggle" aria-expanded="false" aria-label="{{ 'Show nested sources'|t('app') }}"></button>
Expand All @@ -58,7 +63,7 @@
sources: source.nested
} %}
{% endif %}
</li>
{% endtag %}
{% endif %}
{% endfor %}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_layouts/elementindex.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% exit 404 %}
{% endif %}

{% set sources = craft.app.elementSources.getSources(elementType) %}
{% set sources = craft.app.elementSources.getSources(elementType, 'index', true) %}

{% set showSiteMenu = (craft.app.getIsMultiSite() ? (showSiteMenu ?? 'auto') : false) %}
{% if showSiteMenu == 'auto' %}
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.

59 changes: 32 additions & 27 deletions src/web/assets/cp/src/js/BaseElementIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -1544,35 +1544,16 @@ Craft.BaseElementIndex = Garnish.Base.extend({
_setSite: function(siteId) {
let firstSite = this.siteId === null;
this.siteId = siteId;
this.$visibleSources = $();

// Hide any sources that aren't available for this site
var $firstVisibleSource;
var $source;
// Select a new source automatically if a site is already selected, but we don't have a selected source
// (or if the currently selected source ends up not supporting the new site)
var selectNewSource = !firstSite && (!this.$source || !this.$source.length);

for (var i = 0; i < this.$sources.length; i++) {
$source = this.$sources.eq(i);
if (typeof $source.data('sites') === 'undefined' || $source.data('sites').toString().split(',').indexOf(siteId.toString()) !== -1) {
$source.parent().removeClass('hidden');
this.$visibleSources = this.$visibleSources.add($source);
if (!$firstVisibleSource) {
$firstVisibleSource = $source;
}
} else {
$source.parent().addClass('hidden');

// Is this the currently selected source?
if (this.$source && this.$source.get(0) == $source.get(0)) {
selectNewSource = true;
}
}
}
this.updateSourceVisibility();

if (this.initialized && selectNewSource) {
this.selectSource($firstVisibleSource);
if (
this.initialized &&
!firstSite &&
(!this.$source || !this.$source.length) &&
this.$visibleSources.length
) {
this.selectSource(this.$visibleSources[0]);
}

// Hide any empty-nester headings
Expand All @@ -1599,6 +1580,30 @@ Craft.BaseElementIndex = Garnish.Base.extend({
}
},

updateSourceVisibility: function() {
this.$visibleSources = $();

for (let i = 0; i < this.$sources.length; i++) {
const $source = this.$sources.eq(i);

if (
!Garnish.hasAttr($source, 'data-disabled') &&
(typeof $source.data('sites') === 'undefined' || $source.data('sites').toString().split(',').indexOf(this.siteId.toString()) !== -1)
) {
$source.parent().removeClass('hidden');
this.$visibleSources = this.$visibleSources.add($source);
} else {
$source.parent().addClass('hidden');

// Is this the currently selected source?
if (this.$source && this.$source.get(0) === $source.get(0)) {
this.$source = null;
this.sourceKey = null;
}
}
}
},

_handleSortChange: function(ev) {
var $option = $(ev.selectedOption);

Expand Down
Loading

0 comments on commit 3cad4da

Please sign in to comment.