Drupal preprocess a commerce function

谁说我不能喝 提交于 2019-12-13 03:13:37

问题


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



来源:https://stackoverflow.com/questions/52382895/drupal-preprocess-a-commerce-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!