You are here

function uc_product_view_product in Ubercart 8.4

Renders product related content for product-type modules.

2 calls to uc_product_view_product()
uc_product_kit_node_view in uc_product_kit/uc_product_kit.module
Implements hook_node_view().
uc_product_node_view in uc_product/uc_product.module
Implements hook_node_view().

File

uc_product/uc_product.module, line 272
The product module for Ubercart.

Code

function uc_product_view_product(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {

  // Give modules a chance to alter this product. If it is a variant, this will
  // have been done already by uc_product_load_variant(), so we check a flag to
  // be sure not to alter twice -- cf. entity_prepare_view().
  $variant = empty($node->variant) ? _uc_product_get_variant($node) : $node;

  // Skip the add to cart form in comment reply forms.
  if (\Drupal::routeMatch()
    ->getRouteName() != 'comment.reply') {

    // Build the 'add to cart' form, and use the updated variant based on data
    // provided by the form (e.g. attribute default options).
    if (\Drupal::moduleHandler()
      ->moduleExists('uc_cart') && $variant
      ->id() && empty($variant->data['display_only'])) {
      $form_object = new AddToCartForm($node
        ->id());
      $add_to_cart_form = \Drupal::formBuilder()
        ->getForm($form_object, $variant);
      if (\Drupal::config('uc_product.settings')
        ->get('update_node_view')) {
        $variant = $add_to_cart_form['node']['#value'];
      }
    }
  }
  $build['display_price'] = [
    '#theme' => 'uc_product_price',
    '#value' => $variant->display_price,
    '#suffixes' => $variant->display_price_suffixes,
    '#attributes' => [
      'class' => [
        'display-price',
        'uc-product-' . $node
          ->id(),
      ],
    ],
  ];
  if (isset($add_to_cart_form)) {
    $build['add_to_cart'] = $add_to_cart_form;
  }
  $build['#node'] = $variant;
}