Closed (won't fix)
Project:
Examples for Developers
Version:
7.x-1.x-dev
Component:
Node Example
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
15 Nov 2011 at 20:52 UTC
Updated:
3 Oct 2014 at 19:17 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
rfayIt would be perfectly reasonable to add an example of this, probably in NodeAPI Example. Add some info to the $node->content just as illustrated in http://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/...
Comment #2
bobbyaldol commented@ray: the function theme_example_node(in the patch that follows) should output a more reasonable way to display the information. I couldnt think of any, may be this requires some work
Comment #3
rfayStill some work to be done on this.
1. You implemented node_content_view(), but hook_view() should be node_example_view().
2. Please make sure to explain that hook_view() is quite unusual, and that a person would normally use hook_node_view(), which does the same purpose. I suspect that hook_view() is a relic from an earlier version of Drupal that just didn't quite get dropped.
3. For clarity, please use 'content_type' instead of 'type' in your theme function.
4. Don't forget to add a test.
Thanks for all your effort and contribution on Examples!
Comment #4
BrockBoland commentedThere's a more fundamental problem with this example:
hook_view()is only invoked on the module that defines the node type. That is, it will only run for node types defined in the module's implementation ofhook_node_info().And that's where the trouble starts, because there is no
node_example_node_info(): thenode_examplenode type is created innode_example_install()instead, sohook_view()won't ever be invoked.I'm not sure how best to address this. We could re-work the module to define the node type in
hook_node_info()instead of creating it inhook_install(), but I'm not sure how easy it is to create default fields and such when done this way.Comment #5
rfay@BrockBoland, I kind of suspect that hook_view() is just a dinosaur. What do you think?
Comment #6
BrockBoland commented@rfay, I don't know about that: I think it's still worth demonstrating. In most cases where a module is defining its own content type, its because it will need some custom functionality beyond the addition of fields (which, of course, would not require anything in a
hook_view()implementation). Take poll for example: it makes sense forpoll_view()to build out the view of a poll, since that hook will run beforehook_node_view()is invoked on other modules. This ensures that$node->contenthas been built out with the poll form or results before other modules try to act on it. Ifpoll.moduleimplementedpoll_node_view()instead ofpoll_view(), another module with a lower weight could try to modify the poll in its ownhook_node_view()implementation, before the poll module had actually initialized that content.I know it's not a common use case, and poll is really the only example I can think of at the moment, but I think it would be worth including an example.
Comment #7
mile23hook_view() would properly be demonstrated in node_example, since it implements a node type.
However, node_example doesn't implement a node type the right way. :-) It doesn't use hook_node_info(), using node_type_save() in the install phase instead. Not best practices in D7 for a bunch of reasons. I'll make a new issue for that.
Anyway, hook_view() is pretty much a D6 throwback, and also pretty much deprecated, I'd say. Showing how to implement a pseudo-field would be better.
Comment #8
mile23I'm calling this closed.
If anyone wants to pick it up, go right ahead.