2

For certain entries I would like to be able to override how Craft generates the URLs to them. So I create an Entry Model in my module extending from \craft\elements\Entry and override the getUrl() method there.

However I did not manage to replace Craft's class with mine.

How can I achieve this? I found solutions like Yii::$container->set('craft\elements\Entry', '\modules\Entry'); or the same with Craft::

I placed it in my index.php, I tried it in the head of /config/app.php, but nothing happened.

Any suggestions?

1

I was able to get it to use my class via the following in config/app.php

    'container' => [
        'definitions' => [
            \craft\elements\Entry::class => \modules\Entry::class
        ],
    ],

But it doesnt seem like Craft is able to properly load data into it and I get runtime errors. When debugging, the class is instantiated and it uses things like overridden functions correctly, but there is no entry data loaded into it, so it doesnt have an id, section, etc.

I did this based on the docs at https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations#application-configurations and https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.