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]: Invalid Configuration: Either sectionId or fieldId + ownerId must be set on the entry #16254

Closed
jamiepittock opened this issue Dec 2, 2024 · 2 comments
Labels

Comments

@jamiepittock
Copy link

jamiepittock commented Dec 2, 2024

What happened?

Description

I ran across this when attaching a behavior to our entries.

Event::on(
    Entry::class,
    Entry::EVENT_DEFINE_BEHAVIORS,
    function(DefineBehaviorsEvent $e) {
        if ($e->sender->getType()->handle === 'questionType') {
            $e->behaviors['questionTypeStats'] = QuestionTypeBehavior::class;
        }
    }
);

For nested matrix entries this seems to throw the error

Invalid Configuration – yii\base\InvalidConfigException
Either sectionId or fieldId + ownerId must be set on the entry.

These entries have a fieldId + primaryOwnerId, but no ownerId.

Image

Craft CMS version

5.5.3

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

brandonkelly added a commit that referenced this issue Dec 2, 2024
@brandonkelly
Copy link
Member

There were a few areas throughout the control panel where we were instantiating elements without actually configuring them at all (e.g. setting the sectionId/fieldId/ownerId on entries), which led to this error when calling getType().

Craft 4.13.4 and 5.5.4 are out with a couple fixes for those (130fd77 + eff6671).

You still may want to make your code a bit more defensive though, in case any plugins are doing something similar.

use craft\elements\Entry;
use craft\events\DefineBehaviorsEvent;
use yii\base\Event;
use yii\base\InvalidConfigException;

Event::on(Entry::class, Entry::EVENT_DEFINE_BEHAVIORS, function(DefineBehaviorsEvent $e) {
    /** @var Entry $entry */
    $entry = $e->sender;

    try {
        $type = $entry->getType();
    } catch (InvalidConfigException $e) {
        return;
    }

    if ($type->handle === 'questionType') {
        $e->behaviors['questionTypeStats'] = QuestionTypeBehavior::class;
    }
});

@jamiepittock
Copy link
Author

Thanks!

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

2 participants