3

I want to be able to modify a rule based on a setting choice made by a user, but I don't want to expose the form for setting the rule that you see at

admin/commerce/config/payment-methods/manage/<payment_method>/edit/<action_id>

because it contains some sensitive information such as our company's secret key for connecting to the payment gateway.

I have figured out that this UI is governed by a rule, and this particular setting is stored in the rules_config entity in its data field. I want to be able to directly manipulate this field, but I am unfamiliar with the format and do not want to mess up the entire rule.

Is there a way to do a entity_import and change the rule with the new settings on the fly? It seems like all of the hooks are only for when the module loads/installs.

  • This sounds flawed to me. You want to modify the internal logic of a Rule on the fy? Why not rewrite the Rule's logic as a once off to adapt to your varied situations? – J. Reynolds Jan 27 '16 at 7:08
3

There are likely several ways of peeling the proverbial potato, depending on what you're actually trying to do:

  1. See if the rule's callback function has some sort of hooks to allow you to change the data there. This is the easiest way to do anything and the least invasive.
  2. Implement hook_rules_config_execute(). This hook gets called during the invocation of the rule and it appears to give you a chance to modify the rule before everything gets executed.

Not generally recommended but also possible:

  1. Alter the rule's callback to call a new custom callback which wraps or replaces the original function and sets the configuration that you want.
  • When you are speaking of the rule's callback function, what exactly do you mean? Do you mean these drupalcontrib.org/api/search/7/hook_rules? None of these callback functions would work from what I can tell. hook_rules_config_execute does not give a reference to the config, so no changes can be made to it that would effect the original rule. – angms.bh Jan 26 '16 at 14:13
  • None of these solutions work because the callback functions for rules are only called on module enable. hook_rules_config_execute() does not allow you to modify the config, it just provides it so that you could know those settings for whatever else you are doing. Thank you for taking the time try to answer the question, but it doesn't work. – angms.bh Jan 29 '16 at 17:59
2

The issue is that the settings on an action of a rule need to be changed. The settings form for when you implement hook_settings_form($settings) for your payment method maps to the settings of the rule commerce_payment_<name of your payment method>. You need to change that rules action settings and then save. Here is an example.

Lets say that you have added a form element called txn_type to the settings form as follows in hook_settings_form($settings) of your payment method.

  $form['txn_type'] = array(
    '#type' => 'radios',
    '#title' => t('Default credit card transaction type'),
    '#description' => t('The default will be used to process transactions during checkout.'),
    '#options' => array(
            COMMERCE_CREDIT_AUTH_CAPTURE => t('Authorization and capture'),
            COMMERCE_CREDIT_AUTH_ONLY => t('Authorization only (requires manual or automated capture after checkout)'),
    ),
    '#default_value' => isset($settings['txn_type']) ? $settings['txn_type'] : COMMERCE_CREDIT_AUTH_CAPTURE,

);

To make the changes to the rule outside of the settings form, you need to load that rule and make changes to the action settings. You first load the rule, then you load the action settings within the rule, then you make your settings changes. The rule action name is always commerce_payment_enable_<name of your payment method>

$rule = rules_config_load('commerce_payment_<name of your payment method>');

foreach ($rule->actions() as $action) {
if (is_callable(array($action, 'getElementName')) && $action->getElementName() == 'commerce_payment_enable_<name of your payment method>') {
  dpm('bh_market_settings: in action array');
  if (is_array($action->settings['payment_method']) && !empty($action->settings['payment_method']['settings'])) {
    $action->settings['payment_method']['settings']['txn_type']=COMMERCE_CREDIT_AUTH_ONLY;
  }
 }
}

$rule->save();

This example can be applied to anytime you are trying to change a rule outside of the UI. You can make changes to all aspects of the rule and then save it using $rule->save()

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.