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

Element Index Column configuration gets unwieldy with fields outside the global namespace #4314

Closed
BenParizek opened this issue May 25, 2019 · 2 comments
Labels
enhancement improvements to existing features system administration 💼 features related to system administration
Milestone

Comments

@BenParizek
Copy link
Contributor

BenParizek commented May 25, 2019

The following issue describes a UI issue we run into with Sprout Forms. The issue may exist in other similar scenarios.

Form Element Index Customization

Sprout Forms has a Custom Element where users can add custom fields. Those fields are stored outside the global context like matrix blocks:

Element Field Context Edit Visibility
Entry global Via the Craft Fields tab
Matrix Block matrixBlockType:1 Via a specific Matrix Field settings
Form sproutForms:1 Via a specific Form Field Layout

When viewing the Element Index view for the Form Entries, a user can customize what columns appear, however, as seen in the screenshot above, that view shows ALL fields regardless of what Form Source is being shown.

In many cases Forms are used by marketing departments for short-term campaigns, leading to a lot of similar fields being created. One solution could be to create all these fields in the Global context but that also can lead to an intimidating number of not-so-relevant fields to choose from when working within Craft on any other field layout.

It seems this use case falls between the Entry and Matrix Block use cases in that the fields are outside of the global scope and they are also visible to the Element Index page.

@BenParizek
Copy link
Contributor Author

BenParizek commented May 25, 2019

This may relate to, or be more relevant to, Craft Elements if Field Instances are added:
#2403

@brandonkelly brandonkelly added system administration 💼 features related to system administration enhancement improvements to existing features labels May 27, 2019
@brandonkelly brandonkelly added this to the 4.0 milestone Aug 23, 2019
@brandonkelly brandonkelly added authoring ✍️ features related to author experience system administration 💼 features related to system administration and removed system administration 💼 features related to system administration authoring ✍️ features related to author experience labels May 20, 2020
@brandonkelly brandonkelly modified the milestones: 4.0, 3.5 May 20, 2020
@brandonkelly
Copy link
Member

Excited to say that I found a non-breaking way to fix this for Craft 3.5 🎉

To take advantage of the fix, element types will need to start implementing a defineFieldLayouts() method, which returns any field layouts that are associated with a given element source key.

Examples:

cms/src/elements/Asset.php

Lines 297 to 308 in 8678b51

public static function defineFieldLayouts(string $source): array
{
$fieldLayouts = [];
if (
preg_match('/^folder:(.+)$/', $source, $matches) &&
($folder = Craft::$app->getAssets()->getFolderByUid($matches[1])) &&
$fieldLayout = $folder->getVolume()->getFieldLayout()
) {
$fieldLayouts[] = $fieldLayout;
}
return $fieldLayouts;
}

cms/src/elements/Entry.php

Lines 261 to 283 in 8678b51

protected static function defineFieldLayouts(string $source): array
{
// Get all the sections covered by this source
$sections = [];
if ($source === '*') {
$sections = Craft::$app->getSections()->getAllSections();
} else if ($source === 'singles') {
$sections = Craft::$app->getSections()->getSectionsByType(Section::TYPE_SINGLE);
} else if (
preg_match('/^section:(.+)$/', $source, $matches) &&
$section = Craft::$app->getSections()->getSectionByUid($matches[1])
) {
$sections = [$section];
}
$fieldLayouts = [];
foreach ($sections as $section) {
foreach ($section->getEntryTypes() as $entryType) {
$fieldLayouts[] = $entryType->getFieldLayout();
}
}
return $fieldLayouts;
}

craft\base\Element::defineFieldLayouts() returns all field layouts associated with the element type by default, so if you don’t override it, there won’t be any discernible difference compared to how things currently work (all previewable custom fields used by the element type will be available for all sources).

Using the same method to help filter the available sort options based on the selected source as well (#4802).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improvements to existing features system administration 💼 features related to system administration
Projects
None yet
Development

No branches or pull requests

2 participants