public function WebformTestAjaxBlock::build in Webform 6.x
Same name and namespace in other branches
- 8.5 tests/modules/webform_test_ajax/src/Plugin/Block/WebformTestAjaxBlock.php \Drupal\webform_test_ajax\Plugin\Block\WebformTestAjaxBlock::build()
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- tests/
modules/ webform_test_ajax/ src/ Plugin/ Block/ WebformTestAjaxBlock.php, line 50
Class
- WebformTestAjaxBlock
- Provides a 'webform_test_block_context' block.
Namespace
Drupal\webform_test_ajax\Plugin\BlockCode
public function build() {
$webforms = Webform::loadMultiple();
// Ajax links.
$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',
])) {
// Add destination to Ajax webform that don't redirect to confirmation
// page or URL.
$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',
],
],
];
}
// Inline links.
$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.
$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;
}