Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is there a way to do that?
I'm try to create two pages as user/%/workspace and user/%/nodes, showing nodes created by user. user/%/workspace dedicated only for current user and can't be viewed by other. Instead they can view user/%/nodes, and user/%/nodes can't be accessed by user itself (there a workspace instead).
Both of views perfectly works as intended.
Now i'm try to assign this pages to tabs at /user page. And both tabs always present, regardless of what user looks at user's page. One of pages is empty, and i need to hide that page. How?
ps. sorry for my english...
Comments
Comment #1
dawehnerthis is quite hard, especially for drupal6:
the Problem is that drupal6 doesn't support creating of menu items of the fly
but you could do something with your own hook_menu item and a custom title callback
Comment #2
merlinofchaos CreditAttribution: merlinofchaos commentedActually a custom access callback. The problem you run into is that the query will be run a lot more than you think it should, and could impact performance. Views won't support this itself, you'll need to use hook_menu and create your own menu item for this.
Comment #3
aschiwi CreditAttribution: aschiwi commentedIs there any chance somebody can give some example code for this? Would it have to be a custom mini module, or rather a block with php content?
I'm working with Organic Groups to create work environments per group. I'm willing to hide the regular OG menu and create a custom menu which shows links to views if the view is not empty. If I knew php better, I would probably count the nodes of a certain type and if the count is bigger than 0, I would display the menu link.
Comment #4
momper CreditAttribution: momper commentedsubscribing
Comment #5
aschiwi CreditAttribution: aschiwi commentedOkay, I got this working with Views and a block and I'm sharing my code. It's probably not very pretty and most likely a little messy, but this is what I got.
Create a block. Put the following into the block body and set input type to php. This example includes calling two views.
At the bottom of the block add this following code and make it "Show if the following PHP code returns TRUE (PHP-mode, experts only). " This makes the block show only on OG pages.
I hope this is understandable.
Comment #6
brisath CreditAttribution: brisath commentedSubscribing
Comment #7
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedsubscribing
Comment #8
hellomobe CreditAttribution: hellomobe commented@3
Is there a way to do this in the view as an argument? User:Id has a node of content type X > 0. If this argument isn't true than the view page is hidden (and the menu tab?) as part of the validation -- does it work this way?
Comment #9
merlinofchaos CreditAttribution: merlinofchaos commentedYes it does work this way, so your argument validation code could do this.
Comment #10
hellomobe CreditAttribution: hellomobe commentedany idea of the code to do this - I don't see an option for node "count".
Comment #11
merlinofchaos CreditAttribution: merlinofchaos commentedThere's no option, you'd need to write validation code to do it.
Comment #12
whiklojaschiwi, I just wanted to thank you for your code.
I did a modified version for a single view with multiple displays. I haven't tested performance, but the result is exactly what I wanted.
Comment #13
aac CreditAttribution: aac commentedSubscribing!!!
Comment #14
tomsm CreditAttribution: tomsm commentedI want to hide the menu tab when the view is empty.
Can someone help me with the validation code for the argument, please?
Comment #15
5t4rdu5t CreditAttribution: 5t4rdu5t commentedI'm not sure about performance issues but this is at least a solution that works for me. I needed to hide tabs when there were no results on the view.
On the view's argument I chose PHP code as Validator and added the following code:
And set Action to take if argument does not validate: to Hide View / Page not found
Comment #16
lesleyfernandes CreditAttribution: lesleyfernandes commentedI have the same problem, but the argument is an uid.
Comment #17
lesleyfernandes CreditAttribution: lesleyfernandes commentedand if the argument is an uid?
Comment #18
5t4rdu5t CreditAttribution: 5t4rdu5t commentedIf your argument is an uid you'd just need to adjust the code accordingly. Make sure the SQL tries to fetch the same rows you're trying to retrieve with the view. Not an optimal solution but at least works.
I wouldn't recommend this for large complex queries or heavily loaded servers...
Comment #19
5t4rdu5t CreditAttribution: 5t4rdu5t commentedComment #20
5t4rdu5t CreditAttribution: 5t4rdu5t commentedComment #21
melchoir55 CreditAttribution: melchoir55 commentedNice Libriana. #18 worked for me! I'm a little afraid of what will happen if my site gets a lot of traffic, but I guess I'll cross that bridge when I come to it.
Comment #22
melchoir55 CreditAttribution: melchoir55 commentedI may have spoken too soon. #18 does indeed hide menu tabs. Unfortunately, it appears to be hiding the tabs whether or not they contain content!
I wonder if this is because the view is simply showing the content (body) of a node?
Update: I think I have thought up code that will work. I just don't know php well enough to actually write this:
php validator-
Is there anyone who could actually put together this code for me? I'm going to go start working on trying to piece it together, but I'd appreciate help if anyone can levy it.
Comment #23
5t4rdu5t CreditAttribution: 5t4rdu5t commented@melchoir55, are you trying to restrict access based on the current user roles?
If that's the case, have you tried setting the roles on the Access settings of the view? That should do the trick.
In case you need to check some other conditions together with user roles, you can add code to your argument like this
Comment #24
melchoir55 CreditAttribution: melchoir55 commentedThanks for the response libriana.
I have a view that has tabs. These tabs are themselves simply views of different content types which only a special kind of user can create, and these tabs attach to user profiles. . Each special user can create one and only one of each content type. Consequently, the view just filters content type: P and displays node:body in the field. The argument is uid pulled from url, since I want to display tabs created by the user being viewed, not by the user logged in. It all works fine, the only problem is that the tabs display on user profiles even when they haven't created any of the content pages (because they don't have permission to do so and never will).
I think that the problem is similar to OP. So, I figured your code above would have worked. For some reason, it just hides the tabs. They don't become visible when content is present.
P.S. I should mention the tabs are created using views itself. This is done by adding a "page" display then setting the "menu" entry to "menu tab".
Comment #25
5t4rdu5t CreditAttribution: 5t4rdu5t commentedAh... now I have a more clear picture of your situation :) Then the condition you want to check for is whether the node already exists for that uid and content type. The code on #18 should have worked for you, although you'd still need to check for the type. In case you're not very familiar with SQL or accessing the db directly, you can also try using node_load.
First, make sure your argument is fetching the uid from the url (guess you've already done so). Then go like this on the php validation:
This should work for you.
Comment #26
melchoir55 CreditAttribution: melchoir55 commentedThanks again for the response libriana. I don't know whether I'm messing up something really simply, but 25 did not work for me either. I am pulling the uid from url (in the argument section) and I'm filtering based on type: mytype (in the filters section). I'm displaying content Node: Body (in the fields section).
When I dump your code into the php validator through User:Uid argument, all it does is hide my tab entirely. I'm sure I'm doing something fundamentally wrong here, just not sure what.
However! I have managed to pull off what I wanted to do anyway and it is probably less resource intensive. In the User: Uid argument I chose "user" as the validator, then I restricted the view based on user role (a simple checkbox in this validator) and selected the roles whose profiles I wanted to display my tab. This worked perfectly.
Edit:
I should point out that this doesn't do exactly what the OP wanted. That is, the procedure I outline will not hide tabs because they are empty. It will only hide certain kinds of tabs when the user is viewing a profile who's owner cannot normally create those tabs.
The result is that empty tabs will still be present for users who could make the content but haven't done so. This isn't a problem for me, because all my users that have permission to create this content do so.
Comment #27
Shane Birley CreditAttribution: Shane Birley commentedI have been testing this since last week and I have found that there appears to be an issue with Views in how it is conducting the looping in the PHP solutions. I am still debugging and will post anything I find.
Another thing I will mention is that this may need to be a feature request since the overall question is how to hide a view should there be no content, regardless of how that is determined.
Comment #28
monsta_militia CreditAttribution: monsta_militia commentedHello,
from you input php code view. Spending one hour but it work for me :)
It didn't work because you have to remove
Comment #29
pippal CreditAttribution: pippal commentedI have a Project page with an Events tab to show all events that node reference to that project. To hide the events tab if the view is empty I have the following in my argument php validation:
I copied the SQL from another view I created, seems to work. Thought I'd paste it here in case any one else finds it useful, I searched around a lot for an answer this was one of the few threads I found useful.
I have no idea what the effect on performance will be... :/ Any ideas?
Comment #30
gMaximus CreditAttribution: gMaximus commentedHi...
I'm using marketplace where the Seller role can create the content type "products".
I've created a view that lists all products from a seller using their user id from the url as an argument. Then set it as a tab on the users profile, called "shop". I used the path users/%/shop. This works really well btw for others interested, when you include some exposed filters...
Anyway, the problem as you may have guessed already is that the profile tab "shop" is now shown on every bodies profile. Reading this page, it makes sense that the empty tab "shop", would go away on buyers profiles if I knew a little bit of php, for a second argument... Basically hide the view and tab if empty, would work perfectly, as buyers would never be able to create products...
I can't tell you much I'd appreciate some guidance...
Guy
Comment #31
gMaximus CreditAttribution: gMaximus commentedHi... I've been attempting to adapt your php validation to my site, in order to hide a empty profile tab / view...
Can i ask what does %d stand for in your SQL... Reminds me of getting a timestamp to display just the day or something...
I'm a noobie to PHP and this is the first time I've tried the php validator in views... I do understand SQL reasonably well though...
Also, what did you select as the argument? ie User:UID, Node: NID, Global: Null...
Can't tell you how much I'd appreciate any guidance....
Comment #32
gMaximus CreditAttribution: gMaximus commentedAh ha...
So i just worked out that by changing the type of validation to user, I could just tick the boxes for the roles who should have this tab... This in my case was the seller role....
Wanted to share and hope this helps others....
Guy
Comment #33
Petroslib CreditAttribution: Petroslib commentedHello i am using an argument Content:Has taxonomy term ID (with depth) please can you advice me how i edit the php code to hiding tab when view is empty
thanks in advance
Comment #34
geek-merlinI consider this a valid and often wanted feature request against current views.
Although earl verdicted a wontfix in #2, i see reasons to reconsider in the light of current menu integration and caching.
UI:
* Given a page display
* under "access"
* there should be a checkbox "prevent access when view is empty"
* with a big red sign "this can seriously affect performance, be sure to use caching"
Comment #35
elvismdev CreditAttribution: elvismdev commentedThe feature request by @axel.rutz is just what i am looking for at this moment in my project, i also would love to see this implemented in views.
Comment #36
Zythyr CreditAttribution: Zythyr commentedSubscribing.
Has anyone found a solution for this?
Comment #37
geek-merlinMoving to views-in-core.
Comment #38
Zythyr CreditAttribution: Zythyr commented@axel.rutz Moving to views-in-core? Does that mean the request feature/issue won't be fixed for Views in Drupal 7?
Comment #39
geek-merlin> Does that mean the request feature/issue won't be fixed for Views in Drupal 7?
New features are added at current version, then backported.
Comment #40
arua13l CreditAttribution: arua13l commentedSubscribing. Thanks. Looking for Version 7.x
Comment #42
Deno CreditAttribution: Deno commentedAs far as I can see, this is still not resolved in Drupal 8.5.4. Any idea if or when it will be resolved?
Comment #45
W01F CreditAttribution: W01F commentedWould be great to see this implemented:
Similar posts:
Comment #49
wxman CreditAttribution: wxman commentedI was just going to ask this question when I found this. Has this issue been worked on at all?