0

My module provides a field. In hook_form_alter() I need to change only those nodes that have this field set up. (I can't operate use hook_field_form_alter() because it does operates changes for other things than my own field.)

How can check if my field belongs to the node?
Is there an easy function to check if the field type exists in $form?

Otherwise, if I need to look at $form the problem is that I can't know the machine name used for the field by the admin/drupal as it looks like something like $form['my_field_namedbyuser'].

  • are u searching for specific field named 'type' or a 'field type' like term reference , integer,text etc.. – harshal Sep 3 '13 at 5:09
  • Yes i am searching for a 'field type' like 'page' 'article' etc – Dylan G Sep 3 '13 at 7:14
  • sorry but 'page', 'article' are node types and not the field types .Do you want to check whether the field belongs to a particular node type ? – harshal Sep 3 '13 at 7:19
  • Yes sorry: field types: my module creates a field type, that's what i am looking for. Now it is ok: answer below :) – Dylan G Sep 3 '13 at 8:01
3

If you are searching for specific field then see the code below

 function hook_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'form_id_name') { 
        $abc = array();
        foreach ( $form_state['field'] as $key => $value ) {
            $abc[$key] = $key;
        };
}
    }

From this $abc variable you will get all the field name and then you can check from this array to see if the field you want exists or not using any of the php functions.
For e.g. you can use array_key_exists('field_name', $abc);

  • 2
    Thanks for that, and sorry i was not clear, i am searching for a 'field type' like 'page' 'article' etc so i just had to call the function field_info_field to make it happen ! $abc[$key] = field_info_field($key); – Dylan G Sep 3 '13 at 7:43
1

I have found this other solution as well if this can help someone:

  • Set up a new variable in $form_state of hook_mymodule_field_widget_mywidget_form_alter
  • Check for this variable in mymodule_form_alter

    function mymodule_field_widget_mywidget_form_alter(&$element, &$form_state, $context){
    $form_state['IsMyField']['#value'] = TRUE;
    }
    
    function mymodule_form_alter(&$form, $form_state, $form_id)
    if ($form_state['IsMyField']['#value']  == TRUE ) {
        //My CODE HERE
    }
    

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.