turbo_spa-1.0.x-dev/turbo_spa.module
turbo_spa.module
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_page_attachments().
*/
function turbo_spa_page_attachments(array &$page) {
// @todo Don't add to admin pages.
$page['#attached']['library'][] = 'turbo_spa/loader';
// We need to disable snapshot cache on pages with forms to avoid
// re rendering of previous form validation/submission errors.
// NB snapshot cache clears when you reload a page!!
$disable_snapshot = ['entity.node.edit_form', 'node.add'];
// @todo on all form pages only
// pages with /add /edit /update ++ .
// no-preview means don't show previews for this page.
// no-cache tells turbo to not snapshot.
$current_route_name = Drupal::routeMatch()->getRouteName();
if (in_array($current_route_name, $disable_snapshot)) {
$metas = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'turbo-cache-control',
'content' => 'no-preview.',
],
];
$page['#attached']['html_head'][] = [$metas, 'turbo-cache-control'];
}
}
/**
* Implements hook_form_alter().
*/
function turbo_spa_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
// $form['#validate'][] = 'spa_validate_errors';
// Couldn't successfully set a redirect after form validation error
// on login form, disabling turbo on login form
if ($form_id === 'user_login_form') {
// Disables turbo drive for login form.
$form['#attributes']['data-turbo'][] = 'false';
}
}
