I was browsing the requests repo and saw a request from @adrian that reminded me of a PW feature I had forgotten existed: from Page List you can open a page for editing in a modal window by long-clicking on the Edit button.
This is quite handy for when you want to make a quick edit to a page. But as the request notes, it would speed things up if there was a "Save + Close" button in the modal.
Until the request is actioned in the core I thought I'd try implementing it in some custom code, and it's also an opportunity to show an easy way you can add custom JS and CSS to the ProcessWire admin.
The first step is to create the following files at /site/templates/admin-assets/admin-custom.js and /site/templates/admin-assets/admin-custom.css. The CSS is optional.
admin-custom.js
$(document).ready(function() {
const $body = $('body');
const is_modal = $body.hasClass('modal') || $body.hasClass('pw-iframe');
if($body.hasClass('ProcessPageEdit') && is_modal) {
const params = new URLSearchParams(window.location.search);
const $save_button = $('#submit_save');
if(params.has('quick_edit') && $save_button.length) {
$save_button.parent().append('<button type="submit" name="submit_save" value="Save" class="ui-button uk-hidden" id="save-and-close">Save + Close</button>');
}
}
if($body.hasClass('ProcessPageList') && !is_modal) {
$(document).on('ajaxComplete', function() {
$('.PageListActionEdit a:not([data-autoclose])').each(function() {
$(this).attr('data-autoclose', '#save-and-close').attr('href', $(this).attr('href') + '&quick_edit=1');
});
});
}
});
admin-custom.css
/* Avoid Tracy debugbar appearing on top of modal */
.ui-widget-overlay.ui-front { z-index:40000; }
.ui-dialog { z-index:40001; }
/* Place modal buttons on the left rather than the right */
.ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset { float:none; }
The second step is to add the following hook to /site/ready.php
One good thing about using a hook to AdminTheme::getExtraMarkup to add custom CSS is that the file gets loaded after any CSS files that are loaded by modules or the PW core, so you can override any CSS rules without needing to add extra specificity.
// Add custom JS and CSS to admin
$wire->addHookAfter('AdminTheme::getExtraMarkup', function(HookEvent $event) {
$parts = $event->return;
$config = $event->wire()->config;
// JS file should exist at /site/templates/admin-assets/admin-custom.js
$js_url = $config->urls->templates . 'admin-assets/admin-custom.js';
$modified = filemtime(rtrim($config->paths->root, '/') . $js_url);
$parts['head'] .= "<script type='text/javascript' src='$js_url?m=$modified'></script>";
// CSS file should exist at /site/templates/admin-assets/admin-custom.css
$css_url = $config->urls->templates . 'admin-assets/admin-custom.css';
$modified = filemtime(rtrim($config->paths->root, '/') . $css_url);
$parts['head'] .= "<link rel='stylesheet' href='$css_url?m=$modified'>";
$event->return = $parts;
});
The end result: