public function WishlistUserForm::form in Commerce Wishlist 8.3
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ WishlistUserForm.php, line 169
Class
- WishlistUserForm
- Provides the wishlist user form.
Namespace
Drupal\commerce_wishlist\FormCode
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_wishlist\Entity\WishlistInterface $wishlist */
$wishlist = $this->entity;
$owner_access = $this
->ownerAccess($wishlist);
$wishlist_has_items = $wishlist
->hasItems();
$form['#tree'] = TRUE;
$form['#process'][] = '::processForm';
$form['#theme'] = 'commerce_wishlist_user_form';
$form['#attached']['library'][] = 'commerce_wishlist/user';
// Workaround for core bug #2897377.
$form['#id'] = Html::getId($form_state
->getBuildInfo()['form_id']);
$form['header'] = [
'#type' => 'container',
];
$form['header']['empty_text'] = [
'#markup' => $this
->t('Your wishlist is empty.'),
'#access' => !$wishlist_has_items,
];
$form['header']['add_all_to_cart'] = [
'#type' => 'submit',
'#value' => t('Add the entire list to cart'),
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefreshForm',
],
],
'#access' => $wishlist_has_items,
];
$form['header']['share'] = [
'#type' => 'link',
'#title' => $this
->t('Share the list by email'),
'#url' => $wishlist
->toUrl('share-form'),
'#attributes' => [
'class' => [
'use-ajax',
'button',
'btn',
'btn-default',
'wishlist-button',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
'title' => $this
->t('Share the list by email'),
]),
'role' => 'button',
],
'#access' => $owner_access && $wishlist_has_items,
];
$form['items'] = [];
foreach ($wishlist
->getItems() as $item) {
$purchasable_entity = $item
->getPurchasableEntity();
if (!$purchasable_entity) {
continue;
}
$item_form =& $form['items'][$item
->id()];
$item_form = [
'#type' => 'container',
];
$item_form['entity'] = $this
->renderPurchasableEntity($purchasable_entity);
$item_form['details'] = [
'#theme' => 'commerce_wishlist_item_details',
'#wishlist_item_entity' => $item,
];
$item_form['details_edit'] = [
'#type' => 'link',
'#title' => $this
->t('Edit details'),
'#url' => $item
->toUrl('details-form'),
'#attributes' => [
'class' => [
'use-ajax',
'wishlist-item__details-edit-link',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
'title' => $this
->t('Edit details'),
]),
],
'#access' => $owner_access,
];
$item_form['actions'] = [
'#type' => 'container',
];
$item_form['actions']['add_to_cart'] = [
'#type' => 'submit',
'#value' => t('Add to cart'),
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefreshForm',
],
],
'#submit' => [
'::addToCartSubmit',
],
'#name' => 'add-to-cart-' . $item
->id(),
'#item_id' => $item
->id(),
];
$item_form['actions']['remove'] = [
'#type' => 'submit',
'#value' => t('Remove'),
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefreshForm',
],
],
'#submit' => [
'::removeItem',
],
'#name' => 'remove-' . $item
->id(),
'#access' => $owner_access,
'#item_id' => $item
->id(),
];
}
return $form;
}