Problem / Motivation

The path attribute of results is always null. I'm guessing that this should be the url alias of the node. It's also not possible to filter on path, resulting in a 500 error: "Path" Not found.

I was thinking that this issue #2539634: PathItem::delete() never runs because the path field type is a computed field in disguise had something to do with it, but after downloading the latest version with the pathes of that issue, running update.php it still has the same result.

CommentFileSizeAuthor
#2 jsonapi_2799909.patch2.08 KBjanlaureys

Comments

JanLaureys created an issue. See original summary.

janlaureys’s picture

StatusFileSize
new2.08 KB

I created a patch that adds a plugin compared to the file url. It returns an alias at least. Feels a bit dirty though :x

Yeah, that was no good really.

janlaureys’s picture

Assigned: janlaureys » Unassigned
e0ipso’s picture

Title: [BUGFIX] Return the alias and allow filtering on it. » [BUGFIX] Return the alias and allow filtering on it
e0ipso’s picture

Status: Active » Needs work

Settings this to needs work.

dremy’s picture

I added a patch here: https://www.drupal.org/node/2649646#comment-11843756 from Related Issue: #2649646: [pp-1] Normalize path fields as part of the entity: allow REST clients to know the path alias, that has this working.

badrange’s picture

There is a patch at https://www.drupal.org/node/2846554#comment-11924532 which seems to solve this. It also applies to 8.2.6 even though it is made for 8.4.x.

e0ipso’s picture

It's worth noting that even if the path may be correctly exposed, you won't be able to filter by it:

GET /jsonapi/node/article?_format=api_json&filter[alias][condition][path]=path.alias&filter[alias][condition][value]=/foo-bar HTTP/1.1
Host: d8dev.local

Returns:

{
    "errors": [
        {
            "title": "Bad Request",
            "status": 400,
            "detail": "Invalid nested filtering. Invalid entity reference \"path\".",
            "links": {
                "info": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1"
            },
            "code": 0
        }
    ]
}
badrange’s picture

Thanks for that heads up!

In our case we can live with this situation (at least it seems so, for now), because the core patch allows the decoupled front end to fetch the data directly from the backend based on the path without us writing any crazy magic. Time will tell.

badrange’s picture

Oh, it looks like fetching the related via &include= doesn't work either in this way. Or is it just me?

gabesullice’s picture

@badrange are you including path or path.alias?

dawehner’s picture

Given that the path alias will never be stored in fieldapi I doubt we will ever have a chance to filter by it.

e0ipso’s picture

This is a big bummer:

Given that the path alias will never be stored in fieldapi I doubt we will ever have a chance to filter by it.

I suppose that client applications will have to do routing on some alternative custom field that contains the alias information. There is no way that a React app will have UUIDs in their URLs.


Does someone know a contrib that will maintain a textfield with the same contents of the alias?

e0ipso’s picture

Status: Needs work » Closed (cannot reproduce)

Closing since it seems this won't be doable:

Given that the path alias will never be stored in fieldapi I doubt we will ever have a chance to filter by it.

dubcanada’s picture

This is a pretty big issue, not being able to filter content for a specific page doesn't allow you to do react based routing.

Your suggestion about maintaining a textfield with the path alias seems like it might work. But is there a way to fix this or make an exception (do filtering for paths differently) for path aliases?

In regards to the textfield, is the thought we would have a hidden textfield that maintains a copy of the path that gets updated upon entity save? Not sure how we would set it up to work for every entity...

gabesullice’s picture

@dubcanada

My approach has been to create a "slug" property. I auto-generate it sort of like an html class, e.g., "My Node Title" becomes "my-node-title". The field should be unique (at least per entity type).

Instead of filtering on a full path, my front-end app's routes can be structured like so: /article/{slug}

Then, I can just filter by that slug property like so: /jsonapi/node/article?filter[slug][value]=my-node-title

e0ipso’s picture

https://www.drupal.org/project/machine_name and https://www.drupal.org/project/safeword are relevant projects for that. However, they don't have a D8 release. Maybe that would be a good place to start.

spleshka’s picture

This is interesting! The solution I considered as "internal workaround" turns out to be the only viable way to go :) Having this conversation read, I've published our internal module which maps "path" property to the field. Check out Fieldable Path. It still needs tests coverage and minor improvements for wider use cases, though "done is better than perfect".

Critics and feedback are welcome.

e0ipso’s picture

Thanks again for the module @Spleshka!

bmx269’s picture

Awesome module @Spleshka! Thanks

e0ipso’s picture

I think it can be really interesting to add `https://github.com/contentacms/snail` to the mix as well.

segovia94’s picture

Even if we can't filter by it, it would still be helpful to return the path rather than just "null". For example, if I have a directory of people that another website is consuming, it would be good to have the node path so that when a person is clicked on in the list it will take them back to the correct page on the original site.

gabesullice’s picture

@segovia94,

This should land "automatically" when #2392845: Add a trait to standardize handling of computed item lists lands in core. We support computed fields on entities already, it's just that the path field is not a "real" computed field yet.

rozh’s picture

#2392845: Add a trait to standardize handling of computed item lists has landed, but I can't find a way to filter node by path.

Could you help me to construct request to get a node by path, if possible. Or Fieldable Path is still required for that kind of functionality?

e0ipso’s picture

Why do you need to filter by path? 90% of the cases where you think you need to find content by alias what you want instead is http://dgo.to/decoupled_router

rozh’s picture

Thank you e0ipso! This is perfect solution indeed.