Ajax Confirm

Component ID

2778247

Component name

Ajax Confirm

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Downloads

239

Component created

Component changed

Component body

The Ajax Confirm module provides a way for the user to confirm a triggered ajax call through an jQuery Confirmation Dialog. The module consists only of a single JavaScript Library.

An ajax confirmation is useful when e.g. the user might trigger changes which are irreversible without having to reload the whole form such as the removal of elements.

The library is really easy to use and it allows for adding settings which have to be applied on the jQuery confirmation dialog.

1. You have to add a class attribute to the element, which is specifying the #ajax key. This class will be supplied through the drupal settings to the library to flag the ajax execution of the elements having this class for confirmation.
2. You have to include the library.

Example:

$element['remove'] = [
  '#type' => 'submit',
  '#value' => $this->t('Remove this item'),
  '#submit' => [[get_class($this), 'removeSubmit']],
  '#ajax' => [
    'callback' => [get_class($this), 'removeAjax'],
   ],
];

$class = 'confirm-remove';
$element['remove']['#attributes']['class'][] = $class;
$element['#attached']['library'][] = 'ajax_confirm/ajax-confirm';
$element['#attached']['drupalSettings']['ajaxConfirm'][$class] = [
  'title' => $this->t('Confirm element removal'),
  'text' => $this->t('Are you sure that you want to remove this element?'),
  'buttons' => [
    'button_confirm' => ['text' => $this->t('Yes')],
    'button_reject' => ['text' => $this->t('No')],
  ]
];