Problem/Motivation

Linkit is an awesome module that has almost the possibility to create suggestions for everything. Not totally since Views displays that has "uses_route" (for instance page display) cannot be added in the suggestions.
This is a big flaw since Views is part of core and an important tool to create pages that could be referenced in nodes.
I would emphasis this feature is needed from the two following indicators:
- it used to be possible in former versions
- the number of followers on this issue

Proposed resolution

A new matcher plugin for view having a path is proposed as a solution.
As described in #6 and according to my own researched, it will require an API change since we will need some extra information on the
substitution plugin side to target the appropriate display. In is commonly assumed indeed that a view can have multiple page type display.
It will also require a new deriver to authorize EntityMatcher for config entities. This is done to reuse EntityMatcher as a base class for the ViewDisplayMatcher to reuse code.

Remaining tasks

- provide a functionnal patch
- review the patch and agree on a functionnal solution
- update tests accordingly
- review the whole patch
- enjoy

User interface changes

A new matcher (suggestion type) will be available from the UI: 'View display'.

API changes

\Drupal\linkit\SubstitutionInterface getUrl() method has to accept one more parameter.
This may break custom matcher plugins written by DX

Data model changes

None

--------------
EDIT: Original post:
Hi

How can I link pages created using views using Linkit module.

Thanks
Nijil

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nijinanijil created an issue. See original summary.

anon’s picture

Version: 8.x-5.0-beta4 » 8.x-5.x-dev
Priority: Major » Normal

Unfortunately this is not possible at the moment.

You can create a new feature request issue, or convert this into on if you like.

droprocker’s picture

Category: Support request » Feature request
ikeigenwijs’s picture

++!

frazac’s picture

++!

gwagroves’s picture

Having a look at this.

linkit.autocomplete.js only allows us to set (as I understand it) three data attributes for a suggestion: data-entity-type, data-entity-uuid, data-entity-substitution.

Views are entities, so we can build a Matcher that passes the entity type "view" and the view uuid to the autocomplete if criteria (e.g. view name) are matched. So far so good.

We then need a custom substitution plugin "view" to handle the entity type. (We set data-entity-substitution="view" and the LinkitFilter will load it.) The substitution plugin can load the ViewExecutable and can generate a URL using it. However, there is no way to pass in a display id as a data attribute, so currently I'm just linking to page_1 as a default.

If linkit.autocomplete.js would allow an extra field, e.g. data-entity-meta, the LinkitFilter could pass the meta data to the substitution plugin which could use it to generate the url.

(Ignoring the title attribute for now.)

gwagroves’s picture

Solved this using a different approach, using a custom configuration entity to store Drupal route information:

Create a custom config entity "Linkit custom link" that stores the route data (name, parameters, query string, etc.).

Create a custom Matcher to match custom links using the label (e.g. "Events list" is mapped to view.events.page_1.)

Create a custom Substitution class for custom links. The getUrl() method receives the custom entity we created from Linkit. We can now use the stored route information to build and return the URL. All of the required meta data is in the custom entity.

Site builders can define any number of custom links as long as they use a Drupal route, so Linkit can then provide editors with links to custom controllers, views.

The custom link entity could also accept external URLs, so that external links (e.g. "service portal") could be provided and updated centrally.

Dom.’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
12.64 KB

Patch attached and issue summary updated

This patch is functionnally OK, but I did not update any tests for now. I would like first an approval on the direction by module maintainer before further work.

jacobbell84’s picture

I did some testing on my sites, and one issue I ran into is that it's trying to process views that require arguments in their paths. This causes the Url::fromRoute call to throw an exception. I don't think there's a use case to support views like that, since we'd have no way to know what the argument is, so I'm just filtering them out of the result set for now.

jacobbell84’s picture

Fixing the linting issue with the previous patch

Dom.’s picture

I still add the error after upgrading to #10.
Symfony\Component\Routing\Exception\RouteNotFoundException : Route "view.portfolio." does not exist.
Notice the extra point "." at the end of the route. It comes from
"view.{$entity->id()}.$extra"
where $extra is always empty.

