Skip to content

Instantly share code, notes, and snippets.

@kedramon
Last active December 23, 2020 22:55
Show Gist options
  • Save kedramon/f1ef05265524c5c57641e2eb8aece022 to your computer and use it in GitHub Desktop.
Save kedramon/f1ef05265524c5c57641e2eb8aece022 to your computer and use it in GitHub Desktop.
[Drupal8] Bootstrap + Ajax button = fill content, one call.
order.subscription:
path: '/subscription-test'
defaults:
_controller: '\Drupal\order\Controller\PinPukController::test'
_title: 'Testing button'
requirements:
_permission: 'access content'
custom_ajax_link.routing:
path: '/custom_ajax_link'
defaults:
_title: 'Custom Ajax Link'
_controller: '\Drupal\order\Controller\PinPukController::ajaxSample'
requirements:
_permission: 'access content'
<?php
namespace Drupal\order\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class DealerOrderController.
*
* @package dsp_dealer_order
*/
class PinPukController extends ControllerBase {
public function test() {
$button = [
'#type' => 'link',
'#title' => [
'icon',
[
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => $this->t('Simkaart'),
],
],
'#url' => Url::fromRoute('custom_ajax_link.routing', [], ['fragment' => 'customer-simcard-details']),
'#attributes' => [
'class' => ['use-ajax', 'btn', 'btn-primary', 'btn-block', 'simcart-ajax'],
'role' => 'button',
'data-toggle' => 'collapse',
'aria-controls' => 'customer-simcard-details',
],
];
$content['row'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => ['row'],
],
[
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => ['col-md-4'],
],
0 => $button,
],
];
$content['holder'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => [
'panel-bordered',
'panel-indication-right',
'fade',
'collapse',
'no-collapse',
],
'id' => 'customer-simcard-details',
'aria-expanded' => 'false',
],
[
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'id' => 'customer-simcard-details-content',
],
'#value' => 'bfdbbe',
]
];
return $content;
}
public function ajaxSample() {
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#customer-simcard-details-content', 'some markup'))
// Toggle collapsed area.
->addCommand(new InvokeCommand('#customer-simcard-details', 'collapse', ['toggle']))
// Remove event to prevent trigger ajax.
->addCommand(new InvokeCommand('.simcart-ajax', 'off', ['click']));
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment