Skip to content

Commit

Permalink
Limit custom sources to applicable custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Dec 13, 2024
1 parent 335fa0f commit 5677b0c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
- The global sidebar no longer shows “Failed” for queue jobs, for users that don’t have access to the Queue Manager. ([#16184](https://github.com/craftcms/cms/issues/16184))
- Addresses and Matrix fields now show provisional drafts when previewing an owner element. ([#16295](https://github.com/craftcms/cms/issues/16295))
- Color fields with a predefined color palette now primarily show a color select dropdown, rather than a manual color input. ([#16249](https://github.com/craftcms/cms/pull/16249))
- Improved how fields are sized in responsive field layouts. ([#16303](https://github.com/craftcms/cms/pull/16303))
- Improved how fields are sized in responsive field layouts. ([#16303](https://github.com/craftcms/cms/pull/16303))
- Entry indexes now only show table column options and sort options for custom fields associated with the selected sections/entry types within custom entry sources’ conditions.

### Accessibility
- Improved the accessibility of Checkboxes and Radio Buttons fields that allow custom options. ([#16080](https://github.com/craftcms/cms/pull/16080))
Expand Down Expand Up @@ -50,6 +51,7 @@

### Extensibility
- Added `craft\base\Element::EVENT_DEFINE_ALT_ACTIONS`. ([#16294](https://github.com/craftcms/cms/pull/16294))
- Added `craft\base\ElementInterface::fieldLayoutsForCustomSource()`.
- Added `craft\base\ElementInterface::getAltActions()`. ([#16294](https://github.com/craftcms/cms/pull/16294))
- Added `craft\base\conditions\BaseElementSelectConditionRule::allowMultiple()`.
- Added `craft\base\conditions\BaseElementSelectConditionRule::getElementIds()`.
Expand Down
8 changes: 8 additions & 0 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,14 @@ protected static function defineFieldLayouts(?string $source): array
return Craft::$app->getFields()->getLayoutsByType(static::class);
}

/**
* @inheritdoc
*/
public static function fieldLayoutsForCustomSource(array $config): array
{
return Craft::$app->getFields()->getLayoutsByType(static::class);
}

/**
* @inheritdoc
*/
Expand Down
9 changes: 9 additions & 0 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ public static function sourcePath(string $sourceKey, string $stepKey, ?string $c
*/
public static function fieldLayouts(?string $source): array;

/**
* Returns all the field layouts associated with elements from the given custom source.
*
* @param array $config The custom source config
* @return FieldLayout[]
* @since 5.6.0
*/
public static function fieldLayoutsForCustomSource(array $config): array;

/**
* Modifies a custom source’s config, before it’s returned by [[craft\services\ElementSources::getSources()]]
*
Expand Down
58 changes: 58 additions & 0 deletions src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,64 @@ protected static function defineFieldLayouts(?string $source): array
return array_map(fn(EntryType $entryType) => $entryType->getFieldLayout(), $entryTypes);
}

/**
* @interitdoc
*/
public static function fieldLayoutsForCustomSource(array $config): array
{
if (empty($config['condition']['conditionRules'])) {
return parent::fieldLayoutsForCustomSource($config);
}

// see if it specifies any entry types
/** @var TypeConditionRule|null $entryTypeRule */
$entryTypeRule = ArrayHelper::firstWhere(
$config['condition']['conditionRules'],
fn(array $rule) => $rule['class'] === TypeConditionRule::class,
);
$entryTypeOptions = $entryTypeRule['operator'] === 'in' && !empty($entryTypeRule['values'])
? $entryTypeRule['values']
: null;

if ($entryTypeOptions) {
$fieldLayouts = [];
$entriesService = Craft::$app->getEntries();
foreach ($entryTypeOptions as $entryTypeUid) {
$entryType = $entriesService->getEntryTypeByUid($entryTypeUid);
if ($entryType) {
$fieldLayout = $entryType->getFieldLayout();
$fieldLayouts[$fieldLayout->uid] = $fieldLayout;
}
}
return array_values($fieldLayouts);
}

/** @var SectionConditionRule|null $sectionRule */
$sectionRule = ArrayHelper::firstWhere(
$config['condition']['conditionRules'],
fn(array $rule) => $rule['class'] === SectionConditionRule::class,
);
$sectionOptions = $sectionRule['operator'] === 'in' && !empty($sectionRule['values'])
? $sectionRule['values']
: null;

if ($sectionOptions) {
$fieldLayouts = [];
foreach ($sectionOptions as $sectionUid) {
$section = Craft::$app->getEntries()->getSectionByUid($sectionUid);
if ($section) {
foreach ($section->getEntryTypes() as $entryType) {
$fieldLayout = $entryType->getFieldLayout();
$fieldLayouts[$fieldLayout->uid] = $fieldLayout;
}
}
}
return array_values($fieldLayouts);
}

return parent::fieldLayoutsForCustomSource($config);
}

/**
* @inheritdoc
*/
Expand Down
6 changes: 6 additions & 0 deletions src/services/ElementSources.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use craft\helpers\Cp;
use craft\helpers\StringHelper;
use craft\models\FieldLayout;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use yii\base\Component;

Expand Down Expand Up @@ -277,6 +278,11 @@ public function getFieldLayoutsForSource(string $elementType, string $sourceKey)
{
// Don't bother the element type for custom sources
if (str_starts_with($sourceKey, 'custom:')) {
$sourceConfigs = $this->_sourceConfigs($elementType);
$sourceConfig = Arr::first($sourceConfigs, fn(array $config) => ($config['key'] ?? null) === $sourceKey);
if ($sourceConfig) {
return $elementType::fieldLayoutsForCustomSource($sourceConfig);
}
return Craft::$app->getFields()->getLayoutsByType($elementType);
}

Expand Down

0 comments on commit 5677b0c

Please sign in to comment.