Jump to content

Hooks in LanguagesPageFieldValue


bfncs
 Share

Recommended Posts

Hello everyone,

I was just asking myself, why there are no hooks for the methods in the LanguagesFieldValue class.

I was going to improve the multilanguage support with a modul that introduces fallback languages. That would mean that for each language you can declare none or many fallback languages and if there is no translation of the value in the current language, it goes through all fallback languages and looks whether there's a corresponding value defined.

It would be easy to implement this by hooking to the toString() method, but it is not hookable.

Do you see another simple way to implement what I need or can we get hooks in the class in core?

With kind regards,

Marc

Link to comment
Share on other sites

Ryan is open to add hooks when requested and it makes sense or there's a need for it. Language modules are relatively new and not many things has been done with it so it's just a matter of asking. I also am working with languages and recently thought the same while looking at the LanguageSupport modules.

You could for now just make them hookable by putting ___ prefix.

  • Like 1
Link to comment
Share on other sites

I'm not sure about other ways. I haven't thought about it and not 100% sure what you thought would be the way. And there's already a fallback (setting on the language field) to show default language value if empty. Maybe it would be god to start studying where this happens and how it's done? 

Link to comment
Share on other sites

Hi Soma,

thanks for your quick and helpful answer!

I'd definitely just patch the core to support a hook where I need it, if I'd know, that this would also be in releases from 2.4 on. I don't really want to bring me into a situation, where I have to patch the core after every upgrade.

I just filed a pull request, if Ryan likes it, I hope it could already be in the next release.

https://github.com/ryancramerdesign/ProcessWire/pull/259

About the other ways:

Thanks for thinking about this. The fallback is also manifested in the toString() method either, so no chance to do this without a hook. It might make sense to refactor this into a (hookable) method on it's own.

So, I can't really think of anything else that makes sense, but you never know whether you're just too blind, so I'm asking.  ;)

Link to comment
Share on other sites

Thanks for the PR on this. I will go ahead and implement it. Glad to see you moved the hook to a separate function, as __toString() is a native PHP thing that we can't change the name of. But this is also a high-volume function, so one that I might need to add a little extra bits to keep things running fast. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...