Standard "Link to content" field is missing in index views.

So, I found an "URL alias" datasource checkbox at Content index "Edit" tab.

I was happy to find it... but alas... it does not work. The feature is broken, it does not have bundles as configure option so it indexes all content by default, and on top of it I couldn't get it to display url where I need it in views.

At the end, when I went to disable the checkbox it didn't uninstal the URL alias datasource properly... leaving the remnants of installation around. So, at the end I restored the backup (made before enabling URL alias datasource) to get rid of it completely.

With standard "Link to content" field missing in index views and "URL alias" datasource broken it seems that there is no way how to get node url and use it inside views fields.

My config: Search API 8.x-1.18, D9.1.0, PHP7.4

Comments

devad created an issue. See original summary.

devad’s picture

Issue summary: View changes
devad’s picture

Issue summary: View changes
devad’s picture

Category: Feature request » Bug report

I have updated issue description with new data.

Changing category to "Bug report" here.

devad’s picture

Title: "Link to Content" field missing in views » "URL alias" datasource is broken
Issue summary: View changes
devad’s picture

Issue summary: View changes
devad’s picture

Issue summary: View changes
devad’s picture

Issue summary: View changes

Is there any workaround here how to get URL alias inside "Fields" section in content index view?

drunken monkey’s picture

Status: Active » Postponed (maintainer needs more info)

Is there any workaround here how to get URL alias inside "Fields" section in content index view?

Try adding the “URI” field to the index, then it should also appear as a Views field.

Also, the “Link to the Content” option should appear when checking “Use entity field rendering” in the Views field options. Otherwise, “Link this field to its item” should do more or less the same thing.

If none of this helps, please update the IS to clearly state what you want to achieve (and what your current status/problem is). It’s currently mostly about what you tried, which isn’t really helpful. (The “URL alias” datasource is completely wrong for your purpose. This will just add URL aliases as separate search items to your index, not URL aliases for the indexed content.)

devad’s picture

Status: Postponed (maintainer needs more info) » Active

Re: #9

Thanks for reply @drunken monkey

The node URL address is not a problem. I can manage to get url like /node/108 easily inside my content index view.

But I need to get the URL alias as available token inside my view. So that I can link a custom text like this inside "rewrite the output of this field" section:

<a href="{{ alias_url_token }}">some custom text</a>

"Link to the Content" option is not helpful in this case since it is a custom text which needs linking.

The "URL alias" datasource looked promising to fulfill this gap... but it is not working as explained in details in the issue summary.

The "URL alias" datasource should be fixed or removed from Search API as having the broken feature inside Search API is both confusing and time consuming.

bohus ulrych’s picture

+1 I have exactly same problem.

"URL alias: Provides URL alias entities for indexing and searching." is not working for me.
I'm using Database search server. When new Search index field is created, then new table is created (e.g. search_api_db_INDEX_NAME_alias) with correct aliases. But when selecting such field in the Views, it is empty.

vitaliych’s picture

What is worked for me :

I have added to the Search Api Index URL field from general tab

In views in Search category URI field appeared and i have added it to view

in URI field rewrite options "Override the output of this field with custom text" i have added <a href="{{ url}}">some custom text</a> where {{url}} is a replacement pattern.

Works for me to workaround read more link in unformatted views fields

devad’s picture

Re: #12

@vitaliych did you accomplished to get the *** URL ALIAS *** link inside your view or just a regular URL link?

The whole this issue is about the URL Alias.

drunken monkey’s picture

Category: Bug report » Support request
Status: Active » Postponed (maintainer needs more info)

@ devad/#10: Seems like you want links with URL alias for content results, not URL aliases as separate results. As the “URL alias” datasource provides the latter it’s clear that it won’t help with your problem.

For me, when editing a field in Views, I either have a “Link this field to its item” option or a “Link to Content” option (depending on the “Use entity field rendering” option), both of which correctly add the link to the URL alias. I can’t tell what is going wrong in your case, but this should be working correctly in general.

Main takeaway: In 99.9% of cases, the “URL alias” datasource is not what you want to use. It’s just there because we provide generic integreation for all content entity types, and URL aliases are on of them.
Enabling a new datasource in general does not change anything about the indexing/searching/display of the existing search results, but adds new documents/result items to the index. So, you will (potentially) get other results on your search page, but the existing ones will not change in any way.
I hope this makes things clearer.

I’m not sure why the options described above are not there for you. Can you provide a screenshot of the field options in the Views UI, or details about the field in question?

devad’s picture

Thanks for reply @drunken monkey

I need the URL Alias as replacement token which I can use to rewrite the value of another field. Like this:

<a href="http://www.facebook.com/share.php?u={{ the_url_alias_token_i_need }}&amp;title={{ title }}" title="Facebook"><img alt="Facebook" src="/sites/default/files/template/social_media_icons/facebook_share.svg"></a>

<a target="_blank" rel="noopener noreferrer" class="twitter share" href="https://twitter.com/intent/tweet?url={{ the_url_alias_token_i_need }}&amp;hashtags=MySiteName" title="Twitter"><img alt="Twitter" src="/sites/default/files/template/social_media_icons/twitter.svg"></a>

