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

[5.x]: Entry Condition not working properly on Matrix fields. #15260

Closed
belkin opened this issue Jun 27, 2024 · 4 comments
Closed

[5.x]: Entry Condition not working properly on Matrix fields. #15260

belkin opened this issue Jun 27, 2024 · 4 comments
Assignees
Labels

Comments

@belkin
Copy link

belkin commented Jun 27, 2024

What happened?

Description

The "Entry Condition" for fields in a Matrix does not work correctly when setting the condition to "Section -> Is one of -> 'Desired section'". This issue was tested on the latest Craft CMS version (5.2.3) and the demo version (europa.museum 5.1.9), both exhibiting the same problem.

Steps to reproduce

  1. Create a Matrix field with several block types.
  2. Set an Entry Condition on a field within one of the block types using the following settings:
  • Entry Condition -> Section -> Is one of -> "Desired section"
image
  1. Navigate to a section where the Matrix field is used. The field will not be visible in any section, including the "Desired section" specified in the condition.
image

Expected behavior

The field should be visible only in the selected section when editing an entry.

Actual behavior

The field does not appear in any section, including the specified "Desired section".

Craft CMS version

5.2.3

PHP version

8.2

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@belkin belkin added the bug label Jun 27, 2024
@i-just i-just self-assigned this Jun 27, 2024
@brandonkelly brandonkelly assigned brandonkelly and unassigned i-just Jun 27, 2024
brandonkelly added a commit that referenced this issue Jun 27, 2024
@brandonkelly
Copy link
Member

Matrix entries belong to their owner element, not a section, which is why the Section condition rule doesn’t work for them.

I don’t think it would make sense to loosen the condition rule up to include entries whose root-level owner is an entry that belongs to the selected section, for two reasons:

  • It would be a breaking change in behavior, as some conditions could be using the same entry type for section entries and Matrix/CKEditor entries, and expecting that the condition rule would exclude the Matrix/CKEditor entries.
  • It would open a can of worms, as Matrix/CKEditor fields can be added to other element types besides entries. What if you wanted to show a field based on whether the Matrix field is within a certain category group? Or a certain global set? Etc.

We’ll keep the use case in mind as we explore ways to improve conditions/conditional fields down the road, but in the meantime, I just added a new event for Craft 5.3, which you can use to supply your own conditional logic from a custom module:

use craft\base\FieldLayoutComponent;
use craft\elements\Entry;
use craft\events\DefineShowFieldLayoutComponentInFormEvent;
use craft\fieldlayoutelements\CustomField;
use yii\base\Event;

Event::on(
    CustomField::class,
    FieldLayoutComponent::EVENT_DEFINE_SHOW_IN_FORM,
    function (DefineShowFieldLayoutComponentInFormEvent $event) {
        /** @var CustomField $layoutElement */
        $layoutElement = $event->sender;
        if (
            $layoutElement->field->handle === 'narrowWidth' &&
            $event->element instanceof Entry &&
            $event->element->field?->handle === 'contentBlocks'
        ) {
            $owner = $event->element->getOwner();
            if ($owner instanceof Entry) {
                $event->showInForm = $owner->section?->handle === 'exhibits';
            }
        }
    }
);

If you want to test that out, you can change your craftcms/cms requirement in composer.json to:

"craftcms/cms": "5.3.x-dev as 5.3.0-alpha",

Then run composer update.

@mmikkel
Copy link
Contributor

mmikkel commented Jul 3, 2024

...I was taking the 5.3.x branch for a spin, and I'm getting the below exception on all element edit pages, which seems related to this change. I apologise for the noise in case this is already on your radar; I'm aware 5.3 isn't even a beta at this point 😌

Error: Typed property craft\base\FieldLayoutComponent::$_layout must not be accessed before initialization in /var/www/html/vendor/craftcms/cms/src/base/FieldLayoutComponent.php:332
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/models/FieldLayout.php(915): craft\base\FieldLayoutComponent->showInForm(Object(craft\elements\Entry))
#1 [internal function]: craft\models\FieldLayout->_elements(Object(Closure), Object(craft\elements\Entry))
#2 /var/www/html/vendor/craftcms/cms/src/models/FieldLayout.php(717): iterator_to_array(Object(Generator))
#3 /var/www/html/vendor/craftcms/cms/src/models/FieldLayout.php(673): craft\models\FieldLayout->_customFields(Object(craft\elements\Entry))
#4 /var/www/html/vendor/craftcms/cms/src/base/Element.php(6055): craft\models\FieldLayout->getVisibleCustomFields(Object(craft\elements\Entry))
#5 /var/www/html/vendor/craftcms/cms/src/base/Element.php(4924): craft\base\Element->fieldLayoutFields(true)
#6 /var/www/html/vendor/craftcms/cms/src/controllers/ElementsController.php(2306): craft\base\Element->setFieldValuesFromRequest('fields')
#7 /var/www/html/vendor/craftcms/cms/src/controllers/ElementsController.php(267): craft\controllers\ElementsController->_applyParamsToElement(Object(craft\elements\Entry))
#8 [internal function]: craft\controllers\ElementsController->actionEdit(Object(craft\elements\Entry), 45)
#9 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#10 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#11 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('edit', Array)
#12 /var/www/html/vendor/craftcms/cms/src/web/Application.php(349): yii\base\Module->runAction('elements/edit', Array)
#13 /var/www/html/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('elements/edit', Array)
#14 /var/www/html/vendor/craftcms/cms/src/web/Application.php(317): yii\web\Application->handleRequest(Object(craft\web\Request))
#15 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#16 /var/www/html/web/index.php(12): yii\base\Application->run()
#17 {main}

@mmikkel
Copy link
Contributor

mmikkel commented Jul 4, 2024

@brandonkelly On further inspection, the exception seems related to the craft\debug\Module module; it only happens if the "Show the debug toolbar in the control panel" admin preference is enabled.

@brandonkelly
Copy link
Member

@mmikkel Thanks! Fixed via 82524c2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants