WishlistItemForm.php in Commerce Wishlist 8.3
File
src/Form/WishlistItemForm.php
View source
<?php
namespace Drupal\commerce_wishlist\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\entity\Form\EntityDuplicateFormTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WishlistItemForm extends ContentEntityForm {
use EntityDuplicateFormTrait;
protected $dateFormatter;
protected $entityFieldManager;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, DateFormatterInterface $date_formatter, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->dateFormatter = $date_formatter;
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('date.formatter'), $container
->get('entity_field.manager'));
}
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
if ($route_match
->getRawParameter('commerce_wishlist_item') !== NULL) {
$entity = $route_match
->getParameter('commerce_wishlist_item');
}
else {
$wishlist = $route_match
->getParameter('commerce_wishlist');
$values = [
'wishlist_id' => $wishlist
->id(),
'type' => 'commerce_product_variation',
];
$entity = $this->entityTypeManager
->getStorage('commerce_wishlist_item')
->create($values);
}
return $entity;
}
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
$this
->messenger()
->addStatus($this
->t('The item %label has been successfully saved.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirect('entity.commerce_wishlist_item.collection', [
'commerce_wishlist' => $this->entity
->getWishlistId(),
]);
}
}