Skip to content
Snippets Groups Projects
Commit c69a6575 authored by Sven Decabooter's avatar Sven Decabooter
Browse files

Issue #3511105 by svendecabooter: Use OOP hook implementations

parent deece0bc
No related branches found
No related tags found
1 merge request!21Issue #3511105 by svendecabooter: Use OOP hook implementations
Pipeline #440658 passed
......@@ -6,28 +6,23 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_builder_iframe_modal\Controller\RebuildController;
use Drupal\Core\Hook\Attribute\LegacyHook;
use Drupal\layout_builder_iframe_modal\Hook\FormHooks;
use Drupal\layout_builder_iframe_modal\Hook\ThemeHooks;
use Drupal\layout_builder_iframe_modal\Hook\LinkHooks;
/**
* Implements hook_theme().
*/
#[LegacyHook]
function layout_builder_iframe_modal_theme() {
return [
'lbim_iframe' => [
'variables' => [
'iframe_attributes' => NULL,
],
],
'lbim_redirect' => [
'variables' => [],
],
];
return \Drupal::service(ThemeHooks::class)->theme();
}
/**
* Implements hook_preprocess_HOOK().
*/
function layout_builder_iframe_modal_preprocess_html(&$variables) {
function layout_builder_iframe_modal_preprocess_html(&$variables): void {
$route_name = \Drupal::routeMatch()->getRouteName();
if (!empty($route_name)) {
......@@ -46,90 +41,23 @@ function layout_builder_iframe_modal_preprocess_html(&$variables) {
/**
* Implements hook_contextual_links_alter().
*/
function layout_builder_iframe_modal_contextual_links_alter(array &$links, $group, array $route_parameters) {
foreach ($links as &$link) {
if (isset($link['route_name'])
&& !empty($link['localized_options']['attributes']['data-dialog-type'])
&& \Drupal::service('layout_builder_iframe_modal.helper')->isModalRoute($link['route_name'])) {
$link['localized_options']['attributes']['data-dialog-type'] = 'iframe';
unset($link['localized_options']['attributes']['data-dialog-renderer']);
}
}
#[LegacyHook]
function layout_builder_iframe_modal_contextual_links_alter(array &$links, $group, array $route_parameters): void {
\Drupal::service(LinkHooks::class)->contextualLinksAlter($links, $group, $route_parameters);
}
/**
* Implements hook_link_alter().
*/
function layout_builder_iframe_modal_link_alter(&$variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
// Only change links on routes.
if ($route_name === NULL) {
return;
}
// Only change links that open in a dialog.
if (empty($variables['options']['attributes']['data-dialog-type'])) {
return;
}
/** @var Drupal\Core\Url $url */
$url = $variables['url'];
if (!$url || !$url->isRouted()) {
return;
}
$link_route_name = $url->getRouteName();
if (\Drupal::service('layout_builder_iframe_modal.helper')->isModalRoute($link_route_name)) {
$variables['options']['attributes']['data-dialog-type'] = 'iframe';
unset($variables['options']['attributes']['data-dialog-renderer']);
}
#[LegacyHook]
function layout_builder_iframe_modal_link_alter(&$variables): void {
\Drupal::service(LinkHooks::class)->linkAlter($variables);
}
/**
* Implements hook_form_alter().
*/
#[LegacyHook]
function layout_builder_iframe_modal_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// \Drupal\layout_builder\Form\ConfigureBlockFormBase::doBuildForm sets an
// ajax handler on the main submit action for the form whenever any element
// on the form is rebuilt using ajax. This is then cached and a subsequent
// form submission can be processed as ajax even when we don't want that.
// This module never wants the submit action to be ajax,
// so we ensure that. See
// https://www.drupal.org/project/layout_builder_iframe_modal/issues/3202523
$modalHelper = \Drupal::service('layout_builder_iframe_modal.helper');
if (($modalHelper->isModalRoute('layout_builder.add_block') && $form_id == 'layout_builder_add_block') || ($modalHelper->isModalRoute('layout_builder.update_block') && $form_id == 'layout_builder_update_block')) {
if (!empty($form['actions']['submit']['#ajax'])) {
unset($form['actions']['submit']['#ajax']);
}
}
if (strpos($form_id, 'layout_builder_form') === FALSE) {
return;
}
/** @var \Drupal\layout_builder\SectionStorageInterface $section_storage */
$section_storage = \Drupal::routeMatch()->getParameter('section_storage');
if (!$section_storage) {
return;
}
$is_translation = isset($form['layout_builder__translation']['widget']);
$url = RebuildController::buildRouteUrl($section_storage, $is_translation);
$url->setOption('attributes', [
'class' => ['use-ajax', 'button'],
'data-disable-refocus' => 'true',
]);
$form['actions']['rebuild-layout'] = [
'#type' => 'link',
'#weight' => 16,
'#title' => t('Rebuild layout'),
'#url' => $url,
'#attributes' => [
'class' => ['hidden'],
],
];
\Drupal::service(FormHooks::class)->formAlter($form, $form_state, $form_id);
}
services:
main_content_renderer.iframe:
class: Drupal\layout_builder_iframe_modal\Render\MainContent\IframeRenderer
arguments: ['@title_resolver', '@renderer']
autowire: true
tags:
- { name: render.main_content_renderer, format: drupal_iframe }
layout_builder_iframe_modal.helper:
class: Drupal\layout_builder_iframe_modal\IframeModalHelper
arguments: ['@config.factory']
autowire: true
Drupal\layout_builder_iframe_modal\IframeModalHelper: '@layout_builder_iframe_modal.helper'
Drupal\layout_builder_iframe_modal\Hook\FormHooks:
class: Drupal\layout_builder_iframe_modal\Hook\FormHooks
autowire: true
Drupal\layout_builder_iframe_modal\Hook\LinkHooks:
class: Drupal\layout_builder_iframe_modal\Hook\LinkHooks
autowire: true
Drupal\layout_builder_iframe_modal\Hook\ThemeHooks:
class: Drupal\layout_builder_iframe_modal\Hook\ThemeHooks
autowire: true
<?php
declare (strict_types=1);
namespace Drupal\layout_builder_iframe_modal\Hook;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\layout_builder_iframe_modal\Controller\RebuildController;
use Drupal\layout_builder_iframe_modal\IframeModalHelper;
/**
* Form-related hook implementations for Layout Builder Iframe Modal.
*/
class FormHooks {
use StringTranslationTrait;
/**
* Constructs a new FormHooks instance.
*
* @param \Drupal\layout_builder_iframe_modal\IframeModalHelper $iframeModalHelper
* The iframe modal helper service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The current route match.
*/
public function __construct(
protected IframeModalHelper $iframeModalHelper,
protected RouteMatchInterface $routeMatch,
) {}
/**
* Implements hook_form_alter().
*/
#[Hook('form_alter')]
public function formAlter(array &$form, FormStateInterface $form_state, string $form_id): void {
// \Drupal\layout_builder\Form\ConfigureBlockFormBase::doBuildForm sets an
// ajax handler on the main submit action for the form whenever any element
// on the form is rebuilt using ajax. This is then cached and a subsequent
// form submission can be processed as ajax even when we don't want that.
// This module never wants the submit action to be ajax,
// so we ensure that. See
// https://www.drupal.org/project/layout_builder_iframe_modal/issues/3202523
if (($this->iframeModalHelper->isModalRoute('layout_builder.add_block') && $form_id == 'layout_builder_add_block') || ($this->iframeModalHelper->isModalRoute('layout_builder.update_block') && $form_id == 'layout_builder_update_block')) {
if (!empty($form['actions']['submit']['#ajax'])) {
unset($form['actions']['submit']['#ajax']);
}
}
if (!str_contains($form_id, 'layout_builder_form')) {
return;
}
/** @var \Drupal\layout_builder\SectionStorageInterface $section_storage */
$section_storage = $this->routeMatch->getParameter('section_storage');
if (!$section_storage) {
return;
}
$is_translation = isset($form['layout_builder__translation']['widget']);
$url = RebuildController::buildRouteUrl($section_storage, $is_translation);
$url->setOption('attributes', [
'class' => ['use-ajax', 'button'],
'data-disable-refocus' => 'true',
]);
$form['actions']['rebuild-layout'] = [
'#type' => 'link',
'#weight' => 16,
'#title' => $this->t('Rebuild layout'),
'#url' => $url,
'#attributes' => [
'class' => ['hidden'],
],
];
}
}
<?php
declare (strict_types=1);
namespace Drupal\layout_builder_iframe_modal\Hook;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\layout_builder_iframe_modal\IframeModalHelper;
/**
* Link-related hook implementations for Layout Builder Iframe Modal.
*/
class LinkHooks {
/**
* Constructs a new LinkHooks instance.
*
* @param \Drupal\layout_builder_iframe_modal\IframeModalHelper $iframeModalHelper
* The iframe modal helper service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The current route match.
*/
public function __construct(
protected IframeModalHelper $iframeModalHelper,
protected RouteMatchInterface $routeMatch,
) {}
/**
* Implements hook_contextual_links_alter().
*/
#[Hook('contextual_links_alter')]
public function contextualLinksAlter(array &$links, $group, array $route_parameters): void {
foreach ($links as &$link) {
if (isset($link['route_name'])
&& !empty($link['localized_options']['attributes']['data-dialog-type'])
&& $this->iframeModalHelper->isModalRoute($link['route_name'])) {
$link['localized_options']['attributes']['data-dialog-type'] = 'iframe';
unset($link['localized_options']['attributes']['data-dialog-renderer']);
}
}
}
/**
* Implements hook_link_alter().
*/
#[Hook('link_alter')]
public function linkAlter(&$variables): void {
$route_name = $this->routeMatch->getRouteName();
// Only change links on routes.
if ($route_name === NULL) {
return;
}
// Only change links that open in a dialog.
if (empty($variables['options']['attributes']['data-dialog-type'])) {
return;
}
/** @var \Drupal\Core\Url $url */
$url = $variables['url'];
if (!$url || !$url->isRouted()) {
return;
}
$link_route_name = $url->getRouteName();
if ($this->iframeModalHelper->isModalRoute($link_route_name)) {
$variables['options']['attributes']['data-dialog-type'] = 'iframe';
unset($variables['options']['attributes']['data-dialog-renderer']);
}
}
}
<?php
declare (strict_types=1);
namespace Drupal\layout_builder_iframe_modal\Hook;
use Drupal\Core\Hook\Attribute\Hook;
/**
* Theme hook implementations for Layout Builder Iframe Modal.
*/
class ThemeHooks {
/**
* Implements hook_theme().
*/
#[Hook('theme')]
public function theme(): array {
return [
'lbim_iframe' => [
'variables' => [
'iframe_attributes' => NULL,
],
],
'lbim_redirect' => [
'variables' => [],
],
];
}
}
......@@ -7,12 +7,12 @@ namespace Drupal\layout_builder_iframe_modal\Render\MainContent;
use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\layout_builder_iframe_modal\Ajax\OpenIframeCommand;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Render\MainContent\DialogRenderer;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\layout_builder_iframe_modal\Ajax\OpenIframeCommand;
use Symfony\Component\HttpFoundation\Request;
/**
* Renders the requested page inside an iframe.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment