Alter Drupal 7 Text list values when you don't know position of text list in form array

289 views Asked by At

I am creating a module to alter the output of Drupal 7 text list fields.

I know that I can use hook_form_alter to do this, the problem is that the text field is output in different forms in different places and at different depths in the array structure.

In one case it may be at -

$form['elements']['some_array_key']['fields']['my_text_list_field'];

In other cases it may be at -

$form['fields']['some_array_key'][0]['my_text_list_field'];

How can I reliably locate and alter ['my_text_list_field'] in Drupal's form array, regardless of structure of the array it is contained within?

drupal_array_nested_key_exists almost does it, but not quite, since it requires that I know the parents of the element I am looking for.

1

There are 1 answers

0
D34dman On

For your use case, i suggest you try creating a field widget.

Tutorial 1a

also example modules for developers contains excellent code (with documentation) which you can modify to suit your need. You would have to look at field_example module.

In many cases you may want to even provide a display formatter along with your field widget, so if you want to alter the output of a field without any prior knowledge of how and where its going to be used, then you need to create a custom field formatter.

Tutorial 2a Tutorial 2b