In magento blocks what does block type means as in the below block?
<block type="core/template" template="example/view.phtml" />
Please help me in learning magento module development.
Is there some good resource?
The block "type" is a PHP Class. In a normal system, a
core/template
block type will become a Mage_Core_Block_Template class.
As for resources, start here (self link)
Here's what I learnt -
type refers to the PHP file that will provide the methods for this phtml. Examaple:
/app/code/local/modstorm/Block/Filename.php , where all your methods for this block resides. So if, for example, you are have something like $this->getMethodName() in your .phtml, you must have getMethodName() function declared in /app/code/local/modstorm/block/Filename.php.
Hope this helps others as well.
Related
1) What $this/$resource variables refer to in Magento .phtml files?
2) Where do I find the list of methods I can use with $this/$resource?
print_r displays huge objects which functionality is not very easy to understand.
The template files (.phtml) are included in the block methods so a proper html can be returned. See the method Mage_Core_Block_Template::fetchView.
All of the block classes that have a template associated extend the Mage_Core_Block_Template class.
So $this is actually the current block class that is being used.
In theory one template can be used by multiple block classes. But this rarely happens.
If you don't know what $this is just add this in the template file:
echo get_class($this);
and you will get the class name.
If you want the available methods you can do this:
echo "<pre>"; print_r(get_class_methods($this));echo "</pre>";
But ...you will see that you are able to call some methods that don't really exist.
Because the class Mage_Core_Block_Template extends Varien_Object that implements the method __call you will be able to call any method that starts with get, set, uns, has without getting an error even if the method does not exist.
I haven't seen a phtml with the variable $resource but you can treat that the same as $this.
I didn't understand $resource that you are referencing with. But however $this stands for block that defines that template.
To make it more clear, suppose you have a layout code that seems like this
<some_handle>
<reference name="content">
<block type="xxx/yyy" name="custom.block" as="custom.block" template="custom/template.phtml" />
</reference>
</some_handle>
Now above in our demo layout, you can see a block is defined, which defines a template template.phtml. Now your template.phtml may look like this.
location : app/design/frontend/<package>/<theme>/template/custom/template.phtml
<div>
<?php $value = $this->getSomeMethod(); ?>
</div>
Here $this stands for the block that holds this template. In this case, $this is an instance of Namespace_Modulename_Block_Yyy block. (assume xxx stand for Namespace_Modulename module.).
So the method getSomeMethod() is not defined in this class, it will definitely shows an error.
Hope that gives you an idea.
Many extensions (including the one's I've written) include a helper class that just extend the abstract base class without adding any functionality. The helper usually looks like this:
class MyCompany_MyModule_Helper_Data extends Mage_Core_Helper_Abstract {
}
The extended class is therefore just used for things that the abstract class provides, especially for translations. On the other hand, all Block and Controller classes in Magento inherit the __() method for translations - and in an extension I'm currently developing I don't have the need to call the helper class even once.
Can I just delete the helper class and remove it from config.xml? I've tried it and the extension seems to work fine without, but due to Magento's complexity I'm always a bit worried that there are implications I'm not aware of.
If you're creating a module from scratch, helper classes aren't strictly necessary. I usually skip creating one until it's needed.
However, if any XML file uses the module attribute to specify a translation module, that attribute needs to resolve to a valid helper. For example, in this core file
<!-- File: app/code/core/Mage/Catalog/etc/system.xml -->
<tabs>
<catalog translate="label" module="catalog">
<label>Catalog</label>
<sort_order>200</sort_order>
</catalog>
</tabs>
There's module="catalog". By specifying this attribute, the Magento system code that translates the label will look something like this
Mage::helper('catalog')->__('Label');
So, removing the helper from the catalog module would break parts of Magento.
(The single part class alias catalog is automatically converted to Mage::helper('catalog/data') by Magento system code)
This "helper to group translations" feature is used in many of Magento's XML files, not just system.xml (layout, widgets, etc.). Additionally, there are some systems in Magento that will infer and/or require the existence of a helper module for translations (Access Control, external API system,etc. )
Long Story Short: If you're creating a module from scratch, feel free to leave the helper out until you start getting errors that Magento can't instantiate a helper. Never remove an existing helper from a module, and if you want to make sure you're 100% compatible with assumptions other people might make, always include a Data.php helper class.
Magento's Helper classes contain utility methods that will allow you to perform common tasks on objects and variables. http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento#6
Assuming that the Helper file is empty with no custom methods eg.
<?php
class MagePal_SomeModule_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Then some of things that may still be affected are:
system.xml - blank screen for your module in admin -> system -> config
$this->__('') - error in your .phtml template (for internationalization/translation)
So if your helper is empty, without a system config section and no translation then it 'maybe' ok to delete.
I want to create a block and add that block to my template via
$this->_addContent($this->getLayout()->createBlock("device/device"))
Right now, it is not displaying anything.
What are the points to be Noted so that my block will get rendered (what are the files to be aware off?)
Note:
COMPANY NAME: Abc
MODULE NAME: Device
Also, createBlock("device/device") returns "false"
The device/device string that is being passed to createBlock is a class alias. Class aliases give the Magento developer a way to refer to classes without using the actual class name. This indirection allows for one class to be substituted (or rewritten in Magento terminology) for another without having to change any code which instantiates and uses the class.
You start by defining the prefix for your classes in your module's config.xml file as follows (note: add this code into any existing tags, rather than just dropping it in at the bottom of the config.xml):
<config>
<global>
<blocks>
<device>
<class>Abc_Device_Block</class>
</devicer>
</blocks>
</global>
</config>
When building the class name for a block, the part of the xml is the part which comes before the / in the alias, and is substituted for the contents of the tags when generating the class name. The / is then replaced with an _ and the remaining part of the class alias is appended to the class name. So with the class alias device/device, and the above XML, the following class name will be built Abc_Device_Block_Device, which Magento will expect to find in Abc/Device/Block/Device.php. It will search folders in the order specified in the include_path, which is normally app/code/local, then app/code/community, followed by app/code/core and finally `lib.
The same basic logic also applies to model and helper classes.
Alan Storm's indispensable CommerceBug extension has a great tool for testing what model/block/helper class aliases map to in terms of class names and file locations.
The other parameters on this method are a name which can be used to refer to the block (and modify it) from layout XML files, and an array of other attributes which could be found in the layout XML.
That was an excellent answer Jim. Adding a point to it, the priority is the
Local
Community and then comes
Core if I was not wrong .
Hello I have trying to learn Magento, So would be appreciated if anyone helps.
I saw people initiate class
Mage::getResouceModel('report/product_sold_collection');
Then they iterate over the collection using foreach loop and use
getOrderedQty() method.
Like in this thread.
total sales of each product in magento
Where is this method defined? In which class ?
That method doesn't exist. All Magento Objects (that is, objects which inherit from Varien_Object) allow you to get and set data properties.
$object->setData('the_thing',$value);
echo $object->getData('the_thing')
On top of this, there's also special setter and getter methods implemented with PHP's "magic methods". That is, you can get/set a data property by camel casing it's name, and calling it like this
$object->setMyThing($value);
echo $object->getMyThing();
I searched the Magento codebase, and there's no definition for a "getOrderedQty" method. That means it's one of the above mentioned magic methods.
I am trying to update the "type" parameter of an existing Block in my theme layout XMLs.
In example, I would like to use the Block "catalog/rewrite_navigation" instead of "catalog/navigation" for the reference name "catalog.topnav".
I've tested several ways by reading thru core PHP files for layout, blocks, updates and so on but didn't succeed. I just would love to avoid using unsetChild and then recreating a block.
The "regular" way would be :
<reference name="top.menu">
<action method="unsetChild"><name>catalog.topnav</name></action>
<block type="catalog/rewrite_navigation" name="catalog.topnav" template="catalog/navigation/top.phtml"/>
</reference>
But do you guys have a clue to do something like this instead :
<reference name="catalog.topnav">
<action method="setType"><name>catalog/rewrite_navigation</name></action>
</reference>
With such an update the block type of the catalog.topnav would be updated from "catalog/navigation" to "catalog/rewrite_navigation".
Thanks a lot for your ideas !
I have never seen a syntax like this, and I do know that Magento creates the object before running actions on it (because it needs to have an instance before it can execute methods on that instance). That leads me to believe that there is no way to do this using actions.
Also beware of trying to do it by unsetting the block and adding it again. By the time your layout executes, other blocks may have already added children to that block, and they will simply fall off when you remove the block.
The canonical way to do this is simply to override the actual catalog/navigation block, so that it returns the class that you intend it to. If, for some reason, this doesn't work for you (e.g. someone else already overrode the block), you may need to modify the XML file to reflect your new class handle.
Hope that helps!