Needs review
Project:
Profile
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
8 Aug 2017 at 07:07 UTC
Updated:
23 Oct 2024 at 03:24 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
daggerhart commentedLooks like the profile label is never stored in the db, so the auto_entitylabel module won't work (tested to verify).
Digging into the profile module it appears the label does fire an event for overriding the label, so here is an example of code I'll end up using to alter the labels if some other feature is not created.
Register an event subscriber service:
mymodule.services.ymlEvent subscriber - set the new label and return the event.
mymodule/src/EventSubscriber/ProfileLabelEventSubscriber.phpNot ideal, but hope this helps!
Comment #3
daggerhart commentedNot sure if this is the direction we want to go, but here is a first attempt at a patch that adds labels proper to Profile entities.
Changes:
I suspect there are many problems with this atm, and I'm not suggesting we go with this patch as-is. Just wanted to get the conversation going around whether or not we want to go this route.
Feedback /suggestions welcome.
Comment #4
daggerhart commentedComment #6
citlacom commentedI have tested the patch and solved the issue of be able to set custom labels on profile entities in a project I'm currently working on. It seems that we will need to resolve the tests failures in order to have a definitive solution for this problem, right? Any direction to help on this?
Thanks.
Comment #7
bbuchert commentedApplied the patch but couldn't find the option to change the label. Should I find the new field here?
/admin/config/people/profiles/manage/myprofiles/form-displayComment #8
daggerhart commentedI think we need maintainer feedback on the desired approach. If adding label as a new base field is okay, I'm happy to re-roll patch with improvements and tests.
Comment #9
bbuchert commentedFrom a users perspective I would love to be able to just reuse another fields content for labels via a token. This would provide a lot of flexibility. This way It would be possible to reuse the content of a firstname field combine it with the lastname field as the label for the profile.
Comment #10
bbuchert commentedComment #11
bbuchert commentedComment #12
mglamanAlthough I generally run updates from the CLI, since Drupal allows users to use update.php we should queue around 10 at a time using the batch/sandbox parameter
I already didn't like we ran the event on this invoke -- because what if multiple are loaded, or Views does something stupid.
We should store on preSave, and always run. Because that way it is up to date whenever saved.
I have one request: Let's add an event subscriber to ProfileEvents::PROFILE_LABEL in profile_test. In the subscriber, just check if the field is equal to some value, and then modify.
Comment #13
marthinal commentedRerolled
Comment #15
daggerhart commented@mglaman thanks for the notes and direction
@marthinal thanks for the re-roll
Updated patch with the following:
I've tested this in various ways: This is definitely saving the labels to the database, and I was successfully able to use the autoentity_label module to provide a profile's label automatically using tokens.
Comment #17
daggerhart commentedWorking on making tests pass and I found an issue with the preSave() approach to setting the default label.
Since the default label wants the $profile->id() but the object doesn't have an id() before its first save, we can't expect correct default labels to be generated during preSave(). What happens is that the profile ends up with a label like: "Personal profile #" (without the id).
I've updated the patch to instead provide the default label during postSave() during first db insert ($update = false). Not sure this is the best approach, feedback welcome.
I've also updated
Functional\ProfileTest::testCreateEditProfile()to expect the profile's provided label to be the title for the edit form, as opposed to the label pattern provided by the defaultLabel().Comment #18
joelpittet@daggerhart I think he meant for the "some value" to be in the test not in the implementation to ensure/assert it works. Not literalEDIT: Ignore me, you did it right I think;)Comment #19
szeidlerThanks for your effort @daggerhart.
The patch is working great for new profiles. I'm wondering if it is a good idea, to fire it only on
if (!$update) {This basically seems to lock the label forever, until we have a UI for setting or prepopulating the label. Or was it only meant for covering the defaultLabel fallback?
That leads me also to the question how to proceed with the initial issue summary + title. Maybe we should split it up between the groundwork that was done by @daggerhart here and the initial UI based request.
Comment #20
imclean commentedComment #21
joelpittetReroll due to owner being added:
Comment #22
joelpittetAlso needs the update function updated.
Comment #23
joelpittetChange missing but interdiff was correct
Comment #25
asak commented@joelpittet - update function needs to update again... 8007 should work.
Comment #26
asak commentedComment #27
joelpittetLet's see
Comment #28
mglamanNeeds work - #26 failed to apply.
Comment #29
mglamanEntity access checks will cause this to return zero profile IDs.
This method is deprecated for grabbing the query from storage.
This method is deprecated for grabbing the query from storage.
Lets use the storage.
Deprecated function.
Comment #30
mglamanHere is a new patch, that should apply.
Review:
I wonder if we need to run entity key updates like the previous updates. I don't know for sure.
No need to override this method anymore.
Is this on any interface?
This should be on the parent already, correct?
These can be on the same line. Should we just check "isNew" versus not update?
Comment #31
mglamanNeeds work based on review.
Comment #32
joelpittetThanks for the review @mglaman.
Comment #34
joelpittetTest fail was bogus, setting to NR
Comment #35
mglaman@joelpittet thanks for the reroll to address my concerns!
Actually, the status report page. If there are no entity definition update warnings... then my concern was moot!
I feel like it should if the method is public. Does it appear to be internal, though? The only public usage is the update hook which updates existing profiles. And think we should probably skip this. There are Drupal Commerce sites with 10,000 or so profiles for anonymous users. Users with CRM or other instances can write their own update hook to change profile labels.
Yeah, I figured setOwner wouldn't be, as it is not the main interface method. It looks like methods like this to set the label key are always bespoke for the entity and should be declared on the interface as well.
:P no, lack of line context I did not realize the method signature
Comment #36
mglamanTwo more nits. I'm still concerned about the update process. Maybe we should just do an entity query for all where
uidis not zero/anonymous.Also... I'm putting to needs work because the issue title states this is about tokens and the user interface. Yet I don't see any of that here? And if the label is manually set, we no longer fire
\Drupal\profile\Event\ProfileEvents::PROFILE_LABEL. Shouldn't this always be fired and the "default" response is to use whatever tokens were configured for the profile type? The main thing I am seeing out of this patch is a performance boost by not always dynamically generating the profile title.But what about Commerce? Where the label is the address line. Now it would be static. There's a lot of questions here.
Let's make this protected. There is no visible reason for it to be visible... except in the update hook :/ So maybe we we can !$update && notEmptyLabel in preSave.
I think this is redundant as the default is always false.
Comment #37
joelpittet@mglaman,
uidconcern, can you elaborate?Are you suggesting we fire the event on every label change? I think I'm down with that but want to make sure that is what you are suggesting?
Comment #38
joelpittetThis makes the method protected but lets setLabel() deal with the empty values.
Comment #39
mglamanRad. I wasn't sure.
My concern is here, in the update hook
Loading anon profiles
Yeah, update becomes annoying. But feels weird to have it exist only for updates. I wonder if we could do a preSave to generate the label if its empty.
I guess. Or something. Maybe on each save? So instead of every time we view a label the event is generated, we just generate on preSave. This way the label is always generated with the latest labels. Right now the label is super static, generated only when the profile is first created.
Comment #40
joelpittet@mglaman I'm a little worried that if we have an empty check and the default returns empty that we end in a loop of ♾
Comment #41
emptyvoid commentedWhat if on the edit form for the profile a checkbox was displayed, "Update Profile Label".
Or have a configuration option to determine the behavior of how the label is updated in:
Configuration > People > Profile Types > Profile > Edit
Thoughts?
Is there going to ever be a display to browser the revisions of a profile?
Comment #42
joelpittet@emptyvoid That could be one way to solve it... I'd prefer to avoid config options if possible but if that's the only way this will work I'm interested.
Rerolled.
Comment #43
joelpittetRerolled for the update hook.
Comment #44
neograph734Hi joelpittet, I wanted to test this patch. But I am a bit misled on where to change the label. The title of this issue is "Make profile label work with tokens and expose them in the UI", but from the looks of the code I need an event subscriber to return the value of the label?
Comment #45
bojanz commentedI am definitely scared by the update hook. Drupal's update process is unreliable and we can potentially have hundreds of thousands of profiles on a site. It feels too late to do such a thing now that the module is widely used in production.
Is there a downside to only populating labels on preSave()? And documenting how a site could perform the update manually?
You can't add a public method to an entity if it's not on the interface.
Also, it looks really odd for a setter to care about setting a default value. We don't do that anywhere else in Drupal core or contrib.
There's a ProfileTest kernel test for testing profiles, but it wasn't expanded for the new functionality.
Comment #46
joelpittetI'm up for the preSave() approach if there are no objections.
Comment #47
joelpittet@bojanz Are you suggesting I add setLabel() to the interface profile interface?
Here's a WIP where I removed the update code label slow code(still has an update hook to create the field), and changed getDefaultLabel to protected.
Haven't done the test and preSave() solution, still pondering it
Comment #48
imclean commentedI haven't been following this issue closely enough so this is a general comment and question. Pathauto uses hooks to generate paths, presumably so it can use all entity data. Would these paths be available in preSave() or postSave() ? Or is that not relevant here?
Ref: #2972505-15: Pathauto 1.2 is not generating an alias after upgrading from 1.1
Comment #49
cuman commented+1
Applied the patch but couldn't find the option to change the label. Should I find the new field here? /admin/config/people/profiles/manage/myprofiles/form-display
Comment #50
joelpittetRerolled removing the test change from #47 This is still a bit of a Work In Progress WIP
@cuman I'm not sure if it gives the label field to change in the UI, I've not tested that.
@imclean I don't think that is relevant here but I guess it could be because it allows tokens to work with the label...
Comment #51
joelpittetNeeds work to tackle @bojanz's comments in #45 Especially the test. Anybody looking for this please give it a try.
Comment #52
dev.paulson commentedI am new to Drupal 8, Could you please help me on how this patch works? I mean how can i change profile label instead of default auto generated label. I was expecting a settings in admin profile configuration section but cant see a configuration to set that using tokens.
Comment #53
digitalcatalyst commentedhi, just a quick note I tried patch #50, it made a change to the schema for label, but not in a clean way (ie left with outdated entity even after a drush updb), usually id run a drush entup, but thats now retired in 8.7+ so need to use devel_entity_updates.
Uninstalling and reinstalling the module, now the auto_entitylabel is working as expected !
Comment #54
avo webworks commentedThank you @digitalcatalyst comment #53 was very helpful in terms of getting the patch to work. Thank you to all who worked on this patch!
Comment #55
chris matthews commentedThe patch in #50 is working well for me. Thanks!
Comment #56
darkstartom commentedI am in a similar situation as @dev.paulson. I have upgraded the profile module to 8.x-1.0 and successfully applied the patch in #50 but I don't see any way of using tokens to edit the label. I tried devel_entity_updates but it said there was nothing to update. Please can someone offer me some advice? Thanks!
Comment #57
darkstartom commentedI've had another attempt at getting this working and managed to install patch #50 on Profile 1.1 and then successfully ran devel_entity_updates which this time did make some changes. I am not able to uninstall and reinstall the module though as I have a large number of profiles set up already. I still see no way of actually editing the profile labels using tokens or otherwise. Has anyone got this working to a point where you can edit profile labels? I am still using Profile version 8.x-1.0-alpha5 on my production site because of this issue and really need to upgrade! Thanks for any suggestions.
Comment #58
joelpittetReroll
Comment #59
PhilippVerpoortI just installed and tested the patch in #58, and it worked like charm.
I also managed to modify the default label through an event subscriber, and I'm providing my code as an example here for others to make use of (hope this helps). This one just sets the profile label to the user's display label.
File 1: Create my_module/src/EventSubscriber/ProfileLabelSubscriber.php:
File 2: Add entry to my_module.services.yml
One more point: I also agree with the comment in #45 that the
setNamemethod should not use the default label in case the provided argument is empty. In fact it's reasonable that in some cases somebody would want to use thesetLabelmethod to remove any information stored in the database. I wouldn't want to strip anybody of the ability to do that. So I'm also for removing (what's currently) lines 115--117 in Profile.php:However, this is more of an API thing and I doubt it would affect whether this patch works or not, since the argument is provided in the only place where it's used (line 320).
But apart from that it all looks great, so please get this committed soon!
Comment #60
coaston commentedNone of this worked for me on 9.0.3, but this module did the trick:
https://www.drupal.org/project/auto_entitylabel
-i used tokens so it is perfect now.
Comment #61
achikas commented@coaston
how did you config this? I was trying it without no success and found this issue:
https://www.drupal.org/project/auto_entitylabel/issues/2909742
Is this patch working with D9 or will it be integrated in the next stable?
Comment #62
brad.bulger commentedI can confirm that auto_entitylabel does not work with unpatched 9.0.3. Applying the patch makes no UI changes to allow editing of a profile label. I am not sure why? Is there some desire to make that a separate issue/change?
Using auto_entitylabel, the "Re-save" functionality requires the patch from #3201476: ResaveBatch doesn't work for custom entity types to run without error. It only updates existing profiles if you choose the " Automatically generate the label and hide the label field" option, since there is no label field I suppose. For new profiles, the "auto generate if field left blank" also works.
Comment #63
RyanCMcConnell commentednevermind, user error
Comment #66
mlncn commentedThe version of the patch in the merge request avoids trying to update existing labels. Instead, it allows the stored labels to stay blank, and keeps exactly the same behavior as before if that is the case. Now that we have a label base field where labels can be stored, however, this module can now be used with Auto Entity Label, which i think should be the main goal of this issue.
https://www.drupal.org/project/auto_entitylabel
There is not a need for this module to provide its own UI for altering the label pattern when Auto Entity Label exists.
With this merge request the only thing happening in the update hook is the necessary addition of the label field to the profile entity base, so there is no longer a risk of errors or timeouts when the update hook is run. Also, if people do not run the update hook right away, everything continues to work.