[solved]using a frontend module in another module
Hello,
I am writing a module that upload data in a BLOB.
My first question is : how to call this front end page in another module?
I try :
$strTemplate = new FrontendTemplate();
$this->Template = new FrontendTemplate('fe_my_other_form');
The template is displayed, but the code of the module is not called (like a stupid form that do nothing).
My second question is : what is the best way to send info (id) from a module to another in front end? (Session, url...)
Thanks
Re: using a frontend module in another module
Hi synergie
Well, obviously you will never get any code if you just call the other module's template and not the whole module ;)
I guess what you are looking for is
Code:
$this->getFrontendModule();
although this only works with an ID of a module that has been created in the backend.
Otherwise you will have to do something like this:
Code:
$objModule = new ModuleClass();
$objModule->compile();
Second question:
That depends on the requirements. When you are using GET-parameters the user can change the id and do stupid things with it but it is easy to write whereas the session thing is maybe better if the user should not be able to change his choice but it is also a little bit more difficult to code(not really as Contao provides a Session class). :)
Re: using a frontend module in another module
Hello
Thanks that the best answer ever made! This is working.
See U
Re: using a frontend module in another module
Hello,
Just another little question : if I try to create an object of my module without a query in parameter I run a syntax error!
Code:
$objModule= new myModule();
So I need to do that and this is working :
Code:
$sql = $this->Database->prepare("SELECT * FROM tl_xyz")->execute();
$objModule= new myModule($sql);
why? this is a bit stupid I do not need this query. How to remove that? by writing a constructor in myModule? I did not found any example in other modules...
Thanks
Re: using a frontend module in another module
How should Contao be able to generate a module then? Without any data...
Think about it again :)
Re: using a frontend module in another module
Quote:
Originally Posted by Toflar
How should Contao be able to generate a module then? Without any data...
Think about it again :)
But my module do not need any data in input...
I do not use the result of "SELECT * FROM tl_xyz" in my module, this is just a query for nothing...
Code:
$sql = $this->Database->prepare("SELECT * FROM tl_xyz")->execute();
$objModule= new myModule($sql);
myModule is a form to upload a file in a blob, he give back the id of the blob. But he need nothing as input!
Re: using a frontend module in another module
A Module doesn't require any parameters, unless your module requires them...
What you most likely want to do it to parse() the output of your module using a template and insert that into a parameter into your current module. This is clearly demonstrated by the News system.
News List Module uses a template mod_newslist.tpl
However, to generate the list, it also passes the current record of the list to another template which renders that sub-part. Its not needed to run another module in this case.
Also, its possible to just instantiate your module manually and then run whatever command you need, e.g.
Code:
$objModule = $this->Import('Newsletter');
$objModule->Newsletter->send($to);
Re: using a frontend module in another module
Same error if no Database_Result given in parameter :
Code:
Recoverable error: Argument 1 passed to Module::__construct() must be an instance of Database_Result, none given, called in C:\wamp\www\typolight-2.8.2\system\libraries\System.php on line 145 and defined in C:\wamp\www\typolight-2.8.2\system\modules\frontend\Module.php on line 71
#0 C:\wamp\www\typolight-2.8.2\system\modules\frontend\Module.php(71): __error(4096, 'Argument 1 pass...', 'C:\wamp\www\typ...', 71, Array)
#1 C:\wamp\www\typolight-2.8.2\system\libraries\System.php(145): Module->__construct()
#2 C:\wamp\www\typolight-2.8.2\system\modules\alumni_job_board\moduleJobApplication.php(802): System->import('moduleBlobUploa...')
#3 C:\wamp\www\typolight-2.8.2\system\modules\alumni_job_board\moduleJobApplication.php(646): moduleJobApplication->compile_upload()
#4 C:\wamp\www\typolight-2.8.2\system\modules\alumni_job_board\moduleJobApplication.php(109): moduleJobApplication->compile_form_job_application()
#5 C:\wamp\www\typolight-2.8.2\system\modules\frontend\Module.php(127): moduleJobApplication->compile()
#6 C:\wamp\www\typolight-2.8.2\system\modules\alumni_job_board\moduleJobApplication.php(93): Module->generate()
#7 C:\wamp\www\typolight-2.8.2\system\modules\frontend\ContentModule.php(70): moduleJobApplication->generate()
#8 C:\wamp\www\typolight-2.8.2\system\libraries\Controller.php(364): ContentModule->generate()
#9 C:\wamp\www\typolight-2.8.2\system\modules\frontend\ModuleArticle.php(170): Controller->getContentElement('106')
#10 C:\wamp\www\typolight-2.8.2\system\modules\frontend\Module.php(127): ModuleArticle->compile()
#11 C:\wamp\www\typolight-2.8.2\system\modules\frontend\ModuleArticle.php(69): Module->generate()
#12 C:\wamp\www\typolight-2.8.2\system\libraries\Controller.php(297): ModuleArticle->generate(false)
#13 C:\wamp\www\typolight-2.8.2\system\libraries\Controller.php(184): Controller->getArticle('37', false, false, 'main')
#14 C:\wamp\www\typolight-2.8.2\system\modules\frontend\PageRegular.php(68): Controller->getFrontendModule('0', 'main')
#15 C:\wamp\www\typolight-2.8.2\index.php(188): PageRegular->generate(Object(DB_Mysql_Result))
#16 C:\wamp\www\typolight-2.8.2\index.php(311): Index->run()
Re: using a frontend module in another module
The thing is that we don't really know how your code looks like and what exactly you're trying to achieve.
Maybe you even don't want it to be a module and you don't have to extend from Module...
Just have a look at http://api.contao.org and check the code.
Otherwise you will most likely always need a Database_Result because you need at least a headline or cssID etc.
Re: using a frontend module in another module
If you base your class on Module:
Code:
class MyModule extends Module
public function generate()
{
...
...
return parent::generate();
}
...
...
protected function compile()
{
...
...
}
...
...
}
...then neither compile() nor generate() require ANY parameters. Otherwise, you have used some other module code or class that we don't know about... You're not giving enough information... post your entire class so we can see that you're doing... At the moment, we're pretty much in the dark...
However a Module uses a constructor which requires the Database Result, of TL querying the tl_module table and limiting the result to the ID of the module, basically (SELECT * FROM tl_module WHERE id=?)
Code:
public function __construct(Database_Result $objModule, $strColumn='main')
{
parent::__construct();
$this->arrData = $objModule->row();
$this->space = deserialize($objModule->space);
$this->cssID = deserialize($objModule->cssID, true);
$arrHeadline = deserialize($objModule->headline);
$this->headline = is_array($arrHeadline) ? $arrHeadline['value'] : $arrHeadline;
$this->hl = is_array($arrHeadline) ? $arrHeadline['unit'] : 'h1';
$this->strColumn = $strColumn;
}
If you wanna make your own class based on nothing, you can just base it on Frontend or Backend, which doesn't require any additional constuctor parameters. This is typically what you do when you want to run an import or cron utility... checkout TL's cron.php file for looking at how leo does this.
Re: using a frontend module in another module
Just wanted to mention that you can find an example usage on my blog - http://blog.qzminski.com/2010/06/using- ... er-module/.
Re: using a frontend module in another module
thanks everybody I just use :
extends Frontend
now I do not have to add a query.
Thanks again!