Убираем вывод блока дополнительных картинок из uc_product, если он пустой

Главные вкладки

Аватар пользователя Darteg Darteg 3 июля 2011 в 4:42

Возникла в проекте необходимость не выводить через модуль ubercart product (uc_product.module) открывающий и закрывающий блоки <div class="more-product-images"> дополнительные картинки здесь </div> на конечную страницу при отсутствии в этом блоке картинок. Блок темизирован и без картинок получается не в тему.

Постил запрос о решении на drupal.org, но там все на морозе - не ответили. Пришлось думать самому. Т.к. PHP я только начал осваивать придумал только приведённое ниже решение, наверное не элегантно и говнокод страшный, но по-другому пока не умею. Буду благодарен за подсказки если где накосячил, ну и для развития.

+ была необходимость засунуть clearfix, тоже там есть.

<?phpfunction theme_uc_product_image($variables) {
  static $rel_count = 0;
  $images = $variables['images'];

  // Get the current product image widget.
  $image_widget = uc_product_get_image_widget();

  $first = array_shift($images);

  $output = '<div class="product-image"><div class="main-product-image">';
  $output .= '<a href="' . image_style_url('uc_product_full', $first['uri']) . '" title="' . $first['title'] . '"';
  if ($image_widget) {
    $image_widget_func = $image_widget['callback'];
    $output .= $image_widget_func($rel_count);
  }
  $output .= '>';
  $output .= theme('image_style', array(
    'style_name' => 'uc_product',
    'path' => $first['uri'],
    'alt' => $first['alt'],
    'title' => $first['title'],
  ));
  $output .= '</a></div>';
  foreach ($images as $thumbnail) {
    // Node preview adds extra values to $images that aren't files.
    if (!is_array($thumbnail) || empty($thumbnail['uri'])) {
      continue;
    }
    if ($rel_count = 1) { 
      $output .= '<div class="more-product-images">';
      $moreimages = TRUE;
    }
    $output .= '<a href="' . image_style_url('uc_product_full', $thumbnail['uri']) . '" title="' . $thumbnail['title'] . '"';
    if ($image_widget) {
      $output .= $image_widget_func($rel_count);
    }
    $output .= '>';
    $output .= theme('image_style', array(
      'style_name' => 'uc_thumbnail',
      'path' => $thumbnail['uri'],
      'alt' => $thumbnail['alt'],
      'title' => $thumbnail['title'],
    ));
    $output .= '</a>';
    if ($moreimages = TRUE) { 
      $output .= '</div>';
    }
  }
  $output .= '<div class="clearfix"></div></div>';
  $rel_count++;

  return $output;
}?>

Вроде работает, чо. Smile

Комментарии

Аватар пользователя Darteg Darteg 3 июля 2011 в 4:45

Пока усиленно не тестировал, поздно уже. Завтра буду тестить и дорабатывать, наверняка где-то косяк есть. К использованию не рекомендуется.

Аватар пользователя Darteg Darteg 4 июля 2011 в 2:39

Не занимайтесь таким под утро.

Вечером сделал рабочий вариант:

<?php
function theme_uc_product_image($variables) {
  static 
$rel_count 0;
  
$images $variables['images'];

  

// Get the current product image widget.
  
$image_widget uc_product_get_image_widget();

  

$first array_shift($images);

  

$output '<div class="product-image"><div class="main-product-image">';
  
$output .= '<a href="' image_style_url('uc_product_full'$first['uri']) . '" title="' $first['title'] . '"';
  if (
$image_widget) {
    
$image_widget_func $image_widget['callback'];
    
$output .= $image_widget_func($rel_count);
  }
  
$output .= '>';
  
$output .= theme('image_style', array(
    
'style_name' => 'uc_product',
    
'path' => $first['uri'],
    
'alt' => $first['alt'],
    
'title' => $first['title'],
  ));
  
$output .= '</a></div>';
  
  if (
$images) {
  
$output .= '<div class="more-product-images">';
  foreach (
$images as $thumbnail) {  
    
// Node preview adds extra values to $images that aren't files.
    
if (!is_array($thumbnail) || empty($thumbnail['uri'])) {
      continue;
    }
    
$output .= '<a href="' image_style_url('uc_product_full'$thumbnail['uri']) . '" title="' $thumbnail['title'] . '"';
    if (
$image_widget) {
      
$output .= $image_widget_func($rel_count);
    }
    
$output .= '>';
    
$output .= theme('image_style', array(
      
'style_name' => 'uc_thumbnail',
      
'path' => $thumbnail['uri'],
      
'alt' => $thumbnail['alt'],
      
'title' => $thumbnail['title'],
    ));
    
$output .= '</a>';
  }
  
$output .= '</div>';
  } 
  else
  {
  }
  
$output .= '<div class="clearfix"></div></div>';
  
$rel_count++;

  return 

$output;
}
?>