Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying the Wagtail publish dropdown (per app)

Tags:

django

wagtail

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?

like image 957
shacker Avatar asked Jan 06 '17 01:01

shacker


4 Answers

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.

like image 103
gasman Avatar answered Nov 18 '22 11:11

gasman


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.

like image 24
Yannic Hamann Avatar answered Nov 18 '22 11:11

Yannic Hamann


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);
});
like image 2
Mikhail Gerasimov Avatar answered Nov 18 '22 12:11

Mikhail Gerasimov


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']");
like image 1
remort Avatar answered Nov 18 '22 11:11

remort