Magento - Review helper - magento

I'm trying to instantiate the review helper class but failing..
I need to access getReviewsSummaryHtml() of class Mage_Review_Block_Product_View (app/code/code/Mage/Review/Block/Product/View.php).
I've tried this:
Mage::helper('review/product')->getReviewsSummaryHtml($_product, false, true);
But I receive Fatal error: Class 'Mage_Review_Helper_Product' not found.
What am I doing wrong?
(p.s. I can't use $this->getReviewsSummaryHtml as $this is out of scope.)
Thanks

The method getReviewsSummaryHtml() is defined in Mage_Review_Block_Product_View. You can instantiate it anywhere using Mage::app()->getLayout()->createBlock('review/product_view',$product);. In order for this to work, though, you'll need to also have a block instance named product_review_list.count, which is normally defined in review.xml, is of type core/template, and uses the review/product/view/count.phtml template.

You should simply do :
Mage::helper('review')
in order to get helper class which is named as Data.php
The function getReviewsSummaryHtml() resides in a block and you should only be able to call that function (ideally) from you template.
If you have moved that function to helper than you can call it like :
Mage::helper('review')->getReviewsSummaryHtml();
and you should do all these changes in your local.

Related

Trying to understand the view method and general method calling in laravel

I am a new to laravel and trying to understand where the view method comes from and what mechanism allows it to be shown in the web.php folder in laravel.
For example :
Route::get('/', function () { return view('welcome'); })
I guess the view function is defined in some class. Bu which class is it and where is that class made reference to in order to access its method?
Thanks a lot if you can help me understanding this!
In most IDEs you can hold CTRL and left-click the function to view it's definition. view() is not defined in a class. It comes from a file called helpers.php.
This file is included at the beginning, so its functions can be used afterwards.
PHP is not only object oriented. Procedural and object oriented programming can be mixed together.
What I do usually in these cases is to search in the whole project (and remember to include vendor directory in your search) for: "function YOUR_FUNCTION_NAME" because somewhere in PHP there must be that function declared, whether is in a class or in a simple .php file.
view() method is a helper method inside src/Illuminate/Foundation/helpers.php. All the methods that declare here will be available everywhere inside Laravel application. You can check view() method here

Using helper function inside a Core Class in CodeIgniter

There is a helper which I would like to use inside a core class, CI_Router (MY_Router, to be more accurate). In this custom router, I made some modifications to the original code, in order to be able to insert hyphens into my urls.
I have defined the helper on the autoload.php file, as usual, but it seems that I canĀ“t invoque a helper function inside a class other than a view or controller.
Any ideas about how to handle this? My initial approach was to use a helper, so I can reuse it on any place I want.
TYVM.
Helpers are not instantiated until after the core, thus why it does not work.
You will either have to:
Duplicate the function in your MY_Router class, or,
Rethink why you might be using the same function in the Router that you use in a standard controller or view.
Option 1 is obviously easier, but might not be preferable depending on how bad your OCD is.
You could try getting the instance of the main CI object and setting it to a variable, then load the helper using that. Ex:
$ci =& get_instance();
$ci->load->helper('date');
I know that works in other areas, not 100% sure about any of the router classes.

How do I extend default joomla class / function?

I want to extend the submit file function in Joomla in an extension I'm making to custom handle files, how do I do that? Can I get some code samples please.
I've been told to extend them in the Model, but whenever I try to extend anything I get an error, even if I have an empty function that just says "..extends xxxx{}.
see below url and read :-
http://docs.joomla.org/How_Joomla_pieces_work_together
Or
class <name>Controller extends JController
{
}

Magento - Block is not rendered

I'm developing my second Magento module, which should retrieve some data and render a block containing it. Such block would then be used by other pages.
At the moment, I have almost the whole module in place, but, for some reason, the block is not rendered when I call the controller method. I put some debug information, and I could see that the block's __construct() method is called correctly, but then the template doesn't seem to be loaded and the returned page is blank.
Here's the code for the block, which I copied from another module and modified:
class Company_CustomerData_Block_CustomerSummary extends Mage_Core_Block_Template {
const _TEMPLATE = 'customerdata/customersummary.phtml';
public function __construct() {
// This method is called correctly
parent::_construct();
$this->setTemplate(self::_TEMPLATE);
}
}
The file customersummary.phtml is in app/design/frontend/base/default/template/customerdata, which should be the correct place (or, at least, I think it is). Its content is the following:
It works!
Just some plain text. No tags, no code, nothing. I don't mind that it's a static text, it will be populated with data once complete.
In case of need, here's the code for the Controller (I removed the parts where the data is retrieved, as they don't make a difference):
public function dashboardAction() {
// Customer Data to render in the block
$CustomerData = array(); // Data is retrieved elsewhere
$this->getResponse()->setBody(
$this->getLayout()->createBlock('customerdata/customersummary')
->toHtml()
);
}
What could I be doing wrong? I'm afraid I made some stupid mistake again, but I really can't see it.
Finally two more questions:
How do I pass to the template the data I retrieve in the Controller? Specifically, variable $CustomerData.
Once the block renders, how do I render its content from within a page? The block should return a with some stuff in it, I'd like to render it inside the Customer Dashboard, just below the information that is already there.
Thanks in advance for the help.
Most classes on Magneto derive from Varien_Object. Varien_Object (and it's descendents) claim PHP's constructor (__construct) for themselves, and provide an _construct callback which you can use for whatever you like. What this means is that if you override the native PHP constructor (__construct) you need to remember to do a few things:
Accept the same number of parameters as the base class, and...
Call the parent constructor (parent::__construct) with the parameters your constructor was supplied.
Or, alternatively, use the _construct callback supplied by Varien_Object and you're done. There's no need to remember to call parent::_construct if you're using the Magento callback.
So to fix your code snippet above, you can either change...
public function __construct() {
... to ...
public function _construct() {
This will switch you over the using the Magento callback. Or you can change...
parent::_construct();
.. to ...
parent::__construct();
... to call the parent class' constructor. Remembering to add in the parameters that the parent class supplies.
Alan Storm write a great article about Magento's Block Lifecycle and call back methods recently, which might be of assistance.
To your other questions:
Passing information from the controller to a block is generally done via Magento's registry.
Rendering your template as part of a page requires that you create layout instructions (written in XML) which load your block and place it inside a parent block.
Although Jim's Answer is correct, I'm adding a second part because I found out what else was wrong: symbolic links. As I usually do when I develop plugins for a framework, I use symlinks to avoid copying the files over and over again. However, Magento uses function RealPath(), which resolves the symlink to its full path. As a result, the Template file to be loaded resides in a path outside Magento installation directory, and it can't be loaded for security reasons. This makes perfectly sense, pity it's just not very visible.
To fix the issue, I enabled Allow Symbolic Links in configuration on my Development PC, and now the Template is loaded and rendered correctly.
I think that Magento could do with a Log Viewer in the Admin interface. If there isn't a module that does it already, perhaps I should create one myself.
Thanks everybody for the help.

Magento - create a helper class

I'm having trouble figuring out how to create a helper class with a function in it that's available to *.phtml files.
Can someone describe step by step how I can make the function prtHelloWorld() available to all my *.phtml files?
it's rather simple and you have to call your helper from template like this:
Mage::helper('yourmodule/yourclassfile')->prtHelloWorld();
Default helper class is Data and this defaults to Yourmodule/Helper/Data.php
Mage::helper('yourmodule')->prtHelloWorld();
To add to Anton S's, if you want to be able to access the helper's function using $this->prtHelloWorld() in the phtml instead, add it to your Block like this:
public function prtHelloWorld() {
return Mage::helper(whatever)->prtHelloWorld();
}

Resources