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.
I found an issue where sometimes the label is returned as a string by $plugin->getLabel() when using ggroup. This patch addresses the issue by using (string) to cast to a string in every case regardless of whether _toString() is set or not.
Comment | File | Size | Author |
---|---|---|---|
#18 | 2933819-18.patch | 963 bytes | ricovandevin |
| |||
#7 | 2933819-7-superflex.patch | 2.28 KB | seanB |
| |||
#7 | 2933819-7-docchange.patch | 718 bytes | seanB |
| |||
#2 | group-cast_plugin_name_to_string-2933819-2.patch | 1.25 KB | arosboro |
|
Issue fork group-2933819
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
arosboro CreditAttribution: arosboro at DropForge Labs commentedComment #3
arosboro CreditAttribution: arosboro at DropForge Labs commentedComment #4
kristiaanvandeneyndeThe label should never be a string but always an instance of TranslatableMarkup. Sounds like a bug in the ggroup patch. See GroupNodeDeriver for an example in case the bug is in a deriver.
Comment #5
idebr CreditAttribution: idebr at ezCompany commentedWhile I agree the label should be TranslatableMarkup, the GroupContentEnablerInterface::getLabel() currently @returns a string. Since the patch results in an identical implementation but without the backwards compatibility break, it is superior to the current code in HEAD.
On a related note: using magic methods on classed objects to use as array keys is not a very robust way to generate sections, but Iet's discuss that in another issue.
Comment #6
kristiaanvandeneyndeCore is full of examples where an @return mentions "string" but it actually returns translatable markup. I frown upon this myself, but I guess the intention was to say "You may treat this as a string".
I'd rather update the erroneous docblock to mention the true return value than adjust the return value. If people currently have code that calls
$returned_label->getArguments()
for instance, it would now crash. So changing it to always cast the return value as a string, could be regarded as a BC break.Comment #7
seanBUpdated the ggroup patch to fix this (see #2736233-149: Port Subgroup (ggroup) to the D8 version). Also attached a patch to fix the docs, as well as a patch to make getLabel() a bit more flexible. I think I prefer to keep the docs an make sure the methods actually return a string, but I guess it's up to you to make the final call on this.
Comment #8
seanBComment #9
idebr CreditAttribution: idebr at ezCompany commented#7 The 2933819-7-docchange.patch would make sense, but in 2933819-7-superflex.patch you return a string instead. Are these alternative approaches to fix the problem at hand? I fail to see the logic :(
Comment #10
seanBYes, these are alternative approaches. One changes the documentation to match the implementation and the other changes the implementation to match the docs.
The plugins returning a string for a label were failing (this changed somewhere between rc1 and rc2), even though the docs are saying a string is what should be provided. I'm not sure if we should really change the API from a string to
FormattableMarkup
/TranslatableMarkup
when we could easily accept both and not break anything. Besides that, almost all implementations ofgetLabel()
in Core return a string instead ofFormattableMarkup
/TranslatableMarkup
, so that's another reason why I slightly prefer returning a string.Comment #11
idebr CreditAttribution: idebr at ezCompany commentedClosed #2973005: Fatal error on permissions page - call to __toString() on a string. as a duplicate issue.
Comment #12
borisson_There is no need to call
$thing->__toString()
on those objects. The same thing is to do(string) $thing
. I'd prefer that. I agree with the reasoning in #10. Let's return strings.Comment #13
idebr CreditAttribution: idebr at ezCompany commentedComment #14
idebr CreditAttribution: idebr at iO commentedDrupal core has updated the variable type to
string|\Drupal\Core\StringTranslation\TranslatableMarkup
, see #2921307: \Drupal\Core\Entity\EntityTypeInterface label methods docblocks should be to return TranslatableMarkupIdeally this issue should apply the pattern in Drupal core.
Comment #15
kristiaanvandeneyndeFollowing core is (almost) always the right thing to do, agreed.
Comment #18
ricovandevin CreditAttribution: ricovandevin at Finlet for iO commentedUsed suggestion from #14.