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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rfay’s picture

Great idea. It may be you that does it when you figure it out... Patches are welcome!

Wappie08’s picture

Yes 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

Mile23’s picture

Assigned: Unassigned » Mile23

I'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. :-)

Mile23’s picture

FileSize
22.67 KB

In fact, here it is without the tests.

Mile23’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 1229322.patch, failed testing.

Mile23’s picture

Category: support » task
Status: Needs work » Needs review
FileSize
33 KB

Here'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.

Mile23’s picture

Oops, 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...

Mile23’s picture

Status: Needs review » Fixed
rfay’s picture

Mile23++

Mile23’s picture

Fixed 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.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.