How to add a bootstrap class to JHtml in Joomla - joomla

I am using following piece of code to display an image which I am fetching from gravatar.com, so I want to use a bootstrap CSS class to make it more attractive. As soon as I add the style rather than showing the image it shows me the link to the image, when I redirect to the link I am able to see the image. Why I am getting this?
$html[] = JHtml::_('image', $grav_url,'class="img-circle"', JText::_('PLG_CONTENT_AVATAR'), null, true)
Here $grav_url is the url I am getting for the image and img-circle is the bootstrap class that I want to use.

I believe the library you're using is here:
https://github.com/joomla/joomla-cms/blob/staging/libraries/cms/html/html.php#L567
So you can see in the parameter list that there is a param for $attribs which is of the type array. The other thing is there is an additional $alt param you have to pass which might be your jtext just out of order. To pass that to the method you would do:
$html[] = JHtml::_('image', $grav_url, JText::_('PLG_CONTENT_AVATAR'), array('class'=>'img-circle'), true)
Or you could build the array outside the method as a variable:
$attribs = array();
$attribs['class'] = 'img-circle'; // I think this should work? haven't tested though.
$html[] = JHtml::_('image', $grav_url, JText::_('PLG_CONTENT_AVATAR'), $attribs, true)

Related

Codeigniter custom helper for bbcode, apply function on parameter

I made a custom bbcode parser function and added it on my helper
if ( !function_exists('bbcode_parser'))
{
function bbcode_parser($str)
{
$patterns = array(
'#\[b\](.*?)\[/b\]#is',
'#\[img\](.*?)\[/img\]#is',
'#\[url\](.*?)\[/url\]#is',
'#\[url=(.*?)\](.*?)\[/url\]#is'
);
$replacements = array(
'<strong>$1</strong>',
'<img src="$1" />',
'$1',
'$2',
);
$str = preg_replace($patterns, $replacements, $str);
return $str;
}
}
It's good and works like I want it to but my question is how to apply a function on each replacement value.
fe. for the url that doesn't have inner data, i want to replace with the website title of the url
or validate the url if it has http://
i would also like to check the size of the image, if it's too large, i want to resize it when printed by adding a "width" attribute and then just add an tag to the full size image.
is this possible? if so, how to do this?
You can implement this with preg_replace_callback()
http://www.php.net/manual/en/function.preg-replace-callback.php
Only the matched elements will be passed to the callback, so you would be able to create the callbacks you need or apply a standard replace for the normal regex patterns.

Render module in the right position from code (Joomla 2.5)

In my component view, I can show a module by this code:
$module = JModuleHelper::getModule('mod_login');
$html = JModuleHelper::renderModule($module);
echo $html;
or
echo JHtml::_('content.prepare', '{loadmodule login}');
But that will usually place the module in the middle of the screen in the main-content div.
How can I place the module in the position defined in the module params?
Take a look at how the admin template Isis renders the quickicons module on the home page or at how the error page in protostar renders the search modules.
$this->searchmodules = JModuleHelper::getModules('position-0');
foreach ($this->searchmodules as $searchmodule)
{
$output = JModuleHelper::renderModule($searchmodule, array('style' => 'none'));
$params = new JRegistry;
$params->loadString($searchmodule->params);
echo $output;
}
You just want one module, but you get the basic idea.
Try This.
You can Pass the module parameters too.
$document = &JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$Module = &JModuleHelper::getModule('mod_fmDataGrid');
$Params = "param1=bruno\n\rparam2=chris";
$Module->params = $Params;
echo $renderer->render($Module);
This may help you..
You can't, jdoc includes in the template are processed after the component has finished producing its output.
Why not let Joomla render the modules in the right positions? that should be one of the reasons for using it, i.e. taking care of placement and order of modules.
If you have other reasons for doing this, please explain further.

Hide Products without images magento

I have followed this answer to hide products without images on the category listing page. It worked nicely for a while.
Now, for some reason it seems the products without images are still showing up on the listing page.
Any ideas as to why this may be happening?
Note:
The same list.phtml page is being used.
Thank you.
Add the following to list.phtm:
//$_productCollection=$this->getLoadedProductCollection();
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
->load();
This answer recommended the following:
->addAttributeToFilter('image', array('neq' => 'no_selection'))
Whereas I have set it to:
->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
The reason the previous answer did not work was because the product collection doesn't load the regular image, and therefore the regular image cannot be added as an attribute to filter, so instead, I added the small_image as the attribute to filter.
You can also try R.S's answer where he adds the image to the page and hence the collection. You may have to also add all attributes using:
->addAttributeToSelect('*')
There are some tricks to keeping Magento in line. One thing I've learned is that the Magento Model will change for many different reasons, and its kinda hard to figure out why. There are better ways to do this (modifying the collection, etc) but it sometimes just does not work and you don't have days to figure out why.
If you want a surefire way to make sure your image exists, use the following code... It may not be the 'magento way' but it works, and I tested it on my site (Magento EE 1.12). Put it in a function, or use it directly in your phtml if you want!
It basically just makes sure the URL exists.
$exists = false;
$entity_id = 8800;
$product = Mage::getModel('catalog/product')->load($entity_id);
$mediaUrl= Mage::getBaseUrl('media');
$imageUrl = $mediaUrl . "catalog/product" . $product->getImage();
$file_headers = #get_headers($imageUrl);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
var_dump($exists);
var_dump($imageUrl);
echo '<img src="' . $imageUrl . '" />';
$exists will either be true (image does exist) or false (image does not)
I think that the issue is that you are trying to get the 'image' (base image) property on the list.phtml (by default i can only access the thumbnail, small_image).
On list.phtml (not loading the full product resource like on view.pthml)
echo $_product->getImage() //null
echo $_product->getThumbnail() // path/name.jpg
echo $_product->getSmallImage() // path/name.jpg
I think you may need to add adding something like this to app/design/frontend/default/yourtheme/layout/catalog.xml
<action method="addAttribute"><name>image</name></action>
See How to get access to custom Image Attribute in list.phtml in Magento

