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 have a case where users (sellers) on my site can post files for other users (buyers) to purchase. The problem is that if a seller edits or removes a file that has previously been purchased, the file also disappears from the buyer's list of licensed files. One solution that I've thought of is to prevent sellers from editing or removing a file if it's already been purchased. Is there a way to achieve this?
Comments
Comment #1
bojanz CreditAttribution: bojanz commentedYes, implement hook_form_alter (or hook_inline_entity_form_entity_form_alter if you are using Inline Entity Form for products).
In the hook, do an EntityFieldQuery on commerce_licenses by the found product, and if any are found, set #access => FALSE or #disabled => TRUE on the commerce_file element.
So kinda like this:
Share your final code so we can put it in a documentation page :)
Comment #2
b3nji CreditAttribution: b3nji commentedThank you for the code. Pardon my ignorance but how do you get the $product_id? I'm using Inline Entity form for products.
Comment #3
b3nji CreditAttribution: b3nji commentedOk got it. The following code works for me. Thanks bojanz!
Comment #5
b3nji CreditAttribution: b3nji commentedUpon further testing, the code works for Inline Entity form with single values. Do you have any suggestions for how to make it work with inline entity form with multiple values?
Comment #6
bojanz CreditAttribution: bojanz commentedThe hook is invoked the same way for both widgets (single value and multiple value). So the code should be fine.
Which part is failing?
Comment #7
b3nji CreditAttribution: b3nji commentedLicenses are active for a given product however the user is still able to remove the file (commerce_file field is not being disabled).
Comment #8
bojanz CreditAttribution: bojanz commentedYes, I understood that part, but does the hook run?
Does "if ($entity_form['#entity_type'] == 'commerce_product') {" execute?
Does it try to alter $entity_form?
Some step along the way fails.
Comment #9
b3nji CreditAttribution: b3nji commentedAs far as I can tell yes. Could you kindly help me debug? I can send you site details via pm.
Comment #10
b3nji CreditAttribution: b3nji commentedAaah sorry, I had the module disabled... *sigh*
Comment #11
b3nji CreditAttribution: b3nji commented