Drupal preprocess a commerce function

2019-08-20 07:36发布

问题:

I need to preprocess a function from the module "commerce pricing attributes".

Here is the function :

function commerce_pricing_attributes_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {...}

I don't know how to preprocess this (if it's possible).

This function create some element in the back-office and the thing I want to do is to give a color to these elements in function of the type of the option the element is. If it's an insurance option there is a color, if it's a room option another color.

I try to do this with an alter like this : function my_module_field_widget_commerce_pricing_attributes_custom_widget_form_alter(&$element, &$form_state, $context) {...}

But I can't have all the information I need (the type of the option).

Is there any way to preprocess the function so I can use all the values they use in their module ?

回答1:

I think you need to use this hook : hook_field_widget_form_alter

It allow you to override (or add) widget applied to a field

function my_module_field_widget_form_alter(&$element, &$form_state, $context) {

  if ($context['field']['type'] == 'mytype') { // you can use another condition on field name or whatever 

    // Loop through the element children (there will always be at least one).
    foreach (element_children($element) as $key => $child) {
      // Add the new process function to the element
      $element[$key]['#process'][] = 'my_custom_callback_field_widget_process';
    }
  }
}

function my_custom_callback_field_widget_process($element, &$form_state, $form){
// do your stuff
  return $element;
 }

NB : dump variables to target exactly you want if you don't know structre of them