I'm using Nodereference Count to show the number of child references to each parent in a list (e.g. how many kids belong to each hotel guest). So I have a LIST view for guests, and I've first added this NRC cck field to the content-type, and then as a view field so that in a list of guests each is shown with their number of kids. Fine.
However when an individual guest is viewed (normal node/page view) -- this field is again shown (except when the count is ZERO), and in some default location within the other page data. But I don't want it to be shown, because I have a separate block laid out for this.
It seems that because I first needed to add this field to a content-type, this causes it to be displayed in the page view.
1) Is there a module settings panel that I'm missing? If not by UI setting then is there a simple way to hide a field in page/node view? Am I doomed to theme or CSS trickery?
Note: I see that when adding the filed to a view there's a check box "Exclude from display", but checking this results in the field not being shown where I've added it to a view, but it still shows on the default node page, and there isn't any similar checkbox when creating the field in the content-type.
Other questions/issues:
2) A setting to hide this field when the count = ZERO would be nice. (I need both this and #3 bellow so I'll need to hack this module a bit).
3) I use Workflow, so when nodes are not yet approved they are hidden from the base user role (except for the author). Here, Noderefernce Count includes the nodes that are hidden, but I'd like to exclude them in the count. I assume this is because it queries for "published" nodes, whereas Workflow requires that nodes be published throughout the workflow states. Workflow creates similar issues with some other modules, and I realize that asking for changes that might use permissions hooks would be a stretch. This probably a drupal7+workflow discussion...
Thank you!
Comments
Comment #1
mudd commented(sigh) Why is it that a solution so often is stumbled upon mere minutes after posting...
1) Content-type | Display fields <-- this is where I found the means of turning off the field.
But #2 & #3 still stand :)
Comment #2
gilgabar commentedNumber two is easy. I actually started to write instructions on how to do it yourself, but then I decided to just add it to the module. It should be available in a dev release sometime today. It's just an extra display formatter. So the setting you discovered in #1 above, just change that to use the new 'Non Zero' formatter instead of 'Default'.
Number three is more difficult. The counting is actually much simpler than what you describe. It currently doesn't even concern itself with published status. The other two active issues for this module both fall into the same category as this (one of them about counting only published nodes). They basically involve customizing the query to count different stuff. There are lots and lots of possible ways to refine the count based on different factors and if I were to just keep adding stuff I would end up recreating Views which is not something that needs to be done. So it will take some time and thought to figure out a better approach to allow this sort of arbitrary feature addition without bloating things. Ideas are always welcome.
Comment #3
mudd commentedgilgabar, Thank you for adding this feature (#2) -- I'll be looking for it!
As for Workflow states (#3), I haven't put a lot of thought into this but here are two quick ideas:
I only have one state for which references should be counted -- the final posted state, which is SID=7 (and I'm only using Nodereference Count for one field, so hacking is ok for now), so this query seems to work:
Comment #4
gilgabar commentedThe nonzero formatter is available in the 1.1 release. Not sure why a new dev release was never generated, but there was another bug fix that was overdue for a full release anyway.
Since we appear to have addressed the other issues I'm going to change the title to reflect the focus of the remaining issue.
I think a PHP block is a good suggestion, but a bad idea. That is, its a good starting place for thinking about how to address the issue, but on balance its essentially just allowing you to store a hack in the database with all the security, performance, and code management issues that entails. I would prefer to just encourage folks with particular needs to hack it for real instead.
The options as I see them are: a) leave things more or less as they are and encourage people to hack in their special requirements. b) Create a mess of concatenated SQL controlled by checkboxes in the CCK settings. c) Borrow a query builder to generate the SQL for us, also controlled by checkboxes in the CCK settings.
Option A isn't a very good option, though it is currently where things stand.
Option B would not be an unreasonable solution for the current number of requests in the queue for filtering the counts. There are plenty of other ways to restrict and customize the counting of nodereferences though. I'm not excited by the idea of trying to maintain this once the number of things to filter gets really big.
Option C is something that I'm not even sure is possible. Given that once we get carried away adding filters for things to count we are essentially reinventing something Views already does quite well, it seems like it may be possible to actually make use of Views to do some of the work for us. This would take some research. The query building facilities of DBTNG in Drupal 7 may also be of some help, but where things stand with nodereferences in Drupal 7 is something I'm not familiar with currently. That probably deserves its own separate topic.
Those are my current thoughts. There are plenty of people smarter than me on here, so if that's you feel free to chime in.
Comment #5
mudd commentedThanks for the update! Here's some testing feedback. (perhaps I'm doing something wrong, but there might be a condition that's not considered)
It seems the label isn't being suppressed in Views when using Format Non Zero and the count is zero.
I'm using the field in a View (Drupal 6.x is Views-2 (right?), and I check "[x] Rewrite the output of this field" and then enter:
"[field_numdatasets_value] attached datasets", and then set Format: Non Zero (and also select None for the Label)
This produces the correct output when the count is greater than zero (e.g. "5 attached datasets"), but when it's zero...
a) even though the "0" is now gone I still get the text portion of the rewrite string: "attached datasets",
b) similarly, when I don't rewrite the output and set a custom label it also still shows the label, and
c) if just use the default Widget Label I see the same -- the number (count) is suppressed but not he label.
In the default page view however I'm seeing that the Label is properly suppressed as it's supposed to be :D
Comment #6
gilgabar commentedThat sounds like the expected behavior.
Views handles titles and rewritten output separately. I believe, but I'm not positive, that in the node view the title disappears because the formatter is passing no value and CCK doesn't bother adding a title if there isn't anything for it to display. Views handles its own titles, so the behavior is different.
There are a couple approaches you could take depending on the circumstances:
- Filter the View by nodereference_count and don't return rows where the count is zero. That isn't much use though if you still want to return the rest of the row.
- Create your own CCK formatter to add the "attached datasets" part to the field. You don't even need to hack anything to do it. Create a new module or add this to an existing one if it makes sense:
Then enable your module, clear your caches, and visit the CCK display fields page to confirm that it is there. You should be able to see it in Views as well.
There are probably other approaches that also work that are not obvious to me off the top of my head.
Comment #7
mudd commentedThat works great! I didn't know it was so easy to reformat fields.
Thank you so much for adding the non-zero feature and writing that filter for me!!
Comment #8
gilgabar commentedI don't think there is a good solution for the custom count filtering in d6. In d7 I believe hook_query_alter() should cover custom use cases nicely. A tag 'nodereference_count' on the query should allow other modules to alter the way node references are counted to whatever degree is necessary.