0

The code below works inside hook_form_alter. The description and price update from database as desired when in form_alter. I moved the code to the callback and now it wont return/update the values. I use a button to call the ajax callback. The throbber shows and then no change. Partial of custom module, using a button for testing the callback

    if ($form_id=='webform_submission_srvform_add_form' && $form_state->isExecuted()) {
        $lkuppage='';
        $lkuppage=$form_state->get('current_page');
    switch ($lkuppage) {
        case 'billing':
            $form['elements']['billing']['items_update'] = [ 
                '#type' => 'button', '#value' => t('Update prices'),
                '#ajax' => [ 'callback' => 'srvform_items_callback', 'event' => 'click',
                'progress' => [ 'type' => 'throbber', 'message' => t('Updating line items...')] ] ];
            $form['elements']['billing']['desc01']['#prefix'] = '<div id="ajax-desc01">';
            $form['elements']['billing']['desc01']['#suffix'] = '</div>';

and the callback

function srvform_items_callback(array &$form, FormStateInterface $form_state) {
    $lkuppart='';
    $lkupok='yes';
    $node_storage = \Drupal::entityTypeManager()->getStorage('node');
    $lkuppart=$form['elements']['billing']['itemflex01']['part01']['#default_value'];
    if (!empty($lkuppart)) {
        $query = \Drupal::entityQuery('node') ->condition('type', 'parts') ->condition('title', $lkuppart);
        $nids = $query->execute();
        $lkupnodes = $node_storage->loadMultiple($nids);
        foreach ($lkupnodes as $nodecnt) {
            $lkuptitle=$nodecnt->title->value;
        }
        if ($lkuptitle <> $lkuppart) {
            $lkupok='false';
        } else {
            $form['elements']['billing']['desc01']['#default_value']=$nodecnt->body->value;
        }
        $lkuptitle='';
    }
    $lkupdesc = $nodecnt->body->value;
    $renderer = \Drupal::service('renderer');
    $response = new AjaxResponse();
    $response->addCommand(new HtmlCommand('#edit-desc01--value', $lkupdesc));
//    $response->addCommand(new \Drupal\Core\Ajax\HtmlCommand('[data-drupal-selector="edit-desc01"]', $lkupdesc));
//    $response->addCommand(new ReplaceCommand('#ajax-desc01', $renderer->render($form['elements']['billing']['desc01'])));
$response->addCommand(new AlertCommand(t($lkupdesc)));
    return $response;

Is it simply a bad/obsolete command I am using? The AlertCommand works, returns the part description in a window. Unable to replace the desc01 value with any of the above. syntax? or ?

  • Where do you attach the callback to the field? In other words, how are you telling Drupal that your callback function is srvform_items_callback()? – runswithscissors Nov 16 at 0:59
  • Can you please update with the code you have used to assign the callback. Also do check your browser developer toolbar / network tab to see what is the response you get from the server for the request you are sending from the form via ajax if the throbber is showing and there is an actual ajax request being sent. – anoopjohn Nov 16 at 2:41
  • also try changing ReplaceCommand with HtmlCommand so can happen more then once as replace ... removes the wrapper element where as HtmlCommand does not – Taggart Jensen Nov 17 at 22:17
  • Rewrote the code and tried many variations, unable to update description value – DStrider Nov 19 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.