View source
<?php
namespace Drupal\webform_test_ajax\Plugin\Block;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Url;
use Drupal\webform\Entity\Webform;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformTestAjaxBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $configFactory;
protected $redirectDestination;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, RedirectDestinationInterface $redirect_destination) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->redirectDestination = $redirect_destination;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('redirect.destination'));
}
public function build() {
$webforms = Webform::loadMultiple();
$ajax_links = [];
foreach ($webforms as $webform_id => $webform) {
if (strpos($webform_id, 'test_ajax') !== 0 && $webform_id !== 'test_form_wizard_long_100') {
continue;
}
if (!in_array($webform_id, [
'test_ajax_confirmation_page',
'test_ajax_confirmation_url',
'test_ajax_confirmation_url_msg',
])) {
$route_options = [
'query' => $this->redirectDestination
->getAsArray(),
];
}
else {
$route_options = [];
}
$ajax_links[$webform_id] = [
'title' => $this
->t('Open @webform_id', [
'@webform_id' => $webform_id,
]),
'url' => $webform
->toUrl('canonical', $route_options),
'attributes' => [
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 800,
]),
'class' => [
'use-ajax',
],
],
];
}
$webform = Webform::load('contact');
$inline_links = [];
$inline_links['webform'] = [
'title' => $this
->t('Open Contact'),
'url' => $webform
->toUrl('canonical'),
'attributes' => [
'class' => [
'webform-dialog',
'webform-dialog-normal',
],
],
];
$inline_links['source_entity'] = [
'title' => $this
->t('Open Contact with Source Entity'),
'url' => $webform
->toUrl('canonical', [
'query' => [
'source_entity_type' => 'ENTITY_TYPE',
'source_entity_id' => 'ENTITY_ID',
],
]),
'attributes' => [
'class' => [
'webform-dialog',
'webform-dialog-normal',
],
],
];
$inline_links['javascript'] = [
'title' => "Drupal.webformOpenDialog('" . $webform
->toUrl('canonical')
->toString() . "', 'webform-dialog-normal'); return false;",
'url' => Url::fromRoute('<none>'),
'attributes' => [
'onclick' => "Drupal.webformOpenDialog('" . $webform
->toUrl('canonical')
->toString() . "', 'webform-dialog-normal'); return false;",
],
];
$dialog_links = [];
$webform_style_guide = Webform::load('example_style_guide');
if ($webform_style_guide) {
$dialog_links['style_guide'] = [
'title' => $this
->t('Open style guide'),
'url' => $webform_style_guide
->toUrl('canonical'),
'attributes' => [
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
'data-dialog-options' => Json::encode([
'width' => 600,
'dialogClass' => 'ui-dialog-off-canvas webform-off-canvas',
]),
'class' => [
'use-ajax',
],
],
];
}
$build = [];
if ($ajax_links) {
$build['ajax'] = [
'#prefix' => '<h3>' . $this
->t('Ajax links') . '</h3>',
'#theme' => 'links',
'#links' => $ajax_links,
];
}
if ($inline_links) {
$build['inline'] = [
'#prefix' => '<h3>' . $this
->t('Inline (Global) links') . '</h3>',
'#theme' => 'links',
'#links' => $inline_links,
];
}
if ($dialog_links) {
$build['dialog'] = [
'#prefix' => '<h3>' . $this
->t('Dialog/Offcanvas links') . '</h3>',
'#theme' => 'links',
'#links' => $dialog_links,
];
}
$build['#attached']['library'][] = 'webform/webform.dialog';
$build['#attached']['drupalSettings']['webform']['dialog']['options'] = $this->configFactory
->get('webform.settings')
->get('settings.dialog_options');
return $build;
}
public function getCacheMaxAge() {
return 0;
}
}