public function PriceListItemInlineForm::save in Commerce Pricelist 8
Saves the given entity.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityInlineForm::save
File
- src/
Form/ PriceListItemInlineForm.php, line 246
Class
- PriceListItemInlineForm
- Defines the inline form for product variations.
Namespace
Drupal\commerce_pricelist\FormCode
public function save(EntityInterface $entity) {
$product = $entity
->getPurchasedEntity();
$priceList = $entity
->getPriceList();
// Set quantity if quantity is null.
if (!$entity
->getQuantity()) {
$entity
->setQuantity(1);
}
// Set price if price is null.
if ($product && !$entity
->getPrice()) {
if ($product
->getPrice()) {
$entity
->setPrice($product
->getPrice());
}
}
$entity
->save();
$entity_id = $entity
->id();
if ($product && $product->field_price_list_item) {
$target_id = [];
$field_price_list_item = $product->field_price_list_item
->getValue();
foreach ($field_price_list_item as $item) {
$target_id[] = $item['target_id'];
}
if (!in_array($entity_id, $target_id)) {
$product->field_price_list_item[] = [
'target_id' => $entity_id,
];
$product
->save();
}
}
if ($priceList && $priceList->field_price_list_item) {
$target_id = [];
$field_price_list_item = $priceList->field_price_list_item
->getValue();
foreach ($field_price_list_item as $item) {
$target_id[] = $item['target_id'];
}
if (!in_array($entity_id, $target_id)) {
$priceList->field_price_list_item[] = [
'target_id' => $entity_id,
];
$priceList
->save();
}
}
}