Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promo component: admin URL field #139

Open
chrimesdev opened this issue Aug 12, 2019 · 3 comments
Open

Promo component: admin URL field #139

chrimesdev opened this issue Aug 12, 2019 · 3 comments

Comments

@chrimesdev
Copy link
Member

chrimesdev commented Aug 12, 2019

For the URL field of the Promo component, I have to manually enter a full relative URL into the field, I cannot enter /frontend-library as I get an error saying Enter a valid URL. Entering a relative URL would cause problems on staging environments vs live environments (all URLs will be pointing to the live website when on the staging environment).

Would it also be possible to have an option to use the native Wagtail page chooser? The page I want to link to exists within Wagtail CMS, but I only have the option to enter a full URL. This would help if the page ever got renamed or moved.

Attached is a screenshot of the page chooser available in a regular DraftTail content block. I can link to both internal pages and external pages.

Screenshot 2019-08-12 at 17 03 56

This potentially could apply to more than the Promo component.

@chrimesdev
Copy link
Member Author

Spoke to the legend that is @damonjagger

https://github.com/nhsuk/wagtail-nhsuk-frontend/blob/master/wagtailnhsukfrontend/blocks.py#L161

    url = URLBlock(label="URL", required=True)

It currently uses a URLBlock, which is good for external links. If you use a PageChooserBlock, you'll be able to do what you want for internal links (and they are much better than URLBlocks for internal content, since they automatically update when you move/delete/update the page/slug.

http://docs.wagtail.io/en/v2.0/topics/streamfield.html#pagechooserblock

You could either specify whether it's an Internal Promo, or External Promo, and overwrite the url = <BlockType>. Or you could do something like have two fields in the block for Internal link or External link, and just validate that one exists on block creation you should never use URLBlocks for internal content, page chooser way better, prevents dead links a lot better.

@damonjagger
Copy link
Contributor

I've not been working much with Wagtail the past couple of months so you might know better.

I know we had the same requirement Adam has for a project we were working on recently and I think we extended the template/block to add this functionality. We rarely used promos with external urls, but maybe thats a misuse of the component.

@mikemonteith
Copy link
Contributor

mikemonteith commented Aug 15, 2019

We've done this sort of thing before on the NHS Website:

Define two blocks, a page chooser and a URL block.
Write a custom clean method to validate that either one has value but not both.

class FooBlock(blocks.StructBlock):
    ...
    page = blocks.PageChooserBlock(required=False)
    url = blocks.URLBlock(required=False)

    def clean(self, value):
        result = super().clean(value)
        errors = {}
        if value["page"] and value["url"]:
            errors["url"] = ErrorList([
                "This should not be set when Page is selected",
            ])
        if not value["page"] and not value["url"]:
            errors["page"] = ErrorList(["A page or URL is required"])
        if errors:
            raise ValidationError("Validation error in StructBlock", params=errors)
        return result

The issue with this is a clunky UI. It would be really nice if we could get the same UI as the drafttail link picker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants