BundleItemOrderItemInlineForm.php in Commerce Product Bundle 8
File
src/Form/BundleItemOrderItemInlineForm.php
View source
<?php
namespace Drupal\commerce_product_bundle\Form;
use Drupal\commerce_product_bundle\Entity\BundleItemOrderItemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\inline_entity_form\Form\EntityInlineForm;
class BundleItemOrderItemInlineForm extends EntityInlineForm {
public function getEntityTypeLabels() {
$labels = [
'singular' => t('product bundle item order item'),
'plural' => t('product bundle item order items'),
];
return $labels;
}
public function getTableFields($bundles) {
$fields = parent::getTableFields($bundles);
$fields['purchased_entity'] = [
'type' => 'field',
'label' => t('Purchased Entity'),
'weight' => 2,
];
$fields['unit_price'] = [
'type' => 'field',
'label' => t('Unit price'),
'weight' => 2,
];
$fields['quantity'] = [
'type' => 'field',
'label' => t('Quantity'),
'weight' => 3,
];
return $fields;
}
public function entityForm(array $entity_form, FormStateInterface $form_state) {
$entity_form = parent::entityForm($entity_form, $form_state);
$entity_form['#entity_builders'][] = [
get_class($this),
'populateTitle',
];
return $entity_form;
}
public static function populateTitle($entity_type, BundleItemOrderItemInterface $order_item, array $form, FormStateInterface $form_state) {
$bundle_item = $order_item
->getBundleItem();
$bundle_item
->setCurrentVariation($order_item
->getPurchasedEntity());
if ($order_item
->isNew()) {
$order_item
->setTitle($bundle_item
->getTitle());
}
}
}