Yaroslav
Yaroslav

Reputation: 6554

Extending / overriding extension in Magento

I have an extension installed and I want to use its funcionality from my modules. The postAction in that extension is where all happens. It uses youtube API to retrieve a video information and save it on several tables on the Magento EAV data model.

Already have a functional module that I created to test youtube API functions using just a button and a text box to send some search term. But now I want to do it automatically using the extension funcionalities to make that call and fill in the necessary tables instead of doing everything manually from my code.

So I need (or want? or must?) to setup a call to that postAction or extend or override it. I'm lost here, I'm new to Magento and PHP so I haven´t a clear idea on what to do.

This is the class I want to call:

/**
 * Youtube Upload Controller
 */
class AW_Vidtest_YoutubeController extends Mage_Core_Controller_Front_Action {
.....
}

And inside it the postAction function:

/**
 * Get customer video
 */
public function postAction() {
    $data = new Varien_Object($this->getRequest()->getPost());
....
}

I have read the information on these links but I'm not clear on what exactly I'm must do. Follow the Observer pattern? Maybe just creating a post call by myself and somehow adding the $data structure so it can be used on the call? Thanks

Edited: This is the code I have until now, with suggestions made by @Francesco. The function printVideoEntry is called from other function, inside a for each that for now walks the first 3 products on the catalog.

<?php
class Dts_Videotestimonials_Model_SearchVideo extends Mage_Core_Model_Abstract
{
    public $search_term;
    private $productModel;

    function printVideoEntry($videoEntry, $_product, $tabs = "")
    {
        # get user data
        $user = Mage::getSingleton('admin/session');
        $userName = $user->getUser()->getFirstname();
        $userEmail = $user->getUser()->getEmail();
        $data = array(
            "ProductId" => $_product->getId(),
                        "AuthorEmail" => $userEmail,
                        "AuthorName" => $userName,
                        "VideoLink" => $videoEntry->getVideoWatchPageUrl(),
                        "VideoType"  => "link",
                        "Title" => $videoEntry->getVideoTitle(),
                        "Comment" => "this is a comment"
        );
        $actionUrl = Mage::getUrl('vidtest/youtube/post');
        Mage::app()->getResponse()->setRedirect($actionUrl, $data);
    }
}

Upvotes: 1

Views: 2531

Answers (2)

WonderLand
WonderLand

Reputation: 5674

It is not easy to give a clear answer ... the question is not clear because we don't know how the youtube extension works. ( the code is crypted or open ? )

Call a Controller's Action

If you want to just call postAction you can use _redirect($path, $arguments=array()) method. ( defined in Mage/Core/Controller/Varien/Action.php )

  1. $path is defined as 'moduleName/controllerName'
  2. $arguments=array() are defined as couple parameterName => Value.

Ex.

$this->_redirect('checkout/cart', array('Pname' => $pValue, ... );

This will work only if you call it from a Controller ...

you can find more info about _redirect here: magento _redirect with parameters that have + or /

In case you want to do a redirection from a model or any different file form a Controller one you will need to call the url in this way :

Mage::app()->getResponse()->setRedirect(Mage::getUrl($path, $arguments=array()));

so the above ex. becames:

Mage::app()->getResponse()->setRedirect(Mage::getUrl('checkout/cart', array('Pname' => $pValue, ... ));

Observer

Using an Observer means add a new model to your module ( the observer ) and write inside this class a method that perform an action under certain events, probably you want to calls some model/method of the yt extension.

Then you have to declare this stuff in you config.xml binding you observer method to some event ( any predefined even in Magento that suit you or if you need you should create your own rewriting the magento class ... )

Example for Observer

  • PackageName/ModuleName/Model/Observer.php

    class PackageName_ModuleName_Model_Observer {
    
        public function myActionMethod(Varien_Event_Observer $observer) {
    
            // Code ... 
            // just a very indicative example
            $model = Mage::getModel('youtubeExtension/Model....').method();
    
        }
    
    }
    
  • PackageName/ModuleName/etc/config.xml

    <config>
    ....
    <global>
        <events>
            <EventName>
                <observers>
                    <moduleName_observer>
                        <type>singleton</type>
                        <class>PackageName_ModuleName_Model_Observer</class>
                        <method>myActionMethod</method>
                    </moduleName_observer>                         
                </observers>
            </EventName>
        </events>
    </global>
    ....
    

Obviously change EventName and all fake name according to your package/module/methods names

The most of the difficult is to find the right event that suit you ... Everytime you see in magento code something like Mage::dispatchEvent('EventName', Parameters); this is an event.

you can find a list of default Magento event Here

I hope it helps you

Upvotes: 1

drsndodiya
drsndodiya

Reputation: 1685

Just try to extends your module class

class AW_Vidtest_YoutubeController extends Mage_Core_Controller_Front_Action {
.....
}

example class AW1_Vidtest1_YoutubeController1 extends AW_Vidtest_YoutubeController { ..... }

where AW1_Vidtest1_YoutubeController1 Aw1 is namespace Vidtest1 is your module name YoutubeController1 is your controller where you want post action to use.

Hope it's work for you

Upvotes: 1

Related Questions