You are here

function commerce_cart_handler_field_add_to_cart_form::render in Commerce Core 7

Render the field.

Parameters

array $values: The values retrieved from the database.

Overrides views_handler_field::render

File

modules/cart/includes/views/handlers/commerce_cart_handler_field_add_to_cart_form.inc, line 87

Class

commerce_cart_handler_field_add_to_cart_form
Field handler to present an add to cart form for the product..

Code

function render($values) {

  // Attempt to load the specified product.
  $product = $this
    ->get_value($values);
  if (!empty($product)) {

    // Extract a default quantity for the Add to Cart form line item.
    $default_quantity = $this->options['default_quantity'] <= 0 ? 1 : $this->options['default_quantity'];
    $product_ids = array(
      $product->product_id,
    );

    // Build the line item we'll pass to the Add to Cart form.
    $line_item = commerce_product_line_item_new($product, $default_quantity, 0, array(), $this->options['line_item_type']);
    $line_item->data['context']['button_text'] = filter_xss($this->options['button_text'], array());
    $line_item->data['context']['product_ids'] = $product_ids;
    $line_item->data['context']['add_to_cart_combine'] = $this->options['combine'];

    // Generate a form ID for this add to cart form.
    $form_id = commerce_cart_add_to_cart_form_id($product_ids);

    // Add the display path to the line item's context data array if specified.
    if ($this->options['display_path']) {
      if ($this->view->display[$this->view->current_display]->display_plugin == 'page') {
        $line_item->data['context']['display_path'] = $this->view->display[$this->view->current_display]->display_options['path'];
      }
      else {
        $line_item->data['context']['display_path'] = current_path();
      }
    }

    // Store the View data in the context data array as well.
    $line_item->data['context']['view'] = array(
      'view_name' => $this->view->name,
      'display_name' => $this->view->current_display,
      'human_name' => $this->view->human_name,
      'page' => $this->view->current_page,
    );

    // Build the Add to Cart form using the prepared values.
    $form = drupal_get_form($form_id, $line_item, $this->options['show_quantity'], array());
    return drupal_render($form);
  }
}