You are here

function relation_rules_fetch_endpoint_form_alter in Relation 8

Same name and namespace in other branches
  1. 8.2 relation.rules.inc \relation_rules_fetch_endpoint_form_alter()
  2. 7 relation.rules.inc \relation_rules_fetch_endpoint_form_alter()

Form alter callback for the fetch_endpoint action.

File

./relation.rules.inc, line 358
Implements the Rules module API for Relation.

Code

function relation_rules_fetch_endpoint_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  $first_step = empty($element->settings['relation:select']);
  $second_step = !$first_step && empty($element->settings['entity_type']);
  $form['reload'] = array(
    '#weight' => 5,
    '#type' => 'submit',
    '#name' => 'reload',
    '#value' => $first_step ? t('Continue') : t('Reload form'),
    '#limit_validation_errors' => array(
      array(
        'parameter',
        'relation',
      ),
    ),
    '#submit' => array(
      'rules_action_type_form_submit_rebuild',
    ),
    '#ajax' => rules_ui_form_default_ajax(),
    '#description' => $first_step ? '' : t('Reload the form to change the entity/bundle types list.'),
  );

  // Use ajax and trigger as the reload button.
  $form['parameter']['relation']['settings']['relation:select']['#ajax'] = $form['reload']['#ajax'] + array(
    'event' => 'blur',
    'trigger_as' => array(
      'name' => 'reload',
    ),
  );
  if ($first_step || $second_step) {

    // In the first step and second step only show relevant parameters.
    foreach (Element::children($form['parameter']) as $key) {
      if ($key != 'relation' && !($second_step && $key == 'entity_type')) {
        unset($form['parameter'][$key]);
      }
    }
    unset($form['submit']);
    unset($form['provides']);
  }
  else {

    // Change the entity parameter to be not editable.
    $form['parameter']['relation']['settings']['#access'] = FALSE;
    $form['parameter']['relation']['info'] = array(
      '#prefix' => '<p>',
      '#markup' => t('<strong>Selected relation:</strong> %selector', array(
        '%selector' => $element->settings['relation:select'],
      )),
      '#suffix' => '</p>',
    );

    // Hide the reload button in case js is enabled and it's not the first step.
    $form['reload']['#attributes'] = array(
      'class' => array(
        'rules-hide-js',
      ),
    );
  }

  // Add #ajax to the entity_type selection dropdown to reload the form.
  if (isset($form['parameter']['entity_type'])) {
    $form['parameter']['entity_type']['#ajax'] = rules_ui_form_default_ajax() + array(
      'event' => 'change',
      'trigger_as' => array(
        'name' => 'reload',
      ),
    );
  }

  // Add #ajax to the number parameter to allow us to change the type of the provided variable.
  if (isset($form['parameter']['number'])) {
    $form['parameter']['number']['#ajax'] = rules_ui_form_default_ajax() + array(
      'event' => 'change',
      'trigger_as' => array(
        'name' => 'reload',
      ),
    );
  }

  // Disable #ajax for the 'relation:select' as it has troubles with lazy-loaded JS.
  // @TODO: Re-enable once JS lazy-loading is fixed in core.
  unset($form['parameter']['relation']['settings']['relation:select']['#ajax']);
}