Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
A helper function in Commerce to determine whether a given node is a product display node (ie, has a product reference field) would be really useful, as otherwise lots of contrib modules will be reinventing the same wheel when providing node tabs etc.
Comment | File | Size | Author |
---|---|---|---|
#6 | 1588226-product-reference-node-types.patch | 1.12 KB | bojanz |
#3 | 1588226-product-reference-node-types.patch | 1.19 KB | bojanz |
#1 | commerce_product_reference_node_types.diff | 1.35 KB | theo_ |
Comments
Comment #1
theo_ CreditAttribution: theo_ commentedHere is a function which list all node types using a product reference field (what we actually call a product display).
We provide a way to alter this list, for example if you want to hide some elements from it. Maybe we should document it ?
Comment #2
Damien Tournoud CreditAttribution: Damien Tournoud commentedLet's make the following modifications:
^ You should rather use
field_info_fields()
(which is cached and gives you the bundles). Just iterate over the result looking for$field['type'] == 'commerce_product_reference'
.^ Code style: space after the "foreach".
^ Same code style issue.
Just iterate directly over
$field['bundles']['node']
instead of searching it.^ For consistency, the hook should be called
hook_commerce_product_reference_node_types_alter()
+ the new hook needs to be documented.Comment #3
bojanz CreditAttribution: bojanz commentedRerolled. Still needs documentation for the alter hook.
Comment #4
joachim CreditAttribution: joachim commentedLooks great, but can we also have an is_product_display_node() that makes use of it please?
Comment #5
joachim CreditAttribution: joachim commentedAlso, I don't really understand the point of the alter hook. Who might want to alter that list and why?
Comment #6
bojanz CreditAttribution: bojanz commentedRemoved the alter hook.
I don't see a point in is_product_display_node(). Just do an in_array($node->type, array_keys($whatever_this_function_returns));
Comment #7
rszrama CreditAttribution: rszrama commentedI'm committing the patch in #6 as is. I agree, I'm not sure why we'd need an alter hook here, since we're simply reading the field info for node types with product reference fields. If someone needs alter logic, I'd presume they would just do that after calling this function.
My first hunch was that we make this more abstract with a parameter to specify the entity type, but bojanz talked me out of it. This gets the job done, but having made this specific to nodes now, I'm also not sure I'd keep out a function that could determine if a node had a product reference field or not. I'm not adding it in at this point b/c it's not a pattern we're following elsewhere in this module (e.g. look for our usage of commerce_product_line_item_types() throughout core). I can be convinced otherwise. ; )