【问题标题】:Drupal preprocess a commerce functionDrupal 预处理一个商务功能
【发布时间】:2018-09-18 09:06:07
【问题描述】:

我需要从模块“商业定价属性”中预处理一个函数。

这里是函数:

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

我不知道如何预处理这个(如果可能的话)。

这个函数在后台创建一些元素,我想做的是根据元素的选项类型为这些元素赋予颜色。如果是保险选项,则有一种颜色,如果是房间选项,则另一种颜色。

我尝试用这样的改变来做到这一点: function my_module_field_widget_commerce_pricing_attributes_custom_widget_form_alter(&$element, &$form_state, $context) {...}

但我无法获得所需的所有信息(选项的类型)。

有什么方法可以预处理函数,以便我可以使用他们在模块中使用的所有值?

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

    我认为你需要使用这个钩子:hook_field_widget_form_alter

    它允许您覆盖(或添加)应用于字段的小部件

    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 : 如果你不知道它们的结构,将变量转储到你想要的目标

    【讨论】:

    • 我不知道你能不能帮我解决这个问题,但现在的问题是我不知道小部件的选项类型,如果我能得到,我想我可以做到这一点产品的所有可能选项并将它们与我的小部件进行比较,但我没有找到任何具有 drupal commerce 的功能可以给我可能的选项列表
    • 转储变量 $context 并查看结构以在您在表单上识别您的字段
    • 这就是我想要做的,但问题是上下文、form_state 和元素没有我需要对选项类型进行排序的东西。我已经两天了,我找不到解决方案
    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 2011-02-15
    • 2018-10-11
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多