Modifying the Wagtail publish dropdown (per app)
Asked Answered
S

4

6

I'd like to reconfigure the default "Publish" menu. The default configuration is this:

Default publish menu

I'd like to make Publish the default action, and move it to the top. I'd also like to remove Submit for Moderation, as our site has no current need for that feature.

Ideally, I'd love to be able to override the menu config on a per-app basis - we will likely have other sections of our site in the future where we want a different config.

Is this possible?

Shelbyshelden answered 6/1, 2017 at 1:43 Comment(0)
P
5

This isn't currently possible I'm afraid - the menu items are fixed in wagtailadmin/pages/create.html and edit.html.

This is possible as of Wagtail 2.4 using the register_page_action_menu_item hook, as per Yannic Hamann's answer. Additionally, Wagtail 2.7 (not released at time of writing) provides a construct_page_listing_buttons hook for modifying existing options.

Permissible answered 6/1, 2017 at 14:46 Comment(1)
presumably you could copy and override those templates? as in docs.wagtail.io/en/v1.11.1/advanced_topics/customisation/…Marpet
S
5

You can add a new item to the action menu by registering a custom menu item with the help of wagtail hooks. To do so create a file named wagtail_hooks.py within any of your existing Django apps.

from wagtail.core import hooks
from wagtail.admin.action_menu import ActionMenuItem

class GuacamoleMenuItem(ActionMenuItem):
    label = "Guacamole"

    def get_url(self, request, context):
        return "https://www.youtube.com/watch?v=dNJdJIwCF_Y"


@hooks.register('register_page_action_menu_item')
def register_guacamole_menu_item():
    return GuacamoleMenuItem(order=10)

Source

If you want to remove an existing menu item:

@hooks.register('construct_page_action_menu')
def remove_submit_to_moderator_option(menu_items, request, context):
    menu_items[:] = [item for item in menu_items if item.name != 'action-submit']

The default button SAVE DRAFT is still hardcoded and therefore cannot be configured so easily. See here.

Shelbashelbi answered 18/3, 2019 at 10:35 Comment(0)
D
2

It seems it can't be done on server side without some monkey-patching.

However if you want it for yourself (or have access to computers of those who will publish) you can modify your browser instead.

  1. Install Tampermonkey browser addon
  2. Create new script with a content below
  3. Change http://127.0.0.1:8000/admin/* to your wagtail admin panel url pattern
  4. Save script and check admin panel

Result should look:

enter image description here


// ==UserScript==
// @name     Wagtail: replace "Save draft" with "Publish"
// @match    *://127.0.0.1:8000/admin/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==


let $ = window.jQuery;


function modify() {
    let draft = $("button.button-longrunning:contains('Save draft')");
    let publish = $("button.button-longrunning:contains('Publish')");

    if (draft.length && publish.length) {
        swap(publish, draft);
    }
};


function swap(a, b) {
    a = $(a); b = $(b);
    var tmp = $('<span>').hide();
    a.before(tmp);
    b.before(a);
    tmp.replaceWith(b);
};


$(document).ready(function() {
    setTimeout(function() {
        try {
            modify();
        }
        catch (e) {
            console.error(e, e.stack);
        }
    }, 100);
});
Divulgence answered 31/3, 2019 at 5:17 Comment(1)
you could also put your script in a file and let it load through the insert_editor_js hook. That way all users, even those without Tampermonkey installed, can profit.Thaxter
P
1

Modifying the code above, these selectors works for every admin language:

let draft = $("button.button-longrunning.action-save");
let publish = $("button.button-longrunning[name='action-publish']");
Puppis answered 11/4, 2019 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.