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.
Problem/Motivation
The member variables of Flag::types and Flag::entity_type are currently public. There are no accessors for either variable. This breaks the isolation that is supposed to be provided by FlagInterface.
Proposed resolution
Change Flag::types and Flag::entity_type to protected. Create an getter and setter in FlagInterface.
Remaining tasks
Create patch.
User interface changes
None.
API changes
The target variables will only be accessable via accessor methods.
Comments
Comment #1
socketwench CreditAttribution: socketwench commentedTurns out there's already a getFlaggableEntityType(), so now there's a getFlaggableBundle() too.
Comment #3
joachim CreditAttribution: joachim commentedShouldn't this always be returning an array, possibly empty? It's certainly better DX to not have to is_array() what you get back from this.
Comment #4
socketwench CreditAttribution: socketwench commentedYes, that would make more sense now that I'm awake enough to comprehend it... >_<
Comment #5
socketwench CreditAttribution: socketwench commentedComment #7
socketwench CreditAttribution: socketwench commentedComment #9
martin107 CreditAttribution: martin107 commentedI am retesting, locally it works for me...
Comment #11
martin107 CreditAttribution: martin107 commentedThis makes sense to me.
As for the "Do we return an empty array or null" thing
That opens up a schism between developers that is never going to be settled.
Me: I put the tea in before the milk ... any thing else is just crazy :)
Pragmatically this should go in as locking down the interface is the more important concern.
Comment #12
BerdirNo worse than before, but this will fail if no bundles are checked, it will generate and invalid query or throw an exception, I'm not quite sure what.
Then again, I think I've seen a issue somewhere that multiple access check is currently dead code anyway..
With a method, what you *could* do is check for empty and then return all bundles of that entity type...
Then you somehow need to check for that here, however, e.g. with a separate method that returns true/false.
Comment #13
martin107 CreditAttribution: martin107 commentedAs you say no worse than before....
My proposal, for now, is a sticking plaster - I have just copied the warning that the Flag:type annotation provided into the FlagInterface return type where in the new scheme of things I would expect to see it.
Step 2 file another issue to add the new FlagInterface methods to cover the concerns that you raise.
N-people will have N different ways of dealing with this ..and naming things is hard so I will start the ball rolling...
I think this could be used to fix all cases.
Comment #14
socketwench CreditAttribution: socketwench commentedYep. FlagService doesn't currently depend on ModuleHandlerInterface, so we're not even calling any hooks yet. More on that here: https://www.drupal.org/node/2467915#comment-9813823
If getFlaggAbleBundles() returns an empty array here, we won't get any extra psudofields.
Comment #15
socketwench CreditAttribution: socketwench commentedComment #16
joachim CreditAttribution: joachim commentedFollow-up 1: consider ways to improve the DX for the 'applies to all' case. My preference would to hide the implementation, so the getFlaggableBundles() method internally handles giving you all the bundles if the stored value is empty. I do have a sneaking suspicion though that this could cause problems in some cases...
Follow-up 2: let's change this confusing 'types' to 'bundles' or something like that. I thought I'd filed an issue ages ago about renaming various properties of the Flag entity, but I can only find #2457059: Rationalize fields fid and type on flagging entity .
Patch looks good to me: +1 to #14 and I'll commit if we're all happy with that.
Comment #17
joachim CreditAttribution: joachim commentedOn second thoughts, I think this needs to be done differently here, rather than in a follow-up. If this method were called getTypes(), then I would expect it to return the $flag->types property verbatim. However, getFlaggableBundles() I would expect to literally all the bundles that apply -- and do the legwork of converting an empty $types array into an array of all the entity type's bundles.
Working on this now.
Comment #18
joachim CreditAttribution: joachim commentedComment #20
joachim CreditAttribution: joachim commented> However, getFlaggableBundles() I would expect to literally all the bundles that apply -- and do the legwork of converting an empty $types array into an array of all the entity type's bundles
Duh, except as this patch stands, #2409835: [regression] subtypes option should not be required: leaving empty means it appliest to all isn't in yet. So that's immaterial, and adding that second method belongs in the other issue.
Comment #21
joachim CreditAttribution: joachim commentedUpdated patch.
The difference between this and #14 is just a change in the method name getFlaggableEntityType() to getFlaggableEntityTypeId().
Comment #23
joachim CreditAttribution: joachim commentedOops. Our service tests were still accessing $types directly.
Comment #25
joachim CreditAttribution: joachim commentedWow. Everything's failing.
Has something changed in core in the last 12 hours maybe?
Comment #26
joachim CreditAttribution: joachim commentedHuh now it's passing. And the test result page doesn't say what timezone it's showing times in, so I don't know when that happened.
Comment #27
martin107 CreditAttribution: martin107 commentedI have noticed lots of seemingly random test fails while working on other flag issues over say a period of about 2 weeks....
Lots has changed in Drupal core over that time ... and in particular the last few days as the sprints from DrrupalCon LA wind down.
Is you suspicion that is a design instability with flag, or maybe a test infrastructure thing?
Comment #28
joachim CreditAttribution: joachim commentedI think it was a test infrastructure thing. The test report said it was failing, EVERYWHERE, all tests, with a message that seemed to be to do with the test's internal browser. And when I checked again an hour or so later, it was all green...
I'll maybe send it for a retest now, to be sure.
Comment #31
socketwench CreditAttribution: socketwench commentedComment #32
joachim CreditAttribution: joachim commentedCommitted. Thanks everyone!