wagtail / wagtail-autocomplete

An Autocomplete edit handler for selecting Pages, Snippets, and more.
https://wagtail-autocomplete.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
120 stars 55 forks source link

[SOLVED] How to add edit link to item button/label? #176

Closed ordigital closed 10 months ago

ordigital commented 10 months ago
    def autocomplete_label(self):
        edit_url = reverse('wagtailadmin_pages:edit', args=[self.id])
        return f('<a href="{edit_url}">{self.title}</a>')

This results in escaped characters instead of a working link to edit item. How to add working link to button/label?

ordigital commented 10 months ago

Changing label is no way to go. I had to modify sources to achieve this. Here is a solution:

  1. In views.py we take edit link to edit variable:

    def render_page(page):
    if getattr(page, 'specific', None):
        # For support of non-Page models like Snippets.
        page = page.specific
    if callable(getattr(page, 'autocomplete_label', None)):
        title = page.autocomplete_label()
    else:
        title = page.title
    edit = reverse('wagtailadmin_pages:edit', args=[page.id])
    return dict(pk=page.pk, title=title, edit=edit)
  2. In dist.js we look for this code (prettify first!):

    return e.createElement("div", {
                                key: n.pk,
                                className: ee("selection")
                            }, e.createElement("span", {
                                className: ee("selection__label")
                            }, n.title), e.createElement("button", {
                                type: "button",
                                className: ee("selection__button"),
                                onClick: t.handleRemove.bind(t, n)
                            }, e.createElement(k, {
                                className: ee("selection__icon")
                            }), e.createElement("span", {
                                className: ee("sr-only")
                            }, "Remove")))
                        }))))

    and make it look like this:

    return e.createElement("div", {
                            key: n.pk,
                            className: ee("selection")
                        }, e.createElement("span", {
                            className: ee("selection__label")
                        }, n.title),
    
                        e.createElement("div", {
                            className: ee("selection__controls")
                        },
    
                        // edit icon here
                        e.createElement("a", {
                            href: n.edit,
                            target: '__blank',
                            className: ee("edit__button")},
                            e.createElement("svg", {
                                className: ("icon icon-edit icon--item-action")},
                                e.createElement("use", { href: '#icon-edit'}),
                        )),
    
                        e.createElement("button", {
                            type: "button",
                            className: ee("selection__button"),
                            onClick: t.handleRemove.bind(t, n)
                        }, e.createElement(k, {
                            className: ee("selection__icon")
                        }), e.createElement("span", {
                            className: ee("sr-only")
                        }, "Remove"))))
                    }))))
  3. Now we need to uninstall wagtail-autocomplete from pip and put our wagtailautocomplete folder with modified files directly to our app folder, where we have home, search etc. so it can be found by Wagtail.