I have created a hook within a custom module with hook_nodeapi() that allows me to restrict the viewing of a set of defined content types to the owner of the node. I have done this by setting the $node value passed to false when the $op param is 'view'.
This works well until I use it with a CCK content type as within the content.module the _content_field_invoke_default() function assumes the node is valid.
Now not sure if what I have done is regarded as "bad practice" but I get the following error when my hook is invoked
warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\test\sites\all\modules\cck\content.module on line 1284.
I have got round this by putting an if($type['fields']){} round the foreach loop. But I would be greatful for some feedback as to whether I should approach this some other way, keep 'moding' any CCK update I need to add my "patch" or if the code in the CCK module could be updated to take into account the invalid node scenario.
thanks,
St.
Comments
Comment #1
markus_petrux commentedYou should look at how node access rights work in Drupal.
http://api.drupal.org/api/group/node_access/6
There's also an example module in that page to help you learn how it works.
Comment #2
Anonymous (not verified) commentedIt may be miss-understanding on my part(and thats is probably the case) , but to use node access rights I would need to use the hook_access function in my module and/or I would need to set up grants for each user/node id etc. using hook_node_grants.
This seems a lot of work to what appears to me to be a simple use case. hook_access does not work as I am not defining the node type I am setting it up with CCK outside of the module( I have already tried this until I found out hook_access only works with nodes defined within the module). Guess I will just need to keep editing the CCK stuff.
thanks,
Comment #3
markus_petrux commentedhook_nodeapi() is not suitable to check for view access. AFAICT, this should be done using hook_node_grants() because that's what node module uses to grant/deny access to view a node, in node view, in node lists, etc. Other than that, you may try to hack here and there, but you may leave holes.
In other words, in regards to "Now not sure if what I have done is regarded as "bad practice" ..."
Yes, you're right here, AFAICT.
You may try asking in the development forums or file a support request to the Drupal issues queue, node module component.
If you want to hide CCK fields to certain users, then use hook_field_access(). See content_access() in content.module, or use Content Permissions shipped with CCK itself, or use Field Permissions module (recommended over Content Permissions). That's all CCK can do for you, I'm afraid. Beyond fields, yours is a node module related question.
Comment #4
Anonymous (not verified) commentedOK Markus - thanks for the guidance.