The reason is an uproper key in my patch #8 which leads to no substitution-extra added to the WYSIWYG.
Fixing that in attached patch.

Dom.’s picture

damn... I forgot to include the new files in patch #11.

BryanDeNijs’s picture

Thanks for this great patch.

I have made some improvements:
- There was a small issue with the "Include disabled views" setting. It didn't get saved when submitting the config form. This is now fixed.
- The view storage is now a class variable, set by the __construct.
- Made an extra setting to make it possible to only allow a selection of the views to be used by LinkIt.

Hope you like it :)

BryanDeNijs’s picture

I have made a new patch with some more improvements to be more in line with the other matchers.

BryanDeNijs’s picture

For a client, we have now implemented the patch of comment #14. But after a bit of testing, our client requests the possibility to search by display title instead of only the view label or display path.

I added the option. Here is a newer version of the patch.

BryanDeNijs’s picture

Sorry, something went wrong while creating te previous patch.
Here is te correct one.

cedewey’s picture

From a sitebuilding perspective, this is working great. This is a very helpful feature to have. Thank you to everyone who has worked on this. I'm going to leave it as Needs Review for someone else to do a code review.

BryanDeNijs’s picture

Here is a new version of the patch. After some time running the patch from #16, we noticed some errors in the logs about non-existing routes:

Symfony\Component\Routing\Exception\RouteNotFoundException: Route "view.news_overview." does not exist. in Drupal\Core\Routing\RouteProvider->getRouteByName() (regel 206 van /app/web/core/lib/Drupal/Core/Routing/RouteProvider.php).

This was caused by a text-format with the html-filter enabled. The data-entity-substitution-extra attribute was not in the allowed attributes for the <a> tag, so the html-filter deleted it.

The old patch did not check if the display_id (which should be stored in the data-entity-substitution-extra attribute) was present, so I did some improvements:

  • There is now a check if the data-entity-substitution-extra contains a view display ID.
  • There is now a check if the given display ID exists on the view and the display is a page display.
  • If one of the checks above fails, a warning is saved in the watchdog log instead of a nasty Exception

Another important note:
The patch (old+new) has a change in the SubstitutionInterface. Any other (custom) modules that uses this interface will break if this patch is applied, because of the new $extra parameter that was added to the getUrl method. So that is something to remember to put in the changelogs!

BryanDeNijs’s picture

Oops, I accidentally re-uploaded the old patch instead of the new one. Here is the new version.

BryanDeNijs’s picture

The last submitted patch, 18: add_views_suggestions--2867647-16.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 19: add_views_suggestions--2867647-18.patch, failed testing. View results

BryanDeNijs’s picture

FileSize
21 KB
2.11 KB

Another iteration of the patch. The matcher now also matches the view displays by matching path aliases. So for example, the path of the view display page is /news, but there is a translated path alias /nieuws, the view display will now also be found when searching for /nieuws.

undersound3’s picture

Does anybody know if there is something like this available for 6.x or can we use this patch as a base to create a patch for the 6.x version?

Kevin N’s picture

FileSize
21.21 KB

I did a quick update of the patch for 6.0.0-rc1, haven't tested it much, but it seems to work.

jeff.hartman’s picture

My experience with the patch is that it works well with one main caveat... the "data-entity-substitution-extra" attribute is not automatically added to the <a> when choosing the view.

If I add it manually in source, save, and then go to add another link to a view, the second link will have it added automatically.

Seems very odd and have been banging my head against the wall trying to figure out why.

robcarr’s picture

Like the idea of this patch and it's a shame the functionality isn't already there (pretty much every other entity type can be matched). Doesn't apply to 6.0 branch.

Kevin N’s picture

FileSize
20.68 KB

Did another quick tweak of the patch for 6.0.2 support.
Haven't tested it more than to see that linking views still work locally.

Kevin N’s picture

FileSize
21.22 KB

Noticed an issue with the patch at #25 so i made an update for it (This is for 6.0.0-rc1)