1

I'm trying to figure out how to trigger an element with a link in a form using trigger_as.

I tried the following snippet. The button ajax callback works, but the link doesn't. The link does seem to use the right name and value in trigger_as (as seen in the js console), but the post callback doesn't return anything.

$form['add_more'] = array(
  '#type' => 'submit',
  '#value' => t('Add another item'),
  '#attributes' => array('class' => array('add-more-submit')),
  '#submit' => array('field_add_more_submit'),
  '#name' => 'result_fields_add_more',
  '#ajax' => array(
    'callback' => '__update_fields',
    'wrapper' => 'example-wrapper',
    'effect' => 'fade',
  ),
);

$form['add_more_link'] = array(
  '#type' => 'link',
  '#title' => t('add'),
  '#href' => '#',
  '#process' => array('ajax_process_form'),
  '#ajax' => array(
    'callback' => '_update_fields',
    'wrapper' => 'example-wrapper',
    'effect' => 'fade',
    'event' => 'click',
    'trigger_as' => array(
      'name' => 'result_fields_add_more',
      'value' => t('Add another item'),
    ),
  ),
);

I tried to follow this example, but didn't work: http://drupal.org/node/753270#comment-2910362 I also didn't find an ajax link in the rules module, only a button with an ajax callback. I thought I followed the documentation correctly: http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#ajax_trigger_as

EDIT: One fix is to trigger the submit event through javascript code, but I was really hoping the form api would be able to handle this itself: https://drupal.stackexchange.com/a/34375

2 Answers 2

1

No support for links, but for what its worth heres a working snippet that uses a select.

When the select changes it triggers the submit buttons callback. Note, you still have to define the callback on the select box as ajax.inc assumes all ajax enabled elements have one.

<?php

  $submit_name = 'product-submit-' . $node->nid;
  $form['product_id'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#ajax' => array(
      'wrapper' => 'my-form-wrapper',
      'callback' => 'product_change_callback',
      'trigger_as' => array(
        'name' => $submit_name,
      ),
    ),
  );

  $form['submit'] = array(
    '#name' => $submit_name,
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#ajax' => array(
      'callback' => 'product_change_callback',
    ),
  );
?>

0

It's only working with form elements in Drupal 7 as of now, not with link elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.