You are here

function rules_condition_entity_is_of_bundle_form_alter in Rules 7.2

Form alter callback for the condition entity_is_of_bundle.

Use multiple steps to configure the condition as the needed bundle field list depends on the selected entity type.

Related topics

File

modules/entity.rules.inc, line 541
General entity related rules integration.

Code

function rules_condition_entity_is_of_bundle_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  if (empty($element->settings['entity:select'])) {
    $step = 1;
  }
  elseif (empty($element->settings['type'])) {
    $step = 2;
  }
  else {
    $step = 3;
  }
  $form['reload'] = array(
    '#weight' => $form['submit']['#weight'] + 1,
    '#type' => 'submit',
    '#name' => 'reload',
    '#value' => $step != 3 ? t('Continue') : t('Reload form'),
    '#limit_validation_errors' => array(
      array(
        'parameter',
        'entity',
      ),
      array(
        'parameter',
        'type',
      ),
    ),
    '#submit' => array(
      'rules_form_submit_rebuild',
    ),
    '#ajax' => rules_ui_form_default_ajax('fade'),
    '#attributes' => array(
      'class' => array(
        'rules-hide-js',
      ),
    ),
  );

  // Use ajax and trigger as the reload button.
  $form['parameter']['type']['settings']['type']['#ajax'] = $form['reload']['#ajax'] + array(
    'event' => 'change',
    'trigger_as' => array(
      'name' => 'reload',
    ),
  );
  switch ($step) {
    case 1:
      $form['reload']['#limit_validation_errors'] = array(
        array(
          'parameter',
          'entity',
        ),
      );
      unset($form['parameter']['type']);
      unset($form['reload']['#attributes']['class']);

    // NO break.
    case 2:
      $form['negate']['#access'] = FALSE;
      unset($form['parameter']['bundle']);
      unset($form['submit']);
      break;
    case 3:
      if (($info = $element
        ->getArgumentInfo('entity')) && $info['type'] != 'entity') {

        // Hide the entity type parameter if not needed.
        unset($form['parameter']['type']);
      }
      break;
  }
}