Problem/Motivation
It is not obvious, especially to site builders moving to D8 from D7, that under certain circumstances it is necessary to use named route parameters in views paths around entities.
Proposed resolution
Improve the help text that appears under the page path setting in views such that it explains how and when to use named route parameters.
Remaining tasks
none
User interface changes
New help text on the views page path parameter to read (last updated for #27)
This view will be displayed by visiting this path on your site. You may use "%" or named route parameters like "%node" in your URL to represent values that will be used for contextual filters: For example, "node/%node/feed" or "view_path/%". The named route parameters are necessary when used within paths used by entities such as "taxonomy/term/%taxonomy_term" or "user/%user/custom-view"
API changes
none
Data model changes
none - text string update only
Original Issue Summary
An exception is thrown when creating content on a site that includes a view with a menu tab and a path similar to node/%/testing
.
Steps to reproduce (see also this exported view):
- Clean install of D8
- Create a new View named "test", tick the "Create a page" checkbox, change the path to "node/%/test
- Click "Save and edit"
- Under "Page settings" click the "No menu" link, change "Type" to "Menu tab", add a menu link title, click "Apply"
- Under "Advanced" -> "Contextual filters," add a Node ID filter with the default settings, click "Apply"
- Click "Save"
- Navigate to node/add/article, add some dummy content, click "Save and Publish"
Original report
Posted by laurencefass on June 25, 2015 at 11:22am
twig error creating a views menu tab on content.
following this instruction to create a new tab on a node (as per drupal 7)
View settings:
Page settings
Path: /node/%/customView
Menu: Tab: Custom Node View
the views tabs displays on the node but when i navigate i get the following screen
Error
The website encountered an unexpected error. Please try again later.
...and error log.
Type php
Date Thursday, June 25, 2015 - 10:17
User admin
Location http://rest1.drupal8.com/node/40/customView
Referrer http://rest1.drupal8.com/node/40
Message Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("node") to generate a URL for route "entity.node.devel_load".") in "core/themes/classy/templates/navigation/menu-local-task.html.twig" at line 17. in Twig_Template->displayWithErrorHandling() (line 328 of /home/rest1.drupal8.com/public_html/www/core/vendor/twig/twig/lib/Twig/Template.php).
Severity Error
ps: there is no twig component to select so i placed in under views
Comment | File | Size | Author |
---|---|---|---|
#67 | 2511892-67.patch | 1.5 KB | ravi.shankar |
#47 | 2511892_new_message_for_named_route_parameters_updated_text.png | 23.98 KB | bserem |
Issue fork drupal-2511892
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
dawehnerSounds like a problem of the devel module ...
Comment #2
Syntapse CreditAttribution: Syntapse as a volunteer commenteddoes this issue need re-assignment?
Comment #3
mikeker CreditAttribution: mikeker as a volunteer commentedNo. I'm able to reproduce this with the latest core and without Devel installed. The error is different:
Symfony\Component\Routing\Exception\MissingMandatoryParametersException: Some mandatory parameters are missing ("arg_0") to generate a URL for route "view.test.page_1". in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 174 of core\lib\Drupal\Core\Routing\UrlGenerator.php).
(export to reproduce attached).
Comment #4
mikeker CreditAttribution: mikeker as a volunteer commentedUpdated title and clarified STR in the issue summary.
Comment #5
mikeker CreditAttribution: mikeker as a volunteer commentedAck! Somehow managed to delete the original report when updating the issue summary. Replaced it from revisions.
Sorry about that...
Comment #6
dawehnerMH :(
Comment #7
mikeker CreditAttribution: mikeker as a volunteer commentedDowngrading priority to normal as the view will render correctly if you use a named route parameter:
node/%node/test
. The%
placeholder is supposed to be used for contextual filters.However, even if you add a contextual filter you end up with the same exception as above. So some part of this bug remains.
Comment #8
mikeker CreditAttribution: mikeker as a volunteer commentedUpdated title, STR, and exported view based on #7.
Comment #9
Chi CreditAttribution: Chi commentedI have got the same error on RC1. Using named parameter as was suggested in #7 resolved the problem.
Comment #10
kevin.dutra CreditAttribution: kevin.dutra at Workday, Inc. commentedI can confirm it's still there as of 8.0.1.
Comment #11
tannguyenhn CreditAttribution: tannguyenhn commented#7 worked for me, thanks sir
Comment #12
Syntapse CreditAttribution: Syntapse as a volunteer commented...sounds like a simple update of UI help/description needed to close the issue. ?
Comment #13
dawehnerSo yeah the problem is basically that views would have to allow to name the parameters of generated routes, much like it currently allows to name its routes.
Comment #14
fomenkoandrey CreditAttribution: fomenkoandrey commentedI have the same error at drupal 8.0.4
I tried to make a tab in the user's profile with his materials.
I use path: "user/%/posts".
Tab and urls like site/user/12/posts works, but i open any profile - i have the error:
I see this problem occurs with many people.
Is there any way to solve it?
Comment #16
rachel_norfolkHaving had the same error whilst trying to show a view at /user/%/my-travel-stories, I can confirm that simply changing that path to /user/%user/my-travel-stories fixed the exception and the view displayed correctly. So, yes, we need better documentation on this.
I’m not quite sure how to describe the differences between %, %node, %user etc. Anyone with better understanding?
Comment #17
Andreas Radloff CreditAttribution: Andreas Radloff commentedThanks Rachel! Your solution worked for me too, in my case this worked: crm-core/organization/%crm_core_organization/events
Comment #18
ARUN AK CreditAttribution: ARUN AK commentedHi fomenkoandrey (#14),
Your issue got fixed? Can you please tell me what was the fix? Even I also want to add a tab in user profile page.
Thanks,
ARUN AK
Comment #19
rachel_norfolkArun - note that, when adding a tab to the user profile, you’ll probably want a path in the view of
/user/%user/my-tab
rather than
/user/%/my-tab
Hope that helps.
Comment #20
dawehnerIt would be nice if we could come up with some form of validation for that.
Comment #21
rachel_norfolkOkay, I believe that the reason people are hitting this error is more down to D8 being different to D7 but people still assuming everything in D8 views is just the same.
As long as a named parameter is used, as described in #7, #16 #17, all is well and works as expected. We just need to better hint at the expectation for a named parameter.
Attached is a patch that adds another example to the already-existing text mentioning named parameters in the views module.
(Interestingly, why was this a patch to views and not views_ui???)
Moved to 8.2.x
Comment #22
ARUN AK CreditAttribution: ARUN AK commentedrachel_norfolk,
I need to add a custom tab in user profile page. I defined my route like below:
mymodule.routing.yml
mymodule.links.task.yml
mymodule.links.menu.yml
Then after clear cache, tab is appearing in profile page. But when I opens the url /user/1/custom getting page not found.
Comment #23
rachel_norfolkThat’s not something you are doing in views, though, Arun?
Comment #24
WigglyKoala CreditAttribution: WigglyKoala at Torchbox commentedI'm not 100% sure the documentation is clear still, the extra example doesn't stop me doing /node/%/test because it mentioned node/%/feed earlier on.
Comment #25
rachel_norfolkMaybe the text could be something more like...
Would that help?
Comment #27
hussainwebThe problem, IMHO, is that no one reads until the end of the sentence for the example. When I got this error, I read the comment again but never made it till the end. I then just went to search and found this issue.
Also, it is worth considering that most use cases for placeholders will be to use in paths like
user/%/...
ornode/%/...
. It would be worth bringing up the named parameters earlier.How about something like this?
Attaching a patch with above.
Comment #28
rachel_norfolkHussainweb in #27 has done a far better job of providing explanatory text here. I think it makes much better sense now and causes me to read the relevant info.
I think it is now a good, simple change. Pity we can’t squeeze it into 8.2!!
Screenshot:
Comment #29
mikeker CreditAttribution: mikeker as a volunteer commented+1 for #27, much better wording.
Thanks, @hussainweb!
Comment #31
hussainwebBack to RTBC after random failure.
Comment #32
rachel_norfolkPlacing this in front of the Usability Team as it makes a very useful usability improvement to views.
Comment #33
rachel_norfolkI’ve updated the Issue Summary to make this more amenable to committers.
I believe this SHOULD be included in the 8.2 release:
Comment #34
catchI'm not sure about 'within paths used by entities'.
Isn't the issue that user/%user is an existing path, so we're adding user/%user/foo. But then user/%user/foo isn't "within an entity path".
Maybe something like 'matches an existing entity path. For example adding a view at user/%user/foo when user/%user already exists'.
I also wonder whether we could validate this i.e. look for that exception when validating and show an error if so.
Comment #35
dawehnerWell the main point is that its an existing path with a certain slug, which has a certain name. On the other hand the really common case is that the slug will have an entity in there, especially in the context of views.
We could maybe indeed try to. Good idea.
Comment #37
LendudeThis seems like it's at least related to #2489940: Views path with menu tab does not validate and results in fatal error (might be duplicates)
Comment #38
zenimagine CreditAttribution: zenimagine commentedHow do I add a tab to a store ?
Comment #40
dawehnerOn top of that I believe it would be helpful to allow
/node/{node}/example
, because well, %node is really not anymore the recommended way. For example the routing system uses {node} under the hood.Comment #42
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedI am able to add the path node/%/stuff but I cannot make it a tab.I understand the issue now
adding % is just for contextual links and adding %someword is for node tabs.
Comment #45
bserem CreditAttribution: bserem at zehnplus commentedThe attached patch is based on #27 and applies on Drupal 8.8.x and also tries to emphasize the need for named route parameters.
I got this error while working on a contrib module, took me quite some time to figure out that it needed a named route parameter.
Comment #46
vensires CreditAttribution: vensires at E-Sepia Web Innovation commentedThe patch is fine @bserem! I just tested it on simplytest.me and follows exactly the issue description's proposal. The only change I would suggest is to replace necessary with required which is more common as a word and for a reason I believe.
Comment #47
bserem CreditAttribution: bserem at zehnplus commented@vensires thanks for the feedback, it actually sounds/looks nicer the way you suggested.
Attached is the new patch, screenshot and interdiff.
Comment #48
vensires CreditAttribution: vensires at E-Sepia Web Innovation commentedGood to go for me.
Comment #49
bserem CreditAttribution: bserem at zehnplus commentedComment #50
bserem CreditAttribution: bserem at zehnplus commentedI'm trying to add Vensires in the credit list but apparently I can't.
Comment #51
catchThe help text looks OK to me, but the original report here is describing a situation where just configuring a site via the UI you're able to trigger an exception. I think ideally we'd add some validation here too as well. Tagging for subsystem maintainer review - although we could possibly look at the validation in a separate issue.
Comment #52
alexpottWe can't break this translatable string into many lines. That makes it impossible for POTX to work this it all needs to be on one line.
Comment #53
bserem CreditAttribution: bserem at zehnplus commented@alexpott didn't know about that, re-rolled a new patch.
@catch the validation would be tricky because the problem happens if you add a tab with a non-named route parameter. So, the validation should check both the menu settings and the path. It would have to be a validation on the view-level and not these particular settings, because both of them might be correct on their own. It is the combination that triggers the exception.
I will have a look at it, but I do suggest the follow-up issue, as this is gonna be way more complex.
Comment #54
vensires CreditAttribution: vensires at E-Sepia Web Innovation commented@catch I also vote for a separate issue on handling the error. Or, at least, making sure that we provide a better DX on why the error occurs (like we did in #2959445: Entity querying config entities does not work, so neither does JSON API collection filtering: provide helpful DX). Validation should be done, in my opinion, in views entity when saved (a constraint maybe?) in order to also catch configuration imports too. But let's not talk too much on this here.
The problem of letting developers discover the same issue again and again (if they find it) is very time-consuming. If it's properly documented in-site just below the field, even simple site builders will be able to resolve it if it occurs to them. Patch is fine again for me, so setting to RTBC; hoping it will land in Drupal core as soon as possible.
Comment #55
catchComment #56
xjmWe need that followup filed to go ahead with this change. Thanks!
Comment #57
xjmAlso, I think the correct status is NR for the subsystem maintainer review. I'd suggest reaching out to one of the (other) views maintainers for feedback (@dawhener or @tim.plunkett, in this case).
And a UX review would be good too.
Comment #59
bserem CreditAttribution: bserem at zehnplus commentedI am inclined to set this to RTBC as it was in comment #54, for two reasons:
1. I am stumbling upon this every couple of months and I suppose I am not the only one
2. It is stuck in review, waiting for the subsystem maintainer, even though it is a simple wording change.
It applies to 8.9.x and all tests pass.
It doesn't apply to 9.x.x but if this gets a green light we can easily port it.
Comment #60
Lendude#2489940: Views path with menu tab does not validate and results in fatal error will do as a 'follow-up' here I feel (it's actually older then this issue).
Since that issue is pretty hard to solve, I think it's a great idea to move ahead with this documentation change first, hopefully helping some people avoid fatal errors.
But in reply to #59, we port the other way around, this needs to go into 9.2.x first and then we backport it to other versions, so we need something that applies to 9.2.x to start with
Comment #61
bserem CreditAttribution: bserem at zehnplus commentedAttached is the patch for drupal 9.2.x (also applies to 9.1.0) as suggested in #60 by Lendude
Patch in #53 stilll applies to drupal 8.9.x and can be used to backport this.
There is no interdiff, since the patch is actually the same.
Comment #62
bserem CreditAttribution: bserem at zehnplus commentedComment #64
bserem CreditAttribution: bserem at zehnplus commentedFollowing latest updates, a gitlab MR has been created instead of patches.
Hiding patches from the overview.
This MR: https://git.drupalcode.org/project/drupal/-/merge_requests/125 is the same as #61
Comment #65
rachel_norfolkOkay -
1. I have used the preview on tugboat to check that the correct text is appearing when building a view, as below:
2. We have already confirmed that we are happy with the updated language.
I think it is time to mark this as RTBC!
Comment #66
alexpottI still agree with @catch that the
within paths used by entities
feels tricky. I think we can be more explicit here. For example we could say...This view will be displayed by visiting this path on your site. You may use "%" or named route parameters like "%node" in your URL to represent values that will be used for contextual filters: For example, "node/%node/feed" or "view_path/%". Named route parameters are required when this path matches an existing path. For example, paths such as "taxonomy/term/%taxonomy_term" or "user/%user/custom-view".
The one issue with the above is that it only covers the partial matching of user/%user and user/%user/custom-view - but I think that this is okay because we have the example. Maybe someone else has an idea about how to make this even clearer but given the point of this issue is to improve the text I think it is worth refining until we have something that someone new to Drupal can think "yep I get that".
Comment #67
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedMade changes as per comment #66.
Comment #69
rachel_norfolkThanks Alex and Ravi for the updates to the language. It is a lot better now.
Confirming that the latest patch does indeed reflect the language suggested in #66
Setting back to RTBC
Comment #70
larowlanSaving issue credits and updating tags
Comment #72
larowlanCommitted b796e37 and pushed to 9.3.x. Thanks!
I didn't backport this to 9.2.x because there's a string change, and that would break existing translations. We only do that in minor versions. Tagging as such.
Comment #74
iamfredrik CreditAttribution: iamfredrik commentedThe patch doesn't seem to work with commerce products. I get the following errors:
/product/%/mytab
Some mandatory parameters are missing ("arg_0") to generate a URL for route "view.myview.page_1"
/product/%product/mytab
Some mandatory parameters are missing ("product") to generate a URL for route "view.myview.page_1"
Comment #75
vensires CreditAttribution: vensires at E-Sepia Web Innovation commented@iamfredrik you should check what the entity declares as route in its annotation. Check
Product.php
and you will see that the canonical URL is/product/{commerce_product}
. As a result, you should use/product/%commerce_product/mytab
.Comment #77
quietone CreditAttribution: quietone at PreviousNext commentedClosed #2883266: RuntimeException: Callable "Drupal\contact\Access\ContactPageAccess::access" requires a value for the "$user" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() as a duplicate, adding credit.
Comment #78
dbroll CreditAttribution: dbroll commentedDoes anyone know what was actually added to core to resolve this? I'm on a d8 site with commerce, so looking to backport this functionality if i need to. I have setup a view with url /products/%commerce_product/variations-bulk and am getting this error:
TypeError: Argument 1 passed to Drupal\Core\Entity\EntityRepository::getTranslationFromContext() must implement interface Drupal\Core\Entity\EntityInterface, string given, called in /modules/contrib/commerce/modules/product/src/ProductVariationListBuilder.php on line 73 in Drupal\Core\Entity\EntityRepository->getTranslationFromContext() (line 99 of core/lib/Drupal/Core/Entity/EntityRepository.php).
As views is passing a string for "product_id" rather then the product object. Any ideas how to fix?
Comment #79
bserem CreditAttribution: bserem at Annertech commentedYeah, we improved the help message so that it is clear for users what to do.
See: https://git.drupalcode.org/project/drupal/-/commit/b796e3779b21e1f62f9e5...
Comment #80
dbroll CreditAttribution: dbroll commented@bserem thanks, per the description I think i'm using the correct syntax for my url (/products/%commerce_product/variations-bulk) but am still getting an error. Is that a commerce issue? Or a views/core issue?