So field options like “Link this field to its item” are not helpful in this case.

In a normal view there is a "Content: Link to content" field and it's [[ view_node ]] token is what I use... but "Content datasource: Link to content" field is not available.

All "Content datasource:" URL fields I tried generate tokens like /node/123 and that's not what I need.

devad’s picture

Status: Postponed (maintainer needs more info) » Active
drunken monkey’s picture

Title: "URL alias" datasource is broken » Add an "Item URL" field to Views
Category: Support request » Feature request
Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new855 bytes

Ah, thanks, now this makes more sense.
I think this should still be possible out-of-the-box by adding the “URI (search_api_url)” field to the index. Once it’s in the index, you can add it to the view as a field. It should always display the aliased URL, as desired.

However, I see that adding an indexed field to get something to display, especially such a simple thing as the item URL, is both confusing and needlessly complicated. It turns out we can also add built-in support for this very easily: See the attached patch. With it, there should now be a new “Item URL” field available in all Search API views, which (hopefully) does what you want it to.
Please test/review and tell me what you think.

Before committing, however, I fear this still needs test coverage. Would you be willing/able to provide that? it can just be added to the existing \Drupal\Tests\search_api\Functional\ViewsTest::testViewsAdmin() test method – just add a new field and then verify that’s it’s in the output.

devad’s picture

Status: Needs review » Reviewed & tested by the community

I have applied it and I can confirm that the patch #17 works as expected. Both relative and absolute URLs. Great!

Tests... hmmm... I can try...

devad’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.89 KB

Tests incoming...

Status: Needs review » Needs work

The last submitted patch, 19: 3188138-19--item_url_views_field.patch, failed testing. View results

devad’s picture

Status: Needs work » Needs review
StatusFileSize
new2.54 KB

Error message:

Drupal\Tests\search_api\Functional\ViewsTest::testViewsAdmin
Behat\Mink\Exception\ElementNotFoundException: Form field with id|name|label|value "options[fallback_options][multi_separator]" not found.

The URL field has Form which seems to need additional testing section of code... somewhere.

I have tried my best. Need help here. :)

drunken monkey’s picture

Issue tags: -Needs tests
StatusFileSize
new2.71 KB

Thanks a lot, good job so far!
However, you’re not testing the right thing: this issue is about not requiring an indexed field anymore, so instead of adding the url field first to the index and then to the view, we should just add the search_api_url field to the view (which should now always be present) right away.

Attached patch corrects this, and at least locally also passes the tests. If both you and the test bot agree with that, then I can commit.

drunken monkey’s picture

StatusFileSize
new3.01 KB
devad’s picture

I think this should still be possible out-of-the-box by adding the “URI (search_api_url)” field to the index. Once it’s in the index, you can add it to the view as a field. It should always display the aliased URL, as desired.

I don't know if something has changed since I have tried last time to do the same thing, but this time I was able to get the URL aliases of my nodes inside my view with "General / URI (search_api_url)" field available at the Search API "Fields" section. Without any patch from here... just with the latest Search API 8.x-1.21. as you suggested.

Here is a very straightforward and screenshot-enriched how-to if somebody else needs it:

I have added the "General / URI (search_api_url)" field to the list of Search API fields and saved the list.

2

2

It is worth mentioning here that it can be configured here to show the absolute url alias if needed.

After that the "Search: URI (indexed field)" field becomes available to my view and it works nicely as alias url.

2

The solution #12 is the same one actually. With short delay - thanks @vitaliych!

devad’s picture

StatusFileSize
new312.52 KB
new292.23 KB
new234.64 KB
new197.75 KB
devad’s picture

Re #22:

The new "Index Default content index: Item URL" field works nicely for me both at new installation and existing site (after patching the site).

However... at the end of the day... my vote is that we close this issue with "Works as designed".

The new "Item URL" field might be a bit simpler to implement than tutorial described in #24 but if we commit the new "Item URL" field - it will be one more piece of code we will have to maintain and debug in years to come. Maybe better to avoid that... unless the new "Item URL" field brings some significant additional benefits which the "General /URI" field alias does not have? At the moment I can see that it has one option less... the "Absolute URL" option is missing.

drunken monkey’s picture

I’d still prefer committing this, now that it’s otherwise RTBC:

  • The change is almost trivial, just adding a Views data definition for a property we already have.
  • It’s got test coverage (which is also a pretty simple change – with, also, a small general fix to that test which might save headaches in the future (the $this->rebuildContainer() call).
  • The fact that there exists a tutorial in an issue somewhere for how to do this won’t help 95% of people wanting to achieve this.
  • Adding a field to the index just for displaying goes against the basic principles of the Search API, and adds unnecessary overhead.
devad’s picture

Status: Needs review » Reviewed & tested by the community

Sure. Great! RTBC for me.

  • drunken monkey committed 19beaf5 on 8.x-1.x
    Issue #3188138 by drunken monkey, devad: Added an "Item URL" field to...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Great, thanks again!
Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.