For custom action configurations we need feature support for states attribute similar to #state of form API.

Comments

gg24 created an issue. See original summary.

JayKandari’s picture

#states like functionality is directly not supported when defining parameters for a rule action in hook_rules_action_info()

This can be achieved by attaching a 'form_alter' callback function to our action. Eg:

/**
 * Implements hook_rules_action_info().
 */
function rules_joke_rules_action_info() {
  $actions['crack_chuk_norris_joke'] = array(
    'label' => 'Display a Chuck Norris Joke',
    'group' => t('System'),
    'parameter' => array(
      'default_message' => array(
        'label' => 'Default Message',
        'description' => 'Message to show if joke not found.',
        'type' => 'text',
        'optional' => TRUE,
      ),
      'country' => array(
        'label' => 'Your Country',
        'description' => 'Select your country.',
        'type' => 'text',
        'optional' => TRUE,
        'options list' => '_get_countries',
      ),
    ),
    'callbacks' => array(
      'form_alter' => 'rules_joke_test_form_alter',
    ),
  );
  return $actions;
}

/**
 * Get countries
 */
function _get_countries() {
  return array(
    'India' => 'India', 'Pakistan' => 'Pakistan', 'Uganda' => 'Uganda'
  );
}

Callback method :

/**
 * Callback method.
 */
function rules_joke_test_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  $form['parameter']['default_message'] += array(
    '#attributes' => array(
      'class' => array('custom-class'),
    ),
    '#states' => array(
      'invisible' => array(
        '#edit-parameter-country-settings-country' => array('value' => 'Pakistan'),
      ),
    ),
  );
}
gg24’s picture

Awesome! @jaykandari. This works for me.

Thanks!

TR’s picture

Category: Feature request » Support request
Status: Active » Fixed

This was answered by #2.

TR’s picture

Status: Fixed » Closed (fixed)