RemoveButton.php in Commerce Wishlist 8.3
File
src/Plugin/views/field/RemoveButton.php
View source
<?php
namespace Drupal\commerce_wishlist\Plugin\views\field;
use Drupal\commerce_wishlist\WishlistManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RemoveButton extends FieldPluginBase {
use UncacheableFieldHandlerTrait;
protected $wishlistManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, WishlistManagerInterface $wishlist_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->wishlistManager = $wishlist_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('commerce_wishlist.wishlist_manager'));
}
public function clickSortable() {
return FALSE;
}
public function getValue(ResultRow $row, $field = NULL) {
return '<!--form-item-' . $this->options['id'] . '--' . $row->index . '-->';
}
public function viewsForm(array &$form, FormStateInterface $form_state) {
$form['#cache']['max-age'] = 0;
if (empty($this->view->result)) {
unset($form['actions']);
return;
}
$form[$this->options['id']]['#tree'] = TRUE;
foreach ($this->view->result as $row_index => $row) {
$form[$this->options['id']][$row_index] = [
'#type' => 'submit',
'#value' => t('Remove'),
'#name' => 'delete-wishlist-item-' . $row_index,
'#remove_wishlist_item' => TRUE,
'#row_index' => $row_index,
'#attributes' => [
'class' => [
'delete-wishlist-item',
],
],
];
}
}
public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if (!empty($triggering_element['#remove_wishlist_item'])) {
$row_index = $triggering_element['#row_index'];
$wishlist_item = $this
->getEntity($this->view->result[$row_index]);
$this->wishlistManager
->removeWishlistItem($wishlist_item
->getWishlist(), $wishlist_item);
}
}
public function query() {
}
}
Classes
Name |
Description |
RemoveButton |
Defines a form element for removing the wishlist item. |