- πΊπΈUnited States rhovland Oregon
Here is a patch that adds a default value to the title field based on how many existing shipments there are in the order.
Why is this a form override?
I made a function to generate the title/** * Returns a default title for the shipment based on the number of shipments currently in the order. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The shipment. * * @return string * The shipment title. */ public static function generateShipmentTitle(OrderInterface $order) { $existing_shipments = count($order->get('shipments')->referencedEntities()); return 'Shipment #' . ($existing_shipments + 1); }
I tried setting the default value in the base field definition using a callback function
->setDefaultValueCallback('Drupal\commerce_shipping\Entity\Shipment::generateShipmentTitle')
But there is no state at this point so it can't count shipments on the order.
Why not in ShipmentForm.php ?
I cannot set just the default value for the title here, I had to define the entire title form field. This broke the form completely and I couldn't save the shipment or use shipping calculation.$form['title'] = [ '#type' => 'textfield', '#title' => $this->t('Title'), '#default_value' => ShipmentForm::generateShipmentTitle($order), '#maxlength' => 255, ];
- πΊπΈUnited States ericchew
What if you have multiple shipment types? Should probably use
hook_BASE_FORM_ID_alter
instead. - πΊπΈUnited States rhovland Oregon
@ericchew Where is it in ShipmentForm? I don't see it?
I attempted to change just the default value but the whole title field went poof instead
Eg:$form['title'] = [ '#default_value' => ShipmentForm::generateShipmentTitle($order), ];
- πΊπΈUnited States ericchew
It's an entity form so its being built by the form display widgets when it calls
$form = parent::form($form, $form_state);
. Your code in #4 would break it because you're overriding the entire form element array for the title field.Try
$form['title']['widget'][0]['value']['#default_value'] = ShipmentForm::generateShipmentTitle($order);
instead which just overrides the default value key (just like you did in your form alter). This line would have to go AFTER the form is being built (see the link I put in #3 for where it is being built. You'll want to make it translatable too. Something like this:
$form['title']['widget'][0]['value']['#default_value'] = t('Shipment #@number', [ '@number' => ($existing_shipments + 1), ]);
- πΊπΈUnited States rhovland Oregon
Here's a patch taking into account feedback. Thanks for your help!
- πΊπΈUnited States rhovland Oregon
So it seems this latest patch is overwriting the title value when you go to edit a shipment. I'm at a loss for how... it's setting the #default_value not #value
The older patch works fine
- Status changed to Needs review
almost 2 years ago 12:40pm 24 March 2023 - Status changed to Fixed
12 months ago 4:33am 30 December 2023 - πΊπΈUnited States rszrama
Thanks. I don't even really like showing this title field, but defaulting it like this is a good starting point!
-
rszrama β
committed 9c999823 on 8.x-2.x authored by
tonytheferg β
Issue #3264338 by tonytheferg, rhovland, ericchew: Populate the title...
-
rszrama β
committed 9c999823 on 8.x-2.x authored by
tonytheferg β
Automatically closed - issue fixed for 2 weeks with no activity.