I'm trying to put the SKU field in a field_group_table, and it turned out that Commerce uses #markup for the "extra" fields. This is bad practice and has implications for any module wanting to make changes using the render API.
This is the code it's about:
$entity->content[$content_key] = array(
'#markup' => theme($product_extra_field['theme'], $variables),
'#attached' => array(
'css' => array(drupal_get_path('module', 'commerce_product') . '/theme/commerce_product.theme.css'),
),
);
Something along these lines would be better:
$entity->content[$content_key] = array(
'#theme' => $product_extra_field['theme'],
'#title' => $product_extra_field['label'],
'#sku' => $variables['sku'],
'#attached' => array(
'css' => array(drupal_get_path('module', 'commerce_product') . '/theme/commerce_product.theme.css'),
),
);
Of course the above is hardcoded to the SKU field and needs work to be able to apply to the Title and Status fields as well.
A follow-up question is that I noticed that for these fields, no regular field formatter is used. Is there a specific reason not to? I'd like to be able to choose to hide the Field Label for example, like with other fields.
Comments
Comment #1
rszrama commentedSounds like a reasonable change to me.
Re: the label, though, the issue is that these are "extra fields" not "Field API fields." It's a bit confusing, but with extra fields there simply is no option to make those sorts of customizations through the Field UI.
Comment #2
a.ross commentedYeah, I was afraid of that. It would at least make sense to translate the current stuff to what would otherwise be a sensible default for displaying that field. As in, let's say the SKU field was a regular field, you'd have the label display inline and the value plaintext or something. With just the difference that you can't change it in the field UI (-:
Comment #3
a.ross commentedOk, I've been trying to work out a solution, but it's a bit more tricky than I hoped. I can get the fields to work with the regular field template but that would break compatibility. So all I've basically done with this patch is to delay rendering of the extra fields until the entire page is rendered. It's not ideal, albeit probably the best we can do.
Comment #4
a.ross commentedThink this can go in? I'd be delighted
Comment #5
rszrama commentedAhh, I assumed from your description it was an incomplete solution. Reading it again I see that you said you didn't pursue an option because it was a breaking change. Makes sense, as these aren't fields anyways. (Fields and "Extra Fields" are confusingly entirely different concepts in Drupal core. : ) The patch inadvertently removes the : from the first label, but I can fix it in testing and try to get it in in a bit. Tearing through the Address Field queue at the moment. ; )
Comment #6
a.ross commentedAlright, happy to hear that. Good catch, too.
And yeah I noticed they're entirely different :/. I just hoped a utopia was around the corner, where even field_extra_fields used the regular field theme. Guess not (-:
Comment #7
a.ross commentedWheh, Address Field's queue must be pretty long! ;-)
Comment #8
rszrama commentedI've only closed out about 100 issues and 50 bugs so far...
Comment #9
a.ross commentedOh wow, I underestimated that by a tiny margin then o_0
Comment #10
rszrama commentedhehe Well, it hadn't had a new release since May 2012. A lot had built up over time, but there were a lot of duplicate / mis-categorized issues as well. Still, it was a productive run... can't wait to close it out to a 1.0. : )
Comment #11
a.ross commentedBump
Comment #12
philsward commentedIs this issue directly related to the fact that you can't change the label's for the hard-coded stuff like title and SKU? In trying to setup a store, I want to show the title field but get rid of the word "title". Just wondering if I should follow this issue or kick up another one...
At this point, it looks like a CSS hack might be the only solution :-/
Comment #13
a.ross commentedYou should be able to change the output right now by either overriding the theme function or perhaps implementing a pre-render function for it. It's just that with this patch it won't render HTML code until the entire page is rendered, allowing any alter hook down the pipeline to use the Render API code.
Comment #14
roball commentedphilsward:
I had the same problem. A Product Display content type always shows SKU and Title of the referenced product with exactly these wordings for the label, and the label is always set to be displayed Above. Inline and Hidden are not possible. The only built-in alternative is to not display these fields at all, by selecting the Formatter Hidden (instead of Visible). This is what I did. I created two custom fields of type Computed instead. This gives you complete freedom on what is displayed how.
Comment #14.0
roball commentedMinor edit
Comment #15
zmove commentedThis issue is between the feature request and the bug report, but it sounds major to me as it will create bug with a lot of other modules that deal with fields.
Here is a little example : #1987494: Make field_group_table themable
That's just one example, each time I make a project using drupal commerce, I encounter some problems because of that.
Comment #16
joelpittetJust need to add that colon back. Otherwise nice work and I'd RTBC this patch:)
Comment #17
discipolo commentedadded back that colon
Comment #18
joelpittetThank you @discipolo. RTBC!
Comment #19
joelpittetSorry spotted something on second glance.
Missing # in front of first label.
Comment #20
a.ross commentedThis should do it
Comment #21
a.ross commentedComment #22
joelpittetThanks @a.ross, that should do the trick.
Comment #23
rszrama commentedUpdating the patch to remove variables from inside strings then committing.
Comment #24
rszrama commentedAnd committed! Thanks!