Nested docs, parents from another collection

default discord avatar
<Clement/>
3 weeks ago
22

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
    janpeini
    3 weeks ago

    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
    <Clement/>
    3 weeks ago

    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.

    image.png
  • default discord avatar
    janpeini
    3 weeks ago

    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
    <Clement/>
    3 weeks ago

    exactly, and for others collections later

  • default discord avatar
    janpeini
    3 weeks ago

    so like "categories" behave in wordpress and co?

  • default discord avatar
    <Clement/>
    3 weeks ago

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

  • default discord avatar
    janpeini
    3 weeks ago

    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
    <Clement/>
    3 weeks ago

    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
    janpeini
    3 weeks ago

    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
    <Clement/>
    3 weeks ago

    yep it would be great, to have this granular control

  • default discord avatar
    janpeini
    3 weeks ago

    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
    <Clement/>
    3 weeks ago

    Yep, that I want to do πŸ‘


    But it not seems working with the plugin actually :/

  • default discord avatar
    janpeini
    3 weeks ago

    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 @jacobsfletch has some more ideas here πŸ™‚

  • default discord avatar
    <Clement/>
    3 weeks ago

    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
    janpeini
    3 weeks ago

    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
    <Clement/>
    3 weeks ago

    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

    )



    image.png
    image.png
    image.png
  • discord user avatar
    jacobsfletch
    Payload Team
    3 weeks ago

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

  • default discord avatar
    <Clement/>
    3 weeks ago

    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
    Payload Team
    3 weeks ago

    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
    <Clement/>
    3 weeks ago

    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.



    @jacobsfletch 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
    Payload Team
    2 weeks ago

    @<Clement/> amazing! I don’t have the bandwidth to review that immediately but I’ll keep it tracked

  • default discord avatar
    <Clement/>
    2 weeks ago

    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.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.