Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
At @Miles23's suggestion, I've ported the Field Permissions Example from Drupal 7 to Drupal 8.
The port is pretty faithful, although the first pass does not have a test; I have something I can port from the Rules module, but if we need it, it'll have to wait until I get something that generalizes easily.. Includes a test based on the simpletests used to test fields in Core.
Comment | File | Size | Author |
---|---|---|---|
#27 | issue-2581559-field-permissions-example-v10.patch | 28.65 KB | Torenware |
| |||
#27 | interdiff-v10.txt | 5.59 KB | Torenware |
#24 | interdiff-v9.txt | 6.06 KB | Torenware |
#24 | issue-2581559-field-permissions-example-v9.patch | 28.58 KB | Torenware |
| |||
#19 | interdiff-v8.txt | 767 bytes | Torenware |
Comments
Comment #2
Torenware CreditAttribution: Torenware as a volunteer commentedComment #3
Torenware CreditAttribution: Torenware as a volunteer commentedFound a couple of typos, and rearranged the module file to a more logical order.
Comment #4
Torenware CreditAttribution: Torenware as a volunteer commentedCorrect handling of users requires a little more code; now added.
Comment #5
Torenware CreditAttribution: Torenware as a volunteer commentedProbably a good time for some feedback. There are probably some typos and formatting issues that need fixing, but this should give you a sense of how I approached it.
The code works, and while I haven't posted it yet, I have a test based upon the ones used by the Field module in core (SimpleTest KernelTests, because that's what's currently in core for fields). These cover enough cases that I have some confidence in the code.
Comment #6
Torenware CreditAttribution: Torenware as a volunteer commentedI've gotten things pretty well cleaned up, and scanned the verbiage to make sure that it reflects changes in the code between versions.
Pretty happy with the test class. Run it as
to see what combinations it hits. 36 in all. Plus the regular tests that core plugins get. I've just checked against today's RC1-post HEAD; all tests pass.
Hopefully this does what you need. I suspect that you'll want some formatting changes, but this should be close to what you want, I think.
Comment #7
Torenware CreditAttribution: Torenware as a volunteer commentedChecked against phpcs with drupal policies. All errors have been fixed, most warnings as well.
I'm not sure if anything needs to be changed to fit documentation standards, but at this point, it looks like Drupal code.
Comment #8
Torenware CreditAttribution: Torenware as a volunteer commentedNoting #2567501: [meta] Add _permission: 'access content' to applicable routes; I'll post another patch to handle this, already done locally.
Comment #9
Torenware CreditAttribution: Torenware as a volunteer commentedJust discovered #2102699: Port field_permission_example module to Drupal 8; making this a child and claiming the parent.
Comment #10
Mile23Comment #11
Torenware CreditAttribution: Torenware as a volunteer commentedStarting this one back up. Test is now in.
The new patch is not interdiff-able with the old ones for some reason. There will be interdiffs going forward.
Comment #12
jerryimiolo CreditAttribution: jerryimiolo as a volunteer commentedOops. I made an error. Not the right issue. Please except my apologies.
Comment #13
Mile23No worries. :-)
Comment #15
jlnd CreditAttribution: jlnd as a volunteer commentedPatch applied properly (although there were whitespace warnings).
After enabling, there was no menu item added as a child of "Examples" in the toolbar.
When I visit the admin page for the submodule (/examples/field_permission_example) I get an Access Denied error.
Comment #16
Torenware CreditAttribution: Torenware as a volunteer commentedAnyone have advice how to get "git diff" to generate patches that don't have these errors on "git apply"? I'm using a Mac, and beginning to think that's a Mac related thing.
Ah, I see that the Block Example does that. I'll be the monkey in Monkey See, Monkey Do, and add this.
This is new misbehavior since I last did the dev on this. I'll see it also, and I'll see what's up with that.
Comment #17
Torenware CreditAttribution: Torenware as a volunteer commentedOK, just discovered hook_toolbar, and apparently, *.routing.yml uses _permission instead of _access. So let's try this one on for size.
Comment #18
jlnd CreditAttribution: jlnd as a volunteer commentedPatch applied properly (no warnings). The "Field Permission Example" menu item is in the toolbar. No Access Denied error. All tests passed.
The tests file (/src/Tests/FieldNoteItemTest.php) contains calls to deprecated functions ( entity_create, entity_get_form_display, entity_load ).
One more tiny thing... line 46 of field_permission_example.module is missing the word "in".
Comment #19
Torenware CreditAttribution: Torenware as a volunteer commentedFixed the typo.
Comment #20
jlbellidoThis is still needed :
Thanks!
Comment #21
Torenware CreditAttribution: Torenware as a volunteer commented@jlbellido -- we may want to agree what's a reasonable level of "deprecated" to removed it from Examples.
entity_create(), for example, is slated to go out, but only in Drupal 9.0. It is still listed as used > 450 places in Drupal 8.x.
I can remove these, but I think it makes more sense to get valid code checked in, and deal with 9.0 deprecation issues over the next several years, since 9.0 is likely more than 3 years out, and possibly as many as 5.
Comment #22
Torenware CreditAttribution: Torenware as a volunteer commentedI've created #2662976: META -- how do we deal with deprecated APIs in 8.x-1.x, since I'd like to get a consensus as to how we handle APIs that are still in heavy use in Core (which is where I got the code @jlbellido is referring to), but are planned for removal in 9.0.
I'd argue we need to keep this issue in mind, but do not need to get very far ahead of Core in doing it.
Comment #23
Mile23I doubt it will take 3 years to do. :-)
This is a documentation project where we document best practices. Doing follow-ups means we'll commit code that does the opposite of demonstrating best practices.
You could un-assign yourself if you want someone else to deal with that part. Everyone still gets commit credits. :-)
Comment #24
Torenware CreditAttribution: Torenware as a volunteer commented?? Really??
In any event, here's the corrected code.
Comment #25
Mile23All I mean is that if it's frustrating or takes too much time or whatever then don't sweat it. There's been some degree of burnout on this project.
But, since you went ahead and worked on it: Thanks for that. And for all the other stuff, too.
Here's a review:
This whole docblock needs to wrap to 80 chars. Low priority though because I can fix coding standards stuff on commit.
This isn't actually true.
$this->container
is the same one the kernel under test uses, and is also the same as\Drupal
during the test run. It's correct that we want to use the supplied container, however.This is a great idea, but it's untranslatable. We have to use the controller that returns translated text.
Comment #26
Torenware CreditAttribution: Torenware as a volunteer commentedNo need, already set up with drupalcs and kin. I'll reflow it.
What I meant here is that you can't (usefully) implement
ContainerInjectionInterface
, since this assumes the constructor will declare its injections (for a simpletest-style test, the constructor takes the test ID, and gets pissy if you do anything else). I will clarify the comment accordingly.You can, as you point out, use $this->container to get at a reasonable container. Note that I'm already doing this to get at the entity storage.
I forgot to wrap the HTML in a
{% trans %}
block. I've just done this locally and tested this withdrush potx multiple --api=8
. It will work fine once I check in the modified template:This should resolve nicely on localize.drupal.org, or in any POT editing tool. And will be easier for a translator to handle, based on my experience with that crowd :-)
Comment #27
Torenware CreditAttribution: Torenware as a volunteer commentedI've modified the comment, run the whole module through drupalcs, and inserted the needed L10n blocks into the template. Should be close enough for gov't work.
Comment #28
Mile23Thanks.
Well, that's by design...
$this->container
is where you get it and use it if you need it. That way if you have a special-case test base class, it can poke its mock/fixture services into the container. That can include the fixture kernel.Oh, nice. I didn't know that existed. I think that's a good solution for the other modules, too.
Review in a bit...
Comment #30
Mile23Yay global sprints. :-)
There were some wrap-at-80 errors, fixed on commit.
Fixed and committed.
Thanks for sticking with it.