You are here

public function WishlistViewForm::buildForm in UC Wish List 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/WishlistViewForm.php, line 50

Class

WishlistViewForm

Namespace

Drupal\uc_wishlist\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $items = NULL, $wid = NULL, $own = NULL) {
  $form['items'] = [
    '#tree' => TRUE,
  ];
  $form['#attached']['library'][] = 'uc_wishlist/default';
  $sliderNumber = 1;

  // Load each wish list product and add it to the form array.
  $itemNum = 0;
  foreach ($items as $item) {
    $node = Node::load($item->nid);
    $element = [
      '#prefix' => '<div class="uc_wishlist_product_item">',
      '#suffix' => '</div>',
    ];
    $element['nid'] = [
      '#type' => 'hidden',
      '#value' => $node
        ->id(),
    ];
    $element['wpid'] = [
      '#type' => 'hidden',
      '#value' => $item->wpid,
    ];
    $element['module'] = [
      '#type' => 'value',
      '#value' => 'uc_product',
    ];
    if ($own) {
      $element['remove'] = [
        '#type' => 'checkbox',
        '#title' => 'Remove',
      ];
    }
    $item->haveqty = 0;
    if (is_array($item->purchase)) {
      $item->haveqty = count($item->purchase);
    }
    $element['title'] = [
      '#type' => 'item',
      '#markup' => Link::fromTextAndUrl($item->title, Url::fromRoute('entity.node.canonical', [
        'node' => $node
          ->id(),
      ]))
        ->toString(),
    ];
    $imagesFound = FALSE;
    $imageUrls = [];
    $numberOfImages = 0;
    while (!$imagesFound) {
      if ($node
        ->get('uc_product_image')[$numberOfImages] != NULL) {
        $imageUrls[] = $node
          ->get('uc_product_image')[$numberOfImages]->entity
          ->url();
        $numberOfImages = $numberOfImages + 1;
      }
      else {
        $imagesFound = TRUE;
        break;
      }
    }
    $element['images'] = [
      '#tree' => TRUE,
      '#prefix' => '<div id="wishlist_image_container"> <div class="wishlist_slider" id="wishlist_slider_' . $sliderNumber . '" style="width:150px;height:150px;" class="uc_wishlist_product_images">',
      '#suffix' => '</div></div>',
    ];
    $sliderNumber++;
    foreach ($imageUrls as $key => $value) {
      $element['images']['image_' . $key] = [
        '#type' => 'item',
        '#markup' => '<img alt="test" class="uc_wishlistProductImage" src="' . $value . '" width="150" height="150" />',
        '#theme_wrappers' => [],
      ];
    }
    $description = $node
      ->get('body')
      ->getValue();

    // Now allow alterations via hook_uc_product_description_alter().
    if ($description) {
      $element['description'] = [
        '#type' => 'item',
        '#markup' => mb_strimwidth($description[0]['value'], 0, 50, '...' . Link::fromTextAndUrl('More', Url::fromRoute('entity.node.canonical', [
          'node' => $node
            ->id(),
        ]))
          ->toString()),
      ];
    }
    $element['node'] = [
      '#type' => 'value',
      '#value' => $form_state
        ->get('variant') ?: $node,
    ];
    $element['data'] = [
      '#type' => 'hidden',
      '#value' => serialize($item->data),
    ];
    if ($own) {
      $element['wanted_qty'] = [
        '#type' => 'uc_quantity',
        '#title' => 'Wanted Quantity',
        '#default_value' => $item->qty,
      ];
    }
    else {
      $element['wanted_qty'] = [
        '#type' => 'item',
        '#title' => 'Wanted Quantity',
        '#markup' => '<p class="wanted_quantity">' . $item->qty . '</p>',
      ];
    }
    $price = $node
      ->get('price')
      ->getValue()[0]['value'];
    $element['total_price'] = [
      '#type' => 'item',
      '#title' => 'Price for wanted quantity',
      '#markup' => '<p class="total_price"$>' . floatval($price) * $item->qty . '</p>',
    ];
    $element['qty'] = [
      '#type' => 'uc_quantity',
      '#title' => 'Your Quantity',
      '#default_value' => '1',
    ];
    $element['price'] = [
      '#type' => 'item',
      '#title' => 'Your Price',
      '#markup' => '<p class="price">$' . floatval($price) . '</p>',
    ];

    // Checking if uc_stock module is install in the site and
    // prevent user to add product into cart if the stock value of the product
    // is equal to 0.
    // Checking if uc_stock module install in the site.
    if (\Drupal::moduleHandler()
      ->moduleExists('uc_stock')) {

      // If product kit module is installed in the site and wishlist node type
      // is product kit.
      if (\Drupal::moduleHandler()
        ->moduleExists('uc_product_kit') && $node
        ->get('type')
        ->getValue() == 'product_kit') {

        // Getting the number of products attached with the Product Kit.
        // As there is no stock configuration, so we will check the stock value
        // of the each product of Product Kit.
        // If all products of the Product Kit has stock active, then we allow
        // user to purchase product kit.
        // @var unknown_type .
        $products = $node
          ->get('products')
          ->getValue();
        $stock = TRUE;

        // Looping through each product.
        foreach ($products as $product) {

          // Checking stock level of the product.
          if (!uc_stock_level($product->model)) {
            $stock = FALSE;
          }
        }
      }
      else {

        // Getting stock value of the particular product SKU.
        // It will return FALSE, if stock level is not active to product SKU.
        $sku = $node
          ->get('model')
          ->getValue();
        $stock = uc_stock_level($sku[0]['value']);
      }
      if ($stock) {
        $element['addcart'] = [
          '#type' => 'submit',
          '#name' => 'addcart-' . $itemNum,
          '#value' => 'Add to cart',
          '#submit' => [
            'addToCart',
          ],
        ];
      }
      else {
        $element['addcart'] = [
          '#type' => 'item',
          '#name' => 'addcart-' . $itemNum,
          '#markup' => 'Out Of Stock',
        ];
      }
    }
    else {
      $element['addcart'] = [
        '#type' => 'submit',
        '#name' => 'addcart-' . $itemNum,
        '#value' => 'Add to cart',
        '#submit' => [
          'addToCart',
        ],
      ];
    }
    $itemNum++;
    $form['items'][] = $element;
  }
  $form['wid'] = [
    '#type' => 'hidden',
    '#value' => $wid,
  ];
  if ($own) {
    $form['own'] = [
      '#type' => 'value',
      '#value' => TRUE,
    ];
    $form['update'] = [
      '#type' => 'submit',
      '#attributes' => [
        'class' => [
          'uc_wishlist_update_wishlist',
        ],
      ],
      '#name' => 'uc_wishlist_update_wishlist',
      '#value' => 'Update wish list',
    ];
  }
  return $form;
}