0

So, I have a block that I render with the following code.

public function build() {
  $filterForm = \Drupal::formBuilder()->getForm('Drupal\smeno\Form\FilterForm');

  $renderArray['filterform'] = $filterForm;
  $renderArray['McRefund_data'] = $this->getMcRefund();

  return [
    '#theme' => 'Remboursement-sur-compte',
    '#McRefund_build_data' => $renderArray,
  ];
}

screenshot

As you can see within this block, I render a form and data (McRefund_build_data) which I put in a table(couple of Divs). The purpose of the form is to filter the data and put the response back in the block so that the block get rendered with the new filtered data. For this process I want to use AJAX to avoid the page from being reloaded.

I tried adding this on buildForm().

$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
  '#type' => 'submit',
  '#value' => $this->t('OK'),
  '#button_type' => 'primary',
  '#prefix' => '<div class="tc btn_pay relative">',
  '#suffix' => '</div></div>',
  '#attributes' => array(
    'class' => array('proximabold', 'tc', 'transition', 'no-underline', 'ph3', 'pv2', 'mb2', 'dib', 'white'),
  ),
  '#ajax' => [
    // 'callback' => 'Drupal\smeno\Controller\RemboursementsController::filterRefunds',
    'url' => Url::fromUserInput('/filterremboursement'),
    'event' => 'click',
    // 'wrapper' => 'edit-output',
    'progress' => [
      'type' => 'throbber',
      'message' => t('Verifying entry...'),
    ],
  ],
);

I added a route and a controller to handle the request and the response.

Route

filterRemboursement:
path: '/filterremboursement'
defaults:
  _controller: 'Drupal\smeno\Controller\RemboursementsController::filterRefunds'
  _title: 'filterRemboursement'
methods:  [POST]
requirements:
  _access: 'TRUE'

Controller

function filterRefunds() {
  ddl('HELLO WORLD'); //devel module debug messages (usefull for ajax requests)
}

This isn't working, though.

2
  • Why not use the Form API to handle the ajax request and reponse?
    – 4k4
    Nov 11 '17 at 15:26
  • Can yu elaborate that more @4k4 Nov 11 '17 at 15:48
1

Remove the data table from the block

public function build() {
    $filterForm = \Drupal::formBuilder()->getForm('Drupal\smeno\Form\FilterForm');

    $renderArray['filterform'] = $filterForm;

    return [
       '#theme' => 'Remboursement-sur-compte',
       '#McRefund_build_data' => $renderArray,
    ];
}

and add it to the form (placed in a wrapper):

  $form['McRefund_data'] = [
    '#type' => 'container',
    '#attributes' => ['id' => 'table-wrapper'],
    'table' => $this->getMcRefund(),
  ];

Attach this ajax callback to a form element

          '#ajax' => [
            'event' => 'click',
            'callback' => '::updateTable',
            'wrapper' => 'table-wrapper',
            'progress' => [
              'type' => 'throbber',
              'message' => t('Verifying entry...'),
            ],
          ],

and replace the table in the ajax callback:

  public function updateTable(array $form, FormStateInterface $form_state) {
    return $form['McRefund_data'];
  }
4
  • the container part, gives me this error: "0" is an invalid render array key in Drupal\Core\Render\Element::children() the "getMcRefund()" function is a function the send request to an api and get back Json. PS: when i say a table, it's just a couple of DIVs formatted in the shape of a table Nov 11 '17 at 17:23
  • And also @4k4, can i use the table that holds the getMcRefund() results in the template? Nov 11 '17 at 17:29
  • You can return anything in getMcRefund() that is renderable, also a template in #theme.
    – 4k4
    Nov 11 '17 at 17:36
  • does anybody have any idea how can i update the $form['McRefund_data'] element, the table argument within holds many variables and i use the form element in the twig as follow: {% for Num_refund,refund in McRefund_build_data.filterform.McRefund_data['table']['#Refunds_data'] %} ..... {% endfor %}. i want all the values in the loop to be updated with the updateTable function. Nov 14 '17 at 22:35

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.