2

I want to rebuild a Form Item with new property after Ajax triggered on page, I try following codes

<?php

use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;

function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {

    if ($form_id == 'node_trends_form' || $form_id == 'node_trends_edit_form') {
        $form['langcode']['widget'][0]['value']['#ajax'] = array(
            'callback' => 'MYMODULE_language_change',
             'wrapper' =>'edit-field-trends-wrapper',
            'event' => 'change',

        );

        $form['field_trends']['widget'][0]['value']['#autocomplete_route_parameters'] = array('lang'=>'test');
        $form['field_trends']['#prefix'] = '<div id="edit-field-trends-wrapper">';
        $form['field_trends']['#suffix'] = '</div>';
    }

}

/**
 * Ajax callback.
 */

function MYMODULE_language_change(array $form, FormStateInterface $form_state) {
        $form_state->setRebuild(TRUE);
            $form['field_trends']['widget'][0]['value']['#title'] = "New Title After Ajax";

        $form['field_trends']['widget'][0]['value']['#autocomplete_route_parameters'] = array('lang'=>'onajax');
        return $form['field_trends'];
}

Update : in order to find the problem I change the code to simplest manner, enter image description here

The Problem is when ajax triggered autocomplete replaced but changing on #autocomplete_route_parameters to applied !!!

Update 2 Strange things is I try to change field title on ajax callback handler it applied and after ajax new title is New Title After Ajax but autocomplete parameters not changed. I confused 😔😔

  • Do you want to change the html on the page? Because for that you will need to use a wrapper. – Eyal Dec 17 '16 at 21:53
  • Even if you don't want to change a thing on the page, you should try using a wrapper just to check if it helps. – Eyal Dec 17 '16 at 21:54
  • @Eyal No, I want to change #autocomplete_route_parameters of autocomplete field. it's not related to html markups – Yusef Dec 17 '16 at 22:17
  • The route parameters do affect the HTML output. They are used to create the autocomplete URL, used by the client side. – Eyal Dec 17 '16 at 22:29
  • @Eyal can you tell me how can I change It after language ajaxly changed? – Yusef Dec 18 '16 at 6:07
0

Finally I achieve it with following, Change autocomplete parameters prperty in form alter on rebuild event INSTEAD of changing property worked for me,

    use Drupal\Core\Form\FormStateInterface;
    use Symfony\Component\HttpFoundation\Request;

    function zeytondev_form_alter(&$form, FormStateInterface $form_state, $form_id) {

        if ($form_id == 'node_trends_form' || $form_id == 'node_trends_edit_form') {
            $form['langcode']['widget'][0]['value']['#ajax'] = array(
                'callback' => 'zeytondev_language_change',
                'wrapper' =>'mtrend-wrapper',
                'event' => 'change',

            );

            //kint( $form['field_trends']);
            $form['field_trends']['#prefix'] = '<div id="mtrend-wrapper">';
            $form['field_trends']['#suffix'] = '</div>';
            $langcode = $form_state->getValue('langcode');
            if($langcode) {
                $langcode = $form_state->getValue('langcode');
                $langcode = $langcode[0]['value'];
            }
            else {
                $langcode = 'en';
            }
            //var_dump($langcode);
            $form['field_trends']['widget'][0]['value']['#autocomplete_route_parameters'] = array('lang'=>$langcode);
        }

    }


    function zeytondev_language_change(array &$form, FormStateInterface $form_state) {
        $form_state->setRebuild(TRUE);
        return ($form['field_trends']);
    }
  • Yep. Anything modifying the form structure has to occur in the builder. Its tricky. – Kevin Dec 18 '16 at 22:37

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.