lupus_decoupled-1.x-dev/modules/lupus_decoupled_responsive_preview/lupus_decoupled_responsive_preview.module
modules/lupus_decoupled_responsive_preview/lupus_decoupled_responsive_preview.module
<?php
/**
* @file
* General functions and hooks for Lupus Decoupled Responsive Preview.
*/
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements hook_library_info_alter().
*/
function lupus_decoupled_responsive_preview_library_info_alter(&$libraries, $extension) {
if ($extension == 'responsive_preview' && !empty($libraries['drupal.responsive-preview'])) {
$module_path = \Drupal::service('extension.list.module')->getPath('lupus_decoupled_responsive_preview');
$libraries['drupal.responsive-preview']['js']['/' . $module_path . '/js/lupus_decoupled_responsive_preview.js'] = [];
}
}
/**
* Implements hook_toolbar().
*/
function lupus_decoupled_responsive_preview_toolbar_alter(&$items) {
if (isset($items['responsive_preview'])) {
$responsivePreviewService = \Drupal::service('responsive_preview');
$preview_url = $responsivePreviewService->getPreviewUrl() ?? '/';
// ResponsivePreview::previewToolbar() sets an absolute path in the
// drupalSettings 'url' value. It can however be overwritten to a relative
// path, in which case our JS needs to prepend the front end base URL again.
// Pass it in a separate setting.
// @see \Drupal\responsive_preview\ResponsivePreview::handleAjaxDevicePreview()
if (UrlHelper::isExternal($preview_url)) {
$parse = parse_url($preview_url);
$frontend_base_url = ($parse['scheme'] ?? 'https') . '://'
. $parse['host'] . (isset($parse['port']) ? ':' . $parse['port'] : '');
$items['responsive_preview']['#attached']['drupalSettings']['lupus_decoupled_frontend_url'] = $frontend_base_url;
}
else {
$items['responsive_preview']['#attached']['drupalSettings']['lupus_decoupled_frontend_url'] = \Drupal::service('lupus_decoupled_ce_api.base_url_provider')
->getFrontendBaseUrl();
$preview_url = 'internal:' . $preview_url;
}
// Add a preview link below the devices list, on the layout page. (The
// 'regular node preview link' shouldn't be shared like this, because the
// preview is only up-to-date after updating the node's state through a
// POST request.)
if (\Drupal::service('current_route_match')->getRouteName() === 'layout_builder.overrides.node.view') {
$current_user = \Drupal::currentUser();
$items['responsive_preview']['tab']['device_options']['#items']['preview_link'] = [
'#type' => 'link',
'#title' => t('Preview link'),
'#url' => Url::fromUri($preview_url),
'#access' => $current_user->hasPermission('access responsive preview'),
'#attributes' => [
'class' => ['responsive-preview-preview-link', 'responsive-preview-configure'],
],
];
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for seckit module settings form.
*/
function lupus_decoupled_responsive_preview_form_seckit_settings_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
// Disable frame-ancestors directive, because it is handled by the
// ContentSecurityEventSubscriber.
$form['seckit_xss']['csp']['frame-ancestors']['#disabled'] = TRUE;
$form['seckit_xss']['csp']['frame-ancestors']['#default_value'] = '';
$form['seckit_xss']['csp']['frame-ancestors']['#description'] = t('This directive is handled by Lupus Decoupled Response Preview module.');
$form['seckit_xss']['csp']['frame-src']['#disabled'] = TRUE;
$form['seckit_xss']['csp']['frame-src']['#default_value'] = '';
$form['seckit_xss']['csp']['frame-src']['#description'] = t('This directive is handled by Lupus Decoupled Response Preview module.');
}
