简体   繁体   English

Drupal预处理商务功能

[英]Drupal preprocess a commerce function

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) {...} 我尝试使用类似的方法做到这一点: 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 ? 有什么方法可以预处理函数,以便我可以使用它们在模块中使用的所有值吗?

I think you need to use this hook : hook_field_widget_form_alter 我认为您需要使用此钩子: 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 注意:如果您不知道变量的结构,则将其转储以精确地定位您想要的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM