1

Ubercart's uc_cart module offers a 'cart' page which I'm willing to have a theme/template. What I did is added following in mymodule_theme():

    'uc_cart_view' => array(
        'render element' => 'page', // IS THIS CORRECT?
        'template' => 'cart', // Located in mymodule/templates/cart.tpl.php
        'path' => drupal_get_path('module', 'mymodule') . '/templates', 
    ),

But No luck!

My Question:

Shall I have some other value for render element key? Or shall I have a variables entry? I tried with variables with an empty array instead of render element entry but no luck.

Here goes some more information about Ubercart's uc_cart module so that you can help me solving this issue:

For Your Consideration

Portion from uc_cart_menu:

$items['cart'] = array(
    'title' => 'Shopping cart',
    'description' => 'View/modify the contents of your shopping cart or proceed to checkout.',
    'page callback' => 'uc_cart_view',
    'access arguments' => array('access content'),
    'file' => 'uc_cart.pages.inc',
  );

So I figured out I should use my custom template for uc_cart_view. This uc_cart_view() is a function in *uc_cart.pages.inc* page, I've pasted the function here for your convenience.

Any help on what I'm doing wrong?

1

uc_cart_view() is not a form builder; it is a page callback. As such, theme_uc_cart_view() is not automatically called for uc_cart_view(). It is the page callback that needs to call the necessary theme function, or refer it such as in the following code.

  if (empty($items)) {
    return array(
      '#theme' => 'uc_empty_cart',
    );
  }

What you can do is the following:

  • Implement hook_menu_alter() to alter the page callback invoked for http://example.com/cart.

    function mymodule_menu_alter(&$items) {
      if (isset($items['cart'])) {
        $items['cart']['page callback'] = 'mymodule_cart_view';
      }
    }
    
  • Implement the page callback to output the content as you want it to be.

I cannot suggest the code to write for mymodule_cart_view(). Looking at the code used from uc_cart_view(), I notice the main part is the following one.

  // Load the array of shopping cart items.
  $items = uc_cart_get_contents();

  // Display the empty cart page if there are no items in the cart.
  if (empty($items)) {
    return array(
      '#theme' => 'uc_empty_cart',
    );
  }

  $build = array();
  // Load through the cart panes...
  foreach (uc_cart_cart_pane_list($items) as $id => $pane) {
    // If the pane is enabled...
    if ($pane['enabled']) {
      // Add its output to the cart view.
      $build[$id] = $pane['body'];
    }
  }

What a page callback can return, in Drupal 7 is:

  • a string containing the markup to output
  • an array accepted from drupal_render(), such as the following ones

    $build = array(
      '#theme' => 'node', 
      '#node' => $node, 
      '#view_mode' => $view_mode, 
      '#language' => $langcode,
    );
    
    $block = array(
      '#markup' => '<em>' . t('There are no rows matching the selected criteria') . '</em>'.
    );
    
    $styles = array(
      '#type' => 'styles', 
      '#items' => $css,
    );
    

As side note, in your theme definition, the template filename cannot be "cart"; as the template file is for "uc_cart_view" you can report it to be "uc_cart_view" or "uc-cart-view." Any other filenames are not accepted, from Drupal.

0
$items['uc_cart_view_form'] = array(
    'template' => 'cart/uc-cart-view-form',
    'arguments' => array('form' => NULL),
 );

Means you should use uc_cart_view_form

  • Why? I'm willing to change mysite/cart page - should not it means that I should have a template for 'uc_cart_view'? – Shafiul Mar 28 '12 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.