2

I'm new to Craft and PHP, so thanks ahead of time!

Is it possible to override a model method with a plugin? For example, I want to add some additional functionality to the getUrl method of the AssetFileModel class. Can this be done in a plugin?

Thanks!

1
  • As Brad mentioned below, you're probably headed in the wrong direction here... You might want to join Craft Slack and pose that question to the community. You'll probably get some great answers for how to better tackle your problem! – Lindsey D May 15 '16 at 5:21
3

Kind of, but probably not what you're looking for.

Your plugin can certainly do a MyAssetFileModel extends AssetFileModel and (in the scope of your plugin) take advantage of existing AssetFileModel methods and properties and override them as needed. But that's the catch... it will only be within the executing scope of your plugin.

There's no native way (without hacking core Craft files) to tell Craft "use MyAssetFileModel wherever you would normally use AssetFileModel".

However, this is certainly much more doable in the upcoming Craft 3 release since Yii 2 has native support for things like service locators and dependency injection.

1
  • Yeah, I had a feeling that was the case. Appreciate the help! – kroppian May 15 '16 at 1:25

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.