Add css from a block - magento

I am building a custom magento module and i try to add a custom css file to my block. I wrote :
<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
protected function _prepareLayout()
{
$this->getLayout()->getBlock('head')->addCss('css/mycompany/mymodule/stylesheet.css');
return parent::_prepareLayout();
}
}
but it doesn't work, my css file is not loaded, any idea?
Thank you.

My alternative solution was to add this in my xml layout :
<default>
<reference name="head">
<action method="addCss"><stylesheet>css/interactiveslider.css</stylesheet></action>
</reference>
</default>
Thank you for your help

All the CSS & Images are normally available in the "skin" folder. It should be:-
"skin" folder
-> Package Name (like "base" or "default")
-> Theme Name (like "modern" or "mycompany")
-> "css" folder
-> "mymodule" folder
-> "stylesheet.css" file
So I suppose that you have been following this above-mentioned basic structure, which is considered as one of the best practices.
Coming back to your question, I suppose that you have mentioned the correct block class in your module's layout file "layout.xml". So the above code should be, according to the above folder structure:-
<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
protected function _prepareLayout()
{
$this->getLayout()->getBlock('head')->addCss('css/mymodule/stylesheet.css');
return parent::_prepareLayout();
}
}
Lastly, please make sure that you have uploaded your CSS file "stylesheet.css" in the correct folder.
Hope it helps.

You can only use the _prepareLayout() method if the block is defined in the layout XML. If you 'inline' the block inside a CMS page via the {{block type... method, the layout is already prepared by the time the block is rendered

Related

Magento - Set Category meta title, keywords and description

I have a category for shoes on my website. The meta title,description and keywords that I give under Catalog/Category for that category are not reflected. This is not specific to a category , I'm just quoting shoes as an example.
I dont want to use settitle,setkeyword,setdescription on layout files to set the above. Why is the default magento functionality not working.
It'd be great help if you could point out the area where the default values are applied.
For category layouts in the default magento theme, the area where the meta tags are set is :
//file: app/code/core/Mage/Catalog/Block/Category/view.php
//class: Mage_Catalog_Block_Category_View
//function: protected function _prepareLayout()
//...
$category = $this->getCurrentCategory();
if ($title = $category->getMetaTitle()) {
$headBlock->setTitle($title);
}
if ($description = $category->getMetaDescription()) {
$headBlock->setDescription($description);
}
if ($keywords = $category->getMetaKeywords()) {
$headBlock->setKeywords($keywords);
}
//...
So I think the questions to ask yourself are why is $category->getMetaTitle() falsey or what, later on in the layout, is overwriting $category->getMetaTitle() or does my theme rewrite the function Mage_Catalog_Block_Category_View::_prepareLayout().
You may want to search all your .xml files for code like this:
<reference name="head">
<action method="setTitle" translate="title" module="catalog"><title>Site Map</title></action>
</reference>
Because any module can change the meta tags using XML.

How to override product.js file in magento

I want to override the product.js file to add some extra values in some functions.I have copy the file from js/mage/adminhtml/product.js to my folder like js/myfolder/myproduct.js.How can i use the functions of the file as i try and its not working for me .When i change the object name than it will show no error but nothing happend (Products.Gallery = Class.create() orginal is Product.Gallery = Class.create();).
Can anyone tell me how to use all the functions of myproduct without conflicting of original funcitons.
Thanks
Let's say you're going to override function loadImage from js/mage/adminhtml/product.js in product edit page.
Create your custom js:
js/myfolder/myproduct.js:
Product.Gallery.addMethods({
loadImage : function(file) {
alert('hi there');
var image = this.getImageByFile(file);
this.getFileElement(file, 'cell-image img').src = image.url;
this.getFileElement(file, 'cell-image img').show();
this.getFileElement(file, 'cell-image .place-holder').hide();
}
});
Reference: http://prototypejs.org/learn/class-inheritance.html
Then in your layout xml add your custom js:
<adminhtml_catalog_product_edit>
<reference name="head">
<action method="addJs"><script>myfolder/myproduct.js</script></action>
</reference>
</adminhtml_catalog_product_edit>
Using this method, function loadImage will be overridden only if you include your js/myfolder/myproduct.js.
PS:
Make sure js/myfolder/myproduct.js is included after js/mage/adminhtml/product.js (though it is like that by default since js/mage/adminhtml/product.js included in <default> tag)

Magento custom module admin block display

struggling getting access to an admin block ive created.
Ive created a module...it has many elements, all working. Ive got header includes added to certain admin pages no problem, using my adminhtml layout update xml file.
The issue seems to be it cant access/see my block...so muct be referencing wrong, even though ive been following the 'module creator' extension files.
Another silly issue i think, been at this too long! :)
First the code:
Mworkz/MyModuleName/Block/Adminhtml/MyBlock.php
class Mworkz_MyModuleName_Block_Adminhtml_MyModuleName extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
var_dump('WE ARE IN THE ADMIN BLOCK!');exit;
$this->_controller = 'adminhtml_mymodulename';
$this->_blockGroup = 'mymodulename';
$this->_headerText = Mage::helper('mymodulename')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('mymodulename')->__('Add Item');
parent::__construct();
}
}
My layout xml (this file works, and is referenced right, as my admin header includes work)
Should point out i have a custom tab and controller...all working.
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodulename_adminhtml_mymodulename_index>
<reference name="head">
<action method="addJs"><script>Mworkz/MyModuleName.js</script></action>
</reference>
<reference name="content">
<block type="mymodulename/adminhtml_mymodulename" name="mymodulename" ></block>
</reference>
</mymodulename_adminhtml_mymodulename_index>
</layout>
I expect to see the var_dump stmt ive inserted....but it doesnt display.
Thanks in advance...
file naming! Simple caps issue...
My block file was called '...Adminhtml/MyModuleName.php',
My block identifier inside the file was '...Adminhtml_Mymodulename {'
Another set of working code snippets for adminhtml block users i suppose!
Thanks

