I was looking for examples of entity field access code for Drupal 8, and didn't find it. So I wrote some :-)

The enclosed patch adds a number of features to the Content Entity Example:

  • A role-based implementation of hook_entity_field_access
  • Config files, including a simple settings file, and two pre-defined roles so the examples are easier to set up.
  • Simple config admin UI that turns field access control on and off.
  • A preSave setting of the name field (now labeled "Display Name")
  • A dynamic title for the Edit form.
  • A couple of new fields, last_name and volunteer_rating, as an example of data you'd want to restrict access to.

I haven't added any tests, but could if that's really needed.

CommentFileSizeAuthor
#2 issue-2580771-implementation.patch14.65 KBTorenware

Comments

Torenware created an issue. See original summary.

Torenware’s picture

StatusFileSize
new14.65 KB

Added first draft of the feature's patch.

mile23’s picture

Status: Active » Needs work

Wait, so hang on... :-)

I appreciate the enthusiasm, but this is a bit out of scope for the content entity example, which is a bit big already. The goal of Examples is to have single-focus modules as much as possible. That way we're not confusing the matter for the person reading the code.

What we might want to do here is get you porting (or just re-making) field_permission_example.

Just make a simple field implementation (It can just wrap a text field) and give it some novel display. In the D7 version it uses CSS to look like a post-it note. And/or it could just specify a node type with attached fields in YML.

Then give it the permissions-based behavior. Being able to turn that on and off is a nice feature.

Thanks.

Torenware’s picture

@Miles23 --

I figured that'd be an issue. Believe it or not, this is a simplified version of something more complete. Which will ultimately become a blog post, I think.

I've taken a look at the D7 implementation of field_permission_example. I can certainly port it, and I'll look into doing that tomorrow. But we already have an example of a field type plugin. The content example is a bit abstract, and it probably isn't too hard to add a couple of things that will make it a bit more useful. It's certainly a balancing act, but then, part of the reason the Content Entity Example is complex is because the API itself is complex, and not that well documented. You also want an example that helps people get started on that, without requiring them to reverse engineer some of this stuff (which I had to do the write the patch).

Some of the value added of what I was trying to do were things that are not very well documented at this point, and are parts of how one would implement a realistic content entity. Setting the title for an edit, for example, is something people do want to do, and it's not documented at all yet (see #2218311: Document _title_callback for example: AFAIK it was only documented today, and by me at that). Also, creating config files that uninstall with the module (as my role files do), while documented, isn't documented very well. The config stuff doesn't really belong anywhere else, but it doesn't add too much to any given example.

I know that the idea is to keep the examples as simple and as focused as possible, but to the extent possible, we should probably also try to make clear things where the documentation can be overly abstract. Anywhere I can help do that I'm happy to help with.

mile23’s picture

Assigned: Torenware » Unassigned

Generally unassigning issues. Please re-assign yourself as desired.

avpaderno’s picture

Component: Content Entity Example » Code
Category: Feature request » Task
Issue tags: -entity API, -Entity Field API