Jump to content
thirty bees forum

How to add custom fields without overrides


gandalf

Recommended Posts

@datakick said in How to add custom fields without overrides:

@gandalf why the no override requirement?

Because it's a perfect recipe for a disaster. Overriding core features is wrong, under any point of view. During a TB update you have to remember which modules are overriding which function and update these module accordingly and this is not always possible. You could have created an ovverride based on a function that is removed or changed in newer version and you can't bring that back without breaking everything else (discovered this in the worst way on a production ecommerce)

Probably, some hooks to be added in the core would do the trick without any overrides. As PS doesn't have any hook for doing this, i've thought that TB added something.

Maybe a pull-request would be accepted for this ?

Link to comment
Share on other sites

without using overrides.

Looking up the templates, like themes/community-theme-default/identity.tpl or themes/community-theme-default/authentication-create.tpl, one can find a few hooks which allow extending these fields. Just search for hook.

During a TB update you have to remember which modules are overriding which function and update these module accordingly and this is not always possible.

Partially true. It's true that sloppily written overrides can mess up a lot. Especially those just copying the entire original method to change just a line or two. One can write better overrides, though: - Check in each invocation whether the module is actually enabled. - Write code for just the change and call the parent method for everything else.

That said, a solution using hooks is still the better choice. PS 1.7 removed overrides for a reason.

Link to comment
Share on other sites

@traumflug said in How to add custom fields without overrides:

Write code for just the change and call the parent method for everything else.

Exactly. This is what we do when we are forced to use overrides. But this is possible only if parent function can be called (before or after customization). If you need to customize something "in the middle" of parent function, you can't simply call the parent.

And, as Murphy's Law tells you, most of the time you need to customize the middle of the parent method :)

I've never seen "HOOKCREATEACCOUNT_FORM" before, any example on how to use this ? I don't think would be possible to use this hook to save additional data to the customer table, as this extra field won't be saved by the TB controller. Isn't it ?

Link to comment
Share on other sites

I do not know if I read and understood everything correctly, my last sentence is probably the most important to note.

A free shop system for the general public. For special needs you can create / buy a module or use the overrides.

For me it is important that the basic system works. Custom things are a different story. Regional larger things may be an additional topic.

To integrate 2 fields for registration, you must either write a module yourself or buy a suitable one or use overrides.

I'm sorry, that's my personal opinion. That's a minor matter. What's wrong with taking notes on your own customizations / overrides or overwriting the affected files directly? Before each update, you should anyway create backup and test before. I know, it does not always work. Hence the backup from the backup.

Finally, I would like to add: - Overwrite the core files and put them into the database works only partially (why and a mandatory field?) - custom fields ----> - Login - account - invoice - pdf - back office - .... ------> standard theme, panda theme, or warhouse theme etc -> = does not work in the basic system for everything else, it does not matter

Link to comment
Share on other sites

@gandalf the Customer class extends ObjectModel, which provides multiple hooks through the object lifecycle.

In your case you would be interested in actionObjectCustomerAddAfter and actionObjectCustomerUpdateAfter. Probably also actionObjectCustomerUpdateAfter to clean leftover data.

Link to comment
Share on other sites

  • 2 weeks later...

@gonssal said in How to add custom fields without overrides:

@gandalf the Customer class extends ObjectModel, which provides multiple hooks through the object lifecycle.

In your case you would be interested in actionObjectCustomerAddAfter and actionObjectCustomerUpdateAfter. Probably also actionObjectCustomerUpdateAfter to clean leftover data.

How would you use this hook to add, let's say, 2 mandatory custom fields in the customer registration form ? Can you make me an example ?

Link to comment
Share on other sites

My only idea is to add some fields in the customer registration form. Via Javascript, on form submit, take these fields value, concatenate on an existing filed (so that it will be submitted with anything else) and, with some actionObjectCustomerAddBefore and actionObjectCustomerUpdateBefore split these concatenated fields, save properly in DB and let everything else flow through.

Just a proof-of-concept

Link to comment
Share on other sites

@gandalf well the Javascript way is an option, not ideal and easily broken depending on the user's input, but could work.

I would probably combine the already mentioned tpl/form hooks, the actionCustomerAccountAdd hook and the other hooks provided by ObjectModel.

No, I can't make an example, sorry.

It's also very likely there's at least a module that already implements this without overrides.

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
×
×
  • Create New...