Hello, thanks guys for all the nice example implementations, they are really helpful!
I was just looking at hook_node_access(), and then to hook_node_access_records() and hook_node_grants(), this is explained well in both the example node access module and in "Drupal 7 module development".
What I'm missing though is a good explanation and maybe an example of hook_field_access and how it works.. I looked at:
http://api.drupal.org/api/drupal/modules--field--field.module/function/f...
http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...
But that is not very informative.. And I didn't find any example at all when googling!
For example I want to create a field_user_privatefield in the user entity, can I just implement hook_field_access for op='view' for that field? Aren't there other circumstances like listings (this is the reason why one has to implement a hook_node_access_records() for nodes if I'm right).
Thanks in advance for any clarification/help, I think this will benefit many developers!
Greetings Wappie
Comment | File | Size | Author |
---|---|---|---|
#7 | 1229322_field_permission_example.patch | 33 KB | Mile23 |
#4 | 1229322.patch | 22.67 KB | Mile23 |
Comments
Comment #1
rfayGreat idea. It may be you that does it when you figure it out... Patches are welcome!
Comment #2
Wappie08 CreditAttribution: Wappie08 commentedYes I can try, but first I need some good hints/explanation here about the working, scope and pitfalls of hook_field_access, maybe if someone explaines I'll try to make a nice and short example!
Greetings Wappie
Comment #3
Mile23I've got a project going. I just need to finish the tests.
But of course if anyone's got other code we can do some merging. :-)
Comment #4
Mile23In fact, here it is without the tests.
Comment #5
Mile23Comment #7
Mile23Here's what I made:
A field that implements hook_field_access(), as well as hook_permission(). Users can be given all combinations of view/edit any/own field.
It creates a field that's a simple styled text field, with CSS to look like a post-it note. For simplicity, this field has no settings. It's the absolute minimum field you can make, I suppose.
It also has tests to verify that it's all working and that the access disallows what it should.
An aside to any Drupal DX experts: Compare the size and complexity of the tests to the field implementation itself to see where it might be nice to have a few generic field tests built into Drupal. Implementing the field took a few hours. Documenting the field took a few more hours. Writing the tests took many days, off and on.
The tests are designed to be re-useable, so dig in.
Comment #8
Mile23Oops, missed that I needed to change the descriptive page. And no one raised objections about anything else, so here's the commit:
http://drupalcode.org/project/examples.git/commitdiff/1b399daa8739d8cf8a...
Comment #9
Mile23Comment #10
rfayMile23++
Comment #11
Mile23Fixed an unescaped double-quote that would, for instance, give you the WSoD if you visited the testing page.
Basically I feel bad that my errors got committed. But now my fixes are, too.
http://drupalcode.org/project/examples.git/commitdiff/3799559aed04b764bb...
Also scratched my head for a while about huge error messages during testing when using PHP 5.4.4 with MAMP. Some questions answered: #1671200: Simpletest broken: CURLOPT_COOKIEJAR cannot be NULL which is fixed in D7 dev. The best solution: Upgrade to 5.4.5.