So I figured I'd create a configuration for my module. I created a normal form with some fields to let the users configure the module with variables. I also added a button which would do an ajax-call to a service. Problem is, this button is within the form, meaning it submits the form aswell.

$form['products']['update'] = array(
            '#type' => 'button',
            '#ajax' => array(
                'path' => '/products/update',
                'event' => 'click'
            '#value' => t('Update Products')

I just want the button to actually call this service (/products/update) and not act like a submit button. This worked in beta3 but is not an annoying feature in beta4, preventing the actual service call. How do I fix this?

  • Not an answer to the question, but converting the button to a styled link might be an easy solution – Clive Jan 15 '15 at 17:59
  • button types are not supposed to call the submit handlers. You will get the form values, but it shouldn't execute submit handlers. – AKS Jan 15 '15 at 18:10

ajax path is not supported anymore I think. It would need to be a route.

Why do you need a path? The common way to do this is to have a #submit callback on that form element, then just that will be called.

  • Well according to most tutorials out there setting up an AJAX-service in Drupal 8 is by using the path as a main-route and then the callback is there as a.. callback. That's the main reason why I used path this early. I will have to adapt, I suppose. – Undrium Jan 16 '15 at 6:50
  • I'm not sure where you've seen those tutorials, but many D8 tutorials are out of date anyway, so careful with those. You can see the properties for #ajax for example on RenderElement::preRenderAjaxForm(), and it has 'url', which is a Url object. But almost nothing in core uses that, the common case is to use the standard url, which is system/ajax, which takes care of processing your form, submitting it as defined and then calling the callback. – Berdir Jan 16 '15 at 8:35
  • api.drupal.org/api/drupal/core!modules!system!core.api.php/… I suppose this is outdated, but it can be confusing when path actually states being used for requests. Also since I am very used to setting up routes for services from other frameworks I thought this approach was the easiest. – Undrium Jan 16 '15 at 10:12
  • Is there any workaround my initial solution to just have a button use the route 'products/update' through an AJAX request? – Undrium Jan 16 '15 at 10:17
  • Yes, you could create a Url object with Url::fromRoute() or Url::fromUri() and assign that to 'url'. – Berdir Jan 16 '15 at 14:08

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.