Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Nested docs, parents from another collection

default discord avatar
clement6396last year
30

Hi guys, I used nested docs plugin with my

pages

collection, it's working fine, but now I wanna use it with other collections to be able to customize and translate all part of my website url.



I try to implement the same logic on my

articles

collection, but If I select a parent from another collection than

articles

, it doesn't works. The

docs param

of my

generateURL

function is not filled by

pages document relation

.



My goal is to be able to target

pages

parent from

articles

collection.



Here is my plugin config:

  • default discord avatar
    janpeinilast year

    I guess it will not be possible with this plugin. As I understand it, it only works within the realms of one collection.



    But could you probably use your own relation field for this?



    So using nested docs for the "internal" hierarachy and a relation to link between collections

  • default discord avatar
    clement6396last year

    Hi, I can target a

    pages

    parent from

    articles

    already with the plugin, using a special function. But the data of this parent page is not retrieved and pushed to the

    docs

    array.

  • default discord avatar
    janpeinilast year

    But just to be sure to understand you right: You want e.g. an article to be the child of a page?



    Frontend URL like this? /page-slug/article-slug/ ?

  • default discord avatar
    clement6396last year

    exactly, and for others collections later

  • default discord avatar
    janpeinilast year

    so like "categories" behave in wordpress and co?

  • default discord avatar
    clement6396last year

    I think so, I never used WP. πŸ™ˆ

  • default discord avatar
    janpeinilast year

    but you also want stuff like this:


    /page1-slug/page2-slug/article-slug/


    /page1-slug/article1-slug/article2-slug


    ?



    Because if not and you only got one level you probably dont need the nested docs plugin at all. Then it would be kinda easy with simple direct realtions?!

  • default discord avatar
    clement6396last year

    actually, in my Next.js, I'm blocked with static

    collections

    . With this plugins, I could create only one dynamic route to catch all routes, and be able to translate all url


    fr: /produits/livres-32


    en: /products/book-32

  • default discord avatar
    janpeinilast year

    I handle this with a relation to a "category" collection



    the category collection hast a name and slug, which is both localized. With this info I create the localized URLS

  • default discord avatar
    clement6396last year

    yep it would be great, to have this granular control

  • default discord avatar
    janpeinilast year

    You could also add the nested docs plugins to each collection, pages, articles and categories (which than determains the base)



    So all of them can be nested and you get the info from the breadcrumb field. On top of this you can interconnect the items with a relation field



    With this you could create any kind of depth.

  • default discord avatar
    clement6396last year

    Yep, that I want to do πŸ‘


    But it not seems working with the plugin actually :/

  • default discord avatar
    janpeinilast year

    Yeah, you need a seperate relation field for this



    for the interconnection



    For me it makes total sense, that the nested docs plugin only works within a given collection.



    But perhaps

    @808734492645785600

    has some more ideas here πŸ™‚

  • default discord avatar
    clement6396last year

    If create my own relation field for

    articles

    and target

    pages collection

    . I need to retrieve manually the

    parent

    field of this page.


    If the plugin can push all kind of collections, I don't need to do this kind of stuff, and avoid having two types of logic for the same objective πŸ˜…



    I see another guy has the same needs:

    https://github.com/payloadcms/plugin-nested-docs/discussions/22
  • default discord avatar
    janpeinilast year

    Yeah, and as expected, the plugin is not designed for this usecase.


    But shouldnt it be kinda simple with the relationship? At least for the "base folder" logic. I guess the url would be constructed something like this, which would be easy to determine in the frontend and run the corresponding checks.



    /{doc.relationToPage.slug}/{doc.breadcrumbs[-1].url}

    with that you could build:


    /page-name/article-structure/with-more-depth/

    totally localized



    and you dont need to manually retrieve the parent field. It will be directly provided via your API call to the article which has the relation



    The API response should look like this:


    {
        "id": "64c293e2ed24462353dcbe33",
        "title": "Article Title",
        "slug": "article-title",
        "pageRelation": { // your own relation
            "id": "649fc774ddd5fbb016a46d1a",
            "name": "This is the page",
            "slug": "this-is-the-page"
            ... // all other fields of the relation
        },
        "breadcrumbs": [
            {
                "doc": "649fcdaafe5ff392e018a09e",
                "url": "/article-parent",
                "id": "64e2f477cf4f22007632f866"
            },
            {
                "doc": "649fcde3fe5ff392e018a0e4",
                "url": "/article-parent/article-title",
                "id": "64e2f477cf4f22007632f867"
            }
        ],
        "parent": {...} // from Nested Docs, probably no need to use this data
        ... // all your other article fields
    }
  • default discord avatar
    clement6396last year

    The thing is, I don't want to have several logics to determine the url of my pages in my front-end, the plugin already does the job very well in this respect, I just need to use the breadcrumbs generated to determine my url, independently of my collection.



    I've just added these few lines to the plugin, it's not perfect but it seems to work.



    Instead of passing the collection in which the function is called, I first check whether the collection passed as a parameter to "createParentField" is divergent.





    In my

    articles

    collection, I create the parentField and target the

    pages

    collection





    I can now generate custom url, without depth or collection limit:


    /home (

    page

    )/blogs(

    pages

    )/buisness-blogs(

    pages

    )/article3(

    articles

    )



  • discord user avatar
    jacobsfletch
    last year

    I was just about to pop in a link to this discussion, there’s some others asking about the same functionality

  • default discord avatar
    clement6396last year

    I'm about to finnish this part, I can make a PR if you want πŸ™‚



    to be able to give a array of relation collections, and select any document as parent

  • discord user avatar
    jacobsfletch
    last year

    I have not thought through the full technical aspects of this yet but yea if you’d like to give it a shot, by all means πŸ‘ I would also expect this might be a breaking change

  • default discord avatar
    clement6396last year

    For the moment not really, but I don't know all the payload and plugin mechanics. For now I can pass a specific collection or a array of collections within

    createParentField

    . And the breadcrumbs is well created. I think i'll add a

    collection

    field, within breadcrumbs array.


    You'll have to make up your own mind πŸ™‚ , personally I think this plugin could be so much more powerful with this use.



    @808734492645785600

    I just pushed a PR. Bear with me, this is the first time I've helped a project πŸ™ 😁



    https://github.com/payloadcms/plugin-nested-docs/pull/27
  • discord user avatar
    jacobsfletch
    last year
    @220272696582340629

    amazing! I don’t have the bandwidth to review that immediately but I’ll keep it tracked

  • default discord avatar
    clement6396last year

    No worries, I'm a bit dubious about the

    resaveChildren

    hook. I haven't been able to test much yet, but it gives an initial idea of what's possible.

  • default discord avatar
    zephury.last year

    has any of this been resolved, or are there any plans in the near future to resolve it?



    feels like quite a common issue; I would also like to localize urls and be able to reference collections as a child of a nested document.

  • default discord avatar
    janpeinilast year

    Localized urls can easily be achieved by adding

    localized: true

    to the corresponding slug field.

  • default discord avatar
    zephury.last year

    with your changes, may I ask how you get a page via api call? am I correct in understanding that the only way right now is to just query for the slug and that you can't query by url/pathname?

  • default discord avatar
    zephury.last year

    ahh, thank you, very nice. not sure how I missed that; I was looking through abunch of this stuff. Looking forward to your PR πŸ™‚

  • default discord avatar
    clement6396last year

    your welcome 😁



    but the PR is not done yet, and feature not even accepted by the core team, you can only target the same collection actually

  • default discord avatar
    zephury.last year

    ah, I guess I misunderstood, I thought they accepted it, just wanted you to write tests first or something πŸ˜…



    my brain is a bit fried right now, don't mind me 🀣

  • default discord avatar
    clement6396last year

    my PR was more a POC, I need to test it and see other stuff beofre publish it πŸ™ˆ

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.