Magento Adding a Block to an index Action Page

I am creating a module that will have its own page at mysite.com/memymodule/index/index
I want to add functionality from a template file from at templates/me/template.phtml
I have an index controller like this:
<?php
class me_mymodule_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
}
?>
I have a layout update mymodule.xml that looks like this:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_index_index>
<reference name="content">
<block type="core/template" name="me.mymodule" template="me/template.phtml" />
</reference>
</mymodule_index_index>
</layout>
The frontName of the module is mymodule
When the page renders the content block is completely empty and the content of template.phtml is completely ignored.
Help much appreciated :-)
Try this Code within your Controller's Action -
var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
exit("Your Layout Path is ".__LINE__." in ".__FILE__);
This code tells you about the Tag which you need to create within Layout.xml.
Also check Config.xml that the Layout Update Section is correctly defined or not.
Hope it'll be beneficial for you.
THANKS

Magento - Checking if a module is installed?

I have a small piece of code in a template file that I ONLY want to run if a certain module is installed. I found the below code, which you can use to find if a module is active, but I want to know if a module is installed.
$modules = Mage::getConfig()->getNode('modules')->children();
$modulesArray = (array)$modules;
if($modulesArray['Mage_Paypal']->is('active')) {
echo "Paypal module is active.";
} else {
echo "Paypal module is not active.";
}
I'm thinking I could maybe get a list of names of all the modules that are installed, and then use
if (stristr($modulelist, 'Name_Extension'))
to show my code only if the referenced extension is installed.
Anyone any ideas how to do that? Or any better solutions?
There's a core helper for that:
Mage::helper('core')->isModuleEnabled('MyCompany_MyModule');
It's in Mage_Core_Helper_Abstract.
There's also a isModuleOutputEnabled() method to check if output of module is disabled in System -> Configuration -> Advanced -> Disable Modules Output.
Try this:
$modules = Mage::getConfig()->getNode('modules')->children();
$modulesArray = (array)$modules;
if(isset($modulesArray['Mage_Paypal'])) {
echo "Paypal module exists.";
} else {
echo "Paypal module doesn't exist.";
}
Another way to find if a module is installed but disabled is with:
if (Mage::getStoreConfigFlag('advanced/modules_disable_output/Mage_Paypal')) {
echo "Paypal module is installed";
}
Edit
Have just realised a version of this - using the little known ifconfig - could show a block only if another module is disabled. eg.
<layout>
<default>
<reference name="content">
<block ifconfig="advanced/modules_disable_output/Mage_Paypal" type="core/template" template="sorry/this/is/unavailable.phtml" />
</reference>
</default>
</layout>
You can check if a module exists on installation using this snippet Mage::getConfig()->getNode('modules/Mage_Paypal') returns FALSE if does not exist
in the declaration of your module, try adding a depends element, like on this page.
This will probably raise an exception, which you could handle with a try/catch block.
In any class, model, controller or even PHTML you can call and it will work.
Data.php
class Company_Module_Helper_Data extends Mage_Core_Helper_Abstract
{
const XML_PATH_GEN_ENABLE = 'Section/Group/Field';
public function isEnable()
{
return Mage::getStoreConfigFlag(XML_PATH_GEN_ENABLE,Mage::app()->getStore()->getId());
}
}
You can call using below code Mage::helper('module_name')->isEnable()
Your config file is like
config.xml
<global>
<helpers>
<module_name>
<class>Company_Module_Helper</class>
</module_name>
</helpers>
// Another Necessary Code for this extension
</global>

Resources