0

I have written a custom field module and I would like to know how I can programmatically find out what the name of the field in the database.

E.g. someone installs my module, that installs a new fieldtype and widget. The user adds a new field to a content type and calls the field "test_field". How can the module provide the database name of the field ("test_field") to some function within the module?

Currently I am hard-coding the name of the field which works fine until a user decides to use another name. Then I am not able to do some database operations anymore.

  • 1
    How does a user add a new field? Somewhere in your module you should have a form submit handler, with a $form_state variable, there you should look at. How does Drupal know the names of the new added fields, think about it. – mchar Oct 24 '16 at 15:29
  • I've been going through the documentation for the form_state (api.drupal.org/api/drupal/includes!form.inc/function/…) and I was able to get things like the form_id from it, but not the machine_name of the field. Do you any other clues for me? – stopopol Nov 4 '16 at 8:57
  • Maybe I should also add that my costume function to access the database is triggered by a node_form_alter function – stopopol Nov 4 '16 at 13:13
0

OK. I ended up doing it a little differently:

In my field_widget_form function I added a hidden field. In this function it is possible to access the $field and therefore the name of the field using

$field['field_name'];

So I saved that value to the hidden form. Then when I trigger a function within that form it is able to access that value. So in my different function that relies on the machine_name of the field I actually access the value of the form that is set when the widget form is built.

  • That's really good, you tried yourself to fix it, excellent! Just one thing, what if you have ten fields, will you create more ten hidden fields? Think about it. – mchar Nov 7 '16 at 19:01

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.