View source
<?php
namespace Drupal\commerce_pricelist\Form;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\inline_entity_form\Form\EntityInlineForm;
class PriceListItemInlineForm extends EntityInlineForm {
protected $variationTypes;
protected $routeMatch;
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityTypeInterface $entity_type, RouteMatchInterface $route_match) {
parent::__construct($entity_field_manager, $entity_type_manager, $module_handler, $entity_type);
$this->routeMatch = $route_match;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_field.manager'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $entity_type, $container
->get('current_route_match'));
}
public function getEntityTypeLabels() {
$labels = [
'singular' => t('price list item'),
'plural' => t('price list items'),
];
return $labels;
}
public function getTableFields($bundles) {
$fields = parent::getTableFields($bundles);
$fields['price'] = [
'type' => 'field',
'label' => t('Price'),
'weight' => 3,
];
$fields['quantity'] = [
'type' => 'field',
'label' => t('Quantity'),
'weight' => 4,
];
return $fields;
}
public function entityForm(array $entity_form, FormStateInterface $form_state) {
$entity_form = parent::entityForm($entity_form, $form_state);
if (isset($entity_form['purchased_entity']['widget'][0])) {
$entity_form['purchased_entity']['widget'][0]['target_id']['#ajax'] = [
'callback' => [
get_class($this),
'purchasedRefresh',
],
'event' => 'autocompleteclose',
'wrapper' => $entity_form['#ief_row_delta'] . 'purchased_entity_refresh',
];
}
else {
$entity_form['purchased_entity']['widget']['#ajax'] = [
'callback' => [
get_class($this),
'purchasedRefresh',
],
'wrapper' => $entity_form['#ief_row_delta'] . 'purchased_entity_refresh',
];
}
$entity_form = $this
->priceForm($entity_form, $form_state);
$entity_form['price']['#attributes']['id'] = $entity_form['#ief_row_delta'] . 'purchased_entity_refresh';
return $entity_form;
}
public function priceForm(array $entity_form, FormStateInterface $form_state) {
$entity = $entity_form['#entity'];
$entity_form['price']['widget']['#disabled'] = FALSE;
if ($entity
->hasPurchasedEntity()) {
$purchasedEntity = $entity
->getPurchasedEntity();
}
$userInput = $form_state
->getUserInput();
if ($entity_form['#op'] == 'add') {
if (isset($userInput['field_price_list_item']['form']['inline_entity_form']['purchased_entity'])) {
$purchasedEntityId = $userInput['field_price_list_item']['form']['inline_entity_form']['purchased_entity'];
}
if (isset($purchasedEntityId[0]['target_id'])) {
$purchasedEntityId = EntityAutocomplete::extractEntityIdFromAutocompleteInput($purchasedEntityId[0]['target_id']);
}
}
else {
if (isset($userInput['field_price_list_item']['form']['inline_entity_form']['entities'])) {
$entities = $userInput['field_price_list_item']['form']['inline_entity_form']['entities'];
}
if (isset($entities[$entity_form['#ief_row_delta']]['form']['purchased_entity'])) {
$purchasedEntityId = $entities[$entity_form['#ief_row_delta']]['form']['purchased_entity'];
if (isset($purchasedEntityId[0]['target_id'])) {
$purchasedEntityId = EntityAutocomplete::extractEntityIdFromAutocompleteInput($purchasedEntityId[0]['target_id']);
}
}
}
if (isset($purchasedEntityId)) {
$purchasedEntityTargetType = $entity
->getFieldDefinition('purchased_entity')
->getSetting('target_type');
$purchasedEntity = $this->entityTypeManager
->getStorage($purchasedEntityTargetType)
->load($purchasedEntityId);
}
if (!empty($purchasedEntity)) {
$price = $purchasedEntity
->getPrice();
if (!$price) {
$entity_form['price']['widget']['#disabled'] = TRUE;
}
}
return $entity_form;
}
public static function purchasedRefresh(array $form, FormStateInterface $form_state) {
$element = [];
$triggering_element = $form_state
->getTriggeringElement();
$array_parents = array_slice($triggering_element['#array_parents'], 0, -2);
while (!(isset($element['#type']) && $element['#type'] == 'inline_entity_form')) {
$element = NestedArray::getValue($form, $array_parents);
array_pop($array_parents);
}
$elementPrice = $element['price'];
$targetType = $element['#entity']
->getFieldDefinition('purchased_entity')
->getSetting('target_type');
$fieldPriceListItem = $form_state
->getValue('field_price_list_item');
if ($element['#op'] == 'add') {
if (isset($fieldPriceListItem['form']['inline_entity_form']['purchased_entity'][0]['target_id'])) {
$purchasedEntityId = $fieldPriceListItem['form']['inline_entity_form']['purchased_entity'][0]['target_id'];
}
}
else {
$entities = $fieldPriceListItem['form']['inline_entity_form']['entities'];
if (isset($entities[$element['#ief_row_delta']]['form']['purchased_entity'][0]['target_id'])) {
$purchasedEntityId = $entities[$element['#ief_row_delta']]['form']['purchased_entity'][0]['target_id'];
}
}
if (isset($purchasedEntityId)) {
$purchasedEntity = \Drupal::entityTypeManager()
->getStorage($targetType)
->load($purchasedEntityId);
$price = $purchasedEntity
->getPrice();
if (!is_null($price)) {
$elementPrice['widget'][0]['number']['#value'] = sprintf("%.2f", $price
->getNumber());
}
else {
$elementPrice['widget'][0]['number']['#value'] = NULL;
$elementPrice['widget'][0]['number']['#placeholder'] = NULL;
}
}
return $elementPrice;
}
public function save(EntityInterface $entity) {
$product = $entity
->getPurchasedEntity();
$priceList = $entity
->getPriceList();
if (!$entity
->getQuantity()) {
$entity
->setQuantity(1);
}
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();
}
}
}
public function getEntityLabel(EntityInterface $entity) {
return is_null($entity
->label()) ? empty($entity
->getPurchasedEntity()) ? NULL : $entity
->getPurchasedEntity()
->label() : $entity
->label();
}
}