WishlistItemDetailsForm.php in Commerce Wishlist 8.3
File
src/Form/WishlistItemDetailsForm.php
View source
<?php
namespace Drupal\commerce_wishlist\Form;
use Drupal\commerce_wishlist\Entity\WishlistItemInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class WishlistItemDetailsForm extends EntityForm {
use AjaxFormHelperTrait;
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$wishlist_item = $this->entity;
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#id'] = Html::getId($form_state
->getBuildInfo()['form_id']);
$form['comment'] = [
'#type' => 'textarea',
'#title' => $this
->t('Comment'),
'#rows' => 5,
'#default_value' => $wishlist_item
->getComment(),
];
$form['quantity'] = [
'#type' => 'commerce_number',
'#title' => $this
->t('Quantity'),
'#required' => TRUE,
'#default_value' => $wishlist_item
->getQuantity(),
];
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Update details'),
'#submit' => [
'::submitForm',
],
];
if ($this
->isAjax()) {
$actions['submit']['#ajax']['callback'] = '::ajaxSubmit';
}
return $actions;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$wishlist_item = $this->entity;
$wishlist_item
->save();
$wishlist = $wishlist_item
->getWishlist();
$form_state
->setRedirectUrl($wishlist
->toUrl('user-form'));
}
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
$wishlist = $this->entity
->getWishlist();
Cache::invalidateTags($wishlist
->getCacheTags());
$response = new AjaxResponse();
$response
->addCommand(new PrependCommand('.commerce-wishlist-form', [
'#type' => 'status_messages',
]));
$response
->addCommand(new ReplaceCommand('#wishlist-item-details-' . $this->entity
->id(), [
'#theme' => 'commerce_wishlist_item_details',
'#wishlist_item_entity' => $this->entity,
]));
$response
->addCommand(new CloseDialogCommand());
return $response;
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
assert($entity instanceof WishlistItemInterface);
$values = $form_state
->getValues();
unset($values['action']);
foreach ($values as $key => $value) {
if ($entity
->hasField($key)) {
$entity
->set($key, $value);
}
}
}
}