Computed Field - the Usual Rules Don't Apply

17 May, 2024


Sometimes the usual rules don't apply, and such is the case when hooking the module computed_field.

The Computed Field module provides the developer a way to created a field, the content of which will be based, for example, on the contents of other fields. This can be done either on-the-fly or by persisting the field contents in the database, the latter being required if the intention is to use the field in Views.

The documentation states:

The hook implementation function signature should be computed_field_<field_name>...

so applying the normal rules, I took this to mean that in the case of my_module, the function signature would begin with 

function my_module_<field_name>

The configuration form, when adding a computed field to your content type, provides these instructions as a strong suggestion (rightly so) to use the hook rather than entering a PHP snippet in the field itself. It states that when the hook is in place, the text area in which a snippet would be placed will be grayed out. I added the function, and the field remained active. Hmmm.

I then thought that perhaps "computed_field" needs to remain in the function name, and tried:

function my_module_computed_field_<field_name>

but the text area for the snippet continued to thumb its digital nose at me.

It turns out that in the case of hooking this module, the function signature needs to retain the computed_field module name. So, despite the fact that the function appears in my_module, the hook needs to be:

function computed_field_<field_name>

Login or Register to Comment!