Перейти к содержимому


Фотография
- - - - -

How to derive the item image from the category


Лучший Ответ Kess , 02 October 2014 - 05:57

Hello,

 

I think the easiest way is to make some small modifications to the jbimage element.

 

Here is one way to do it:

 

In the file

/media/zoo/applications/jbuniversal/elements/jbimage/jbimage.php

find the _getDefaultImage function and add these lines at the top:

$item = $this->getItem();
$mainCat = $item->getPrimaryCategory();
$mainCatImgPath = $mainCat->params->get('content.category_teaser_image', '');

and find this block of code:

if (strpos($imagePath, 'http') !== false) {
    $result = $imagePath;
} else if (JFile::exists($imagePath) || JFile::exists(JPATH_ROOT . '/' . $imagePath)) {
    return $this->app->jbimage->resize($imagePath, $width, $height);
} else {
    $result = $this->app->jbimage->placeholder($width, $height);
}

and add one more condition so it will look like this:

if (strpos($imagePath, 'http') !== false) {
    $result = $imagePath;
} else if (JFile::exists($imagePath) || JFile::exists(JPATH_ROOT . '/' . $imagePath)) {
    return $this->app->jbimage->resize($imagePath, $width, $height);
} else if (JFile::exists($mainCatImgPath) || JFile::exists(JPATH_ROOT . '/' . $mainCatImgPath)) {
    return $this->app->jbimage->resize($mainCatImgPath, $width, $height);
} else {
    $result = $this->app->jbimage->placeholder($width, $height);
}

That's seems to be enough.

 

Now go to the elements configuration of your item type and enable default image option for you image element while leaving its path blank. Now it will use the primary category image of your items.

Перейти к сообщению


  • Закрытая тема Тема закрыта
Сообщений в теме: 2

#1 dgwo

dgwo

Отправлено 29 September 2014 - 21:42

Hi,
 
I have created a blog application, with an image and text. The image has no default value. What I would like to achieve is that in case the image is not populated in the blog submission, the image must be derived from the category teaser image. So blogs in each category will get the same image, unless a specific image is selected for the blog.
 
In order to achieve this without modifying core files, I would like to use the jbevent item. In order to fetch the category teaser image url, I found some code in [TEMPLATE]/renderer/subcategory/_default.php that could be helpful:
 
$subcategory = $vars['object'];
$params = $subcategory->getParams('site');
 
[...]
 
    <?php if ($vars['params']->get('template.subcategory_teaser_image', 1) && $image['src']) : ?>
        <div class="subcategory-image align-<?php echo $imageAlign;?>">
            <a href="<?php echo $link;?>" title="<?php echo $subcategory->name; ?>"><img
               src="<?php echo $image['src']; ?>" <?php echo $image['width_height']; ?>
               alt="<?php echo $subcategory->name; ?>"
               title="<?php echo $subcategory->name; ?>"
            /></a>
        </div>
    <?php endif; ?> 
 
Unfortunately I cannot use the same way to get the category teaser image in the Event Handler, since the Event Handlers user static classes.
 
Do you have any hint how to proceed?

 

Oops, forgot to include some code:
 
$image = $this->app->jbimage->get('category_teaser_image', $params);

#2 Kess

Kess

Отправлено 02 October 2014 - 05:57   Лучший Ответ

Hello,

 

I think the easiest way is to make some small modifications to the jbimage element.

 

Here is one way to do it:

 

In the file

/media/zoo/applications/jbuniversal/elements/jbimage/jbimage.php

find the _getDefaultImage function and add these lines at the top:

$item = $this->getItem();
$mainCat = $item->getPrimaryCategory();
$mainCatImgPath = $mainCat->params->get('content.category_teaser_image', '');

and find this block of code:

if (strpos($imagePath, 'http') !== false) {
    $result = $imagePath;
} else if (JFile::exists($imagePath) || JFile::exists(JPATH_ROOT . '/' . $imagePath)) {
    return $this->app->jbimage->resize($imagePath, $width, $height);
} else {
    $result = $this->app->jbimage->placeholder($width, $height);
}

and add one more condition so it will look like this:

if (strpos($imagePath, 'http') !== false) {
    $result = $imagePath;
} else if (JFile::exists($imagePath) || JFile::exists(JPATH_ROOT . '/' . $imagePath)) {
    return $this->app->jbimage->resize($imagePath, $width, $height);
} else if (JFile::exists($mainCatImgPath) || JFile::exists(JPATH_ROOT . '/' . $mainCatImgPath)) {
    return $this->app->jbimage->resize($mainCatImgPath, $width, $height);
} else {
    $result = $this->app->jbimage->placeholder($width, $height);
}

That's seems to be enough.

 

Now go to the elements configuration of your item type and enable default image option for you image element while leaving its path blank. Now it will use the primary category image of your items.


  • 0

#3 dgwo

dgwo

Отправлено 08 October 2014 - 20:40

This solution works very good. Thanks!






Click to return to top of page in style!