Getting image for bundle product child products, currently returning placeholder

My problem:
I need to retrieve the main product image from a cut down product object which is supplied by the class: Mage_Bundle_Model_Resource_Price_Index
My code demonstrating the issue:
$_product = $this->getProduct();
$_options = Mage::helper('core')->decorateArray($this->getOptions());
foreach($_options as $_option):
$_selections = $_option->getSelections();
foreach ($_option->getSelections() as $tmpsel) {
var_dump($tmpsel->getImageUrl());
}
Which returns my placeholder image:
http://dev.dev/media/catalog/product/cache/7/image/265x/0dc2d03fe217f8c83829496872af24a0/placeholder/default/logo_3.jpg
My horrible and hacky work around:
In order to get correct image Url, I have resorted to loading a completely new product object, which is terribly inefficient.
foreach ($_option->getSelections() as $tmpsel) {
$product = Mage::getModel("catalog/product")->load($tmpsel->getId());
$image = Mage::helper('catalog/image')->init($product, 'image');
var_dump($image."");
}
This returns correctly:
http://dev.dev/media/catalog/product/cache/7/image/0dc2d03fe217f8c83829496872af24a0/M/P/MP1428219-107-Main.jpg
What I want to do:
I want to be able to use the catalog/image helper with the selection ($tmpsel), but when I try I end up getting the placeholder image again.
foreach ($_option->getSelections() as $tmpsel) {
$image = Mage::helper('catalog/image')->init($tmpsel, 'image');
var_dump($image."");
}
Extra Info:
Anything I think of that could help I will add here
Cut down product object includes some reference to image
'small_image' => string '/M/P/MP1428219-107-Main.jpg'
'thumbnail' => string '/M/P/MP1428219-107-Main.jpg'
Description of getSelection()
In: ./app/code/core/Mage/Bundle/Model/Resource/Price/Index.php
* Retrieve bundle options with selections and prices by product
The function uses low level SQL to generate the collection, so I can always extend it to add options if needed, not sure which options though.
Thank you for reading, hope someone can give me a good way of doing this, will keep updated.
Init() method require a product object
init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
Try
<?php echo $this->helper('catalog/image')->init($tmpsel, 'small_image')->resize(40,40); ?>
You do not have 'image' but you do have 'small_image' attribute
See Magento - how to retrieve bundled option images
To get the full web address you can use this code:
Mage::getSingleton('catalog/product_media_config')->getMediaUrl($product->getImage());
The limitation is that bring only the main image.
Oh wow, I guess I can thank you stack overflow for making me write my thoughts down.
After comparing the full product object and the semi product object I can see that I was calling the incorrect attribute name for catalog/image
$image = Mage::helper('catalog/image')->init($tmpsel, 'image');
Should have been
$image = Mage::helper('catalog/image')->init($tmpsel, 'small_image');

Pagination on Pyrocms pages

I am currently displaying a list of products on pyrocms page. Using plugin i get the products from db tables and display them as a list i want to use pagination but have no idea how to use it and where to start from. Does any one know any tutorial or have some suggstion?
You won't need to load pagination library or initialize it. It is a little different from you do in regular codeigniter, also you can surely use the way you do it in codeigniter pagination class
I usually do this for pagination.
in your controller use this
....
// Create pagination links
$total_rows = $this->products_m->count_all_products();
$pagination = create_pagination('products/list', $total_rows);
//notice that product/list is your controller/method you want to see pagination there
// Using this data, get the relevant results
$params = array(
'limit' => $pagination['limit']
);
$this_page_products = $this->product_m->get_all_products($params);
....
//you got to pass $pagination to the view
$this->template
->set('pagination', $pagination)
->set('products', $this_page_products)
->build('products/view');
obviously, you will have a model named products_m
At your model this would be your get_all_products function, I am sure you can build the count_all_products() function too
private function get_all_products($params){
//your query to get products here
// use $this->db->limit();
// Limit the results based on 1 number or 2 (2nd is offset)
if (isset($params['limit']) && is_array($params['limit']))
$this->db->limit($params['limit'][0], $params['limit'][1]);
elseif (isset($params['limit']))
$this->db->limit($params['limit']);
//rest of your query and return
}
Now at your view you have to foreach in your passed $products variable and to show pagination links use this line in your view
<?php echo $pagination['links'];?>
I use this in my works, hope it help.
Pyrocms used codeigniter framework this code works perfectly in case of module but i have never tried this on a plugin but still you can try this.
Load pagination library in Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Use this code in view to genrate the links
echo $this->pagination->create_links();

Resources