I've been trying to add a custom form field to the checkout step of the delivery method, where the customer can pick a date for delivery.
I've added the delivery_date value to Shipment, it shows up in dump logs and mysql.
I wrote a ShipmentTypeExtension file like this
<?php
namespace AppBundle\Form\Extension;
use Sylius\Bundle\ShippingBundle\Form\Type\ShipmentType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Entity\Shipment;
final class ShipmentTypeExtension extends AbstractTypeExtension
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('delivery_date', DateType::class, [
'required' => true,
'label' => 'delivery_date',
]);
}
/**
* {@inheritdoc}
*/
public function getExtendedType(): string
{
return ShipmentType::class;
}
}
I added a services.yml in AppBundle/Resources/config
services:
app.form.extension.type.shipment:
class: AppBundle\Form\Extension\ShipmentTypeExtension
tags:
- { name: form.type_extension, extended_type: Sylius\Bundle\ShippingBundle\Form\Type\ShipmentType }
And add this part to my config.yml
sylius_shipping:
resources:
shipment:
classes:
model: AppBundle\Entity\Shipment
I overwrote _shipment.html.twig and added this line after the for loop where it outputs all delivery methods
{% include '@SyliusShop/Checkout/SelectShipping/_deliveryTimePicker.html.twig' %}
So finally, I can mess around in my new _deliveryTimePicker.html.twig to try and add a datepicker field.
But it doesnt work, how do I render the form properly?
Here is my twig dump():
array:17 [▼
"configuration" => RequestConfiguration {#25119 ▶}
"metadata" => Metadata {#4881 ▶}
"resource" => Order {#15648 ▶}
"order" => Order {#15648 ▶}
"form" => FormView {#37012 ▼
+vars: array:24 [▼
"value" => Shipment {#35552 ▶}
"attr" => []
"form" => FormView {#37012}
"id" => "sylius_checkout_select_shipping_shipments_0"
"name" => "0"
"full_name" => "sylius_checkout_select_shipping[shipments][0]"
"disabled" => false
"label" => null
"label_format" => null
"multipart" => false
"block_prefixes" => array:3 [▶]
"unique_block_prefix" => "_sylius_checkout_select_shipping_shipments_entry"
"translation_domain" => null
"cache_key" => "_sylius_checkout_select_shipping_shipments_entry_sylius_checkout_shipment"
"errors" => FormErrorIterator {#37013 ▶}
"valid" => true
"data" => Shipment {#35552 ▶}
"required" => true
"size" => null
"label_attr" => []
"compound" => true
"method" => "POST"
"action" => ""
"submitted" => false
]
+parent: FormView {#37008 ▶}
+children: array:1 [▶]
-rendered: false
-methodRendered: false
}
"wrap_fields_with_addons" => true
"app" => AppVariable {#23784 ▶}
"sylius" => ShopperContext_d398ef6 {#4770 ▶}
"sylius_base_locale" => "de"
"sylius_meta" => array:1 [▶]
"sonata_block" => GlobalVariables {#24160 ▶}
"shipment" => Shipment {#35552 ▼
#delivery_date: null
#order: Order {#15648}
#id: 4
#state: "cart"
#method: ShippingMethod {#35915 ▶}
#units: PersistentCollection {#35916 ▶}
#tracking: null
#createdAt: DateTime @1525251156 {#35547 ▶}
#updatedAt: DateTime @1525251157 {#35548 ▶}
}
"_parent" => array:11 [▶]
"_seq" => PersistentCollection {#17988 ▶}
"_iterated" => false
"loop" => array:8 [▶]
"_key" => 0
]
This is the error Im getting:
Neither the property "delivery_date" nor one of the methods "delivery_date()", "getdelivery_date()"/"isdelivery_date()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView"