How to create a folder in the media manager upon installing a module - joomla

Hey I am making my first module in Joomla!.
I want to create a new folder in Joomla!'s media manager when someone installs my module. I have been duck-duck-go-ing around and found some information. But I just can't get it to work. Probably because the information might be for older Joomla! versions. Or I might've implemented it wrong.
Anyway I came up with the following:
mod_mymodule.xml
<extension type="module" version="3.8.1" client="site" method="upgrade">
<files>
<scriptfile>install.componentname.php</scriptfile>
</files>
</extension>
install.createmap.php
<?php
$destination = JPATH_SITE."/images/mynewmap";
JFolder::create($destination);
?>
Can anyone explain me how to add a folder to the media manager (root/images) on installing a module in Joomla 3?

You need a script.php file which executes your install tasks:
https://docs.joomla.org/J3.x:Creating_a_simple_module/Adding_an_install-uninstall-update_script_file
Here is the example content the script.php:
<?php
// No direct access to this file
defined('_JEXEC') or die;
class mod_helloWorldInstallerScript
{
function install($parent)
{
echo '<p>The module has been installed</p>';
}
function uninstall($parent)
{
echo '<p>The module has been uninstalled</p>';
}
function update($parent)
{
echo '<p>The module has been updated to version' . $parent->get('manifest')->version . '</p>';
}
function preflight($type, $parent)
{
echo '<p>Anything here happens before the installation/update/uninstallation of the module</p>';
}
function postflight($type, $parent)
{
echo '<p>Anything here happens after the installation/update/uninstallation of the module</p>';
}
}
It contains a class with some methods so you can create the necessary folder structure. You want to extend the postflight method.

Related

joomla 3+ get parameters from module and output them

I have tried a few ways to do this but can't get any thing to work. My issue is that I want to pass a parameter from my module to a variable and output it in my html, through the default.php file.
Here is my config in the xml:
<config>
<fields name="params">
<fieldset name="basic">
<field name="prod_price"
type="text"
size="50"
label="Product Price"
description="Enter the product price" />
</fieldset>
</fields>
</config>
I think this is okay as it appears in the back-end.
My mod_helloworld.php file is simple and include a helper.php require, see below:
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';
$hello = modHelloWorldHelper::getHello($params);
$mydata = modHelloWorldHelper::getMydata($params);
require JModuleHelper::getLayoutPath('mod_helloworld');
$document = JFactory::getDocument();
$document->addStyleSheet('modules/mod_helloworld/css/style.css');
$document->addScript('modules/mod_helloworld/js/test.js');
I think this is okay, the variables are all returning values.
My helper.php file is as below:
class ModHelloWorldHelper
{
public static function getHello($params)
{
return 'Hello, World!';
}
public static function getMydata($params)
{
return 'Hello, World! ';
}
}
I then echo these variable in my default.php and they output fine. What I think I need to do is to get the $params variable somehow and reference the prod_price but I can't seem to do it. I am a beginner with creating modules so excuse any obvious errors. If the getMyData function could collect the params then I could perhaps output them but I am not sure where to put code?
Kind regards,
James
suppose http://examples.com/index.php?prod_price=20
You can receive that data in your module via
$price = JFactory::getApplication()->input->get('prod_price',0);
it will receive post or get request.

Custom HTML in Joomla Template Manager

While developing a Joomla Template, i got stuck on this question.
Every Joomla Module has a manifest. In that manifest you can set your Paramaters for your template with Field and Fieldset types. Is it possible to add a custom image in these sections? I tried CDATA for this but it doesn't work.
I've never tried this with a template, only a plugin and module, but I believe it's still the same concept.
Create a new folder in your template folder and name it "elements".
Then, in your template.xml file, find the <fieldset> that the parameter belongs to and add the following inside the tag:
addfieldpath="/modules/mod_social_slider/fields"
You now need to add a new parameter field like so:
<field name="image" type="custom" default="" label="" description="" />
Now, create a new PHP and place it inside your newly created "elements" folder and call it "custom.php". Then add the following code:
<?php
defined('_JEXEC') or die('Restricted access');
class JFormFieldCustom extends JFormField {
protected $type = 'Custom';
protected function getInput() {
return '<img src="' . JUri::root() . 'templates/your_template/images/image.jpg" alt="" />';
}
}
?>
Note that I haven't tested this so let me know if it works or not

embeding a joomla module in a component

I want to show a module in a component in joomla-2.5.
I tried below code but it is not working.
jimport('joomla.application.module.helper');
$modules = JModuleHelper::getModules('latest albums');
foreach($modules as $module)
{
echo JModuleHelper::renderModule($module);
}
'latest albums' is the title of my module.
Also I used mod_name alone and also (mod_name,mod title) together but they didn't work.
How can I resolve it.
Thanks

First component creation based on HelloWorld failed

I'm trying to create my first component for Joomla 2.5 but when try to execute get this error:
Error: 500
You may not be able to visit this page because of:
an out-of-date bookmark/favourite
a search engine that has an out-of-date listing for this site
a mistyped address
you have no access to this page
The requested resource was not found.
An error has occurred while processing your request.
View not found [name, type, prefix]: transportation, html, transportationView
What I've developed now is very basic and this is the controller under site/components/com_transportation/controllers/controller.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
class TransportationController extends JController {
}
And under site/components/com_transportation/views/view.html.php this:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
class TransportationViewTransportation extends JView {
// Overwriting JView display method
function display($tpl = null) {
// Assign data to the view
$this->msg = 'Hello World';
// Display the view
parent::display($tpl);
}
}
What I'm missing? What is wrong?
Your folder structure is incorrect. Your view file must be in site/components/com_transportation/views/transportation/view.html.php
Try this ,
When you start developing a new component go through the tutorial deeply,Then start modifying the samples .
follow this url it will help you .Its for 1.5 but the things are same for 2.5.
Only you have to mention version in the xml
<install type="component" version="1.5.0">
Also you will get a sample component download from this.
Download it and compare with your component then find the issue.
Hope this may helps..
View not found [name, type, prefix]: transportation, html, transportationView
Means just tha no view was found with the class name of transporationViewtransporation and the type view.html.php. What is the name of the class in your view.html.php file? is the second transportation really lower case like that? Also what are the name(s) of your layout and xml files in the tmpl folder?

can I use joomla's onAfterRender for a module rather than a plugin?

I want to insert some code to Joomla when any page is loaded.
For this I created a module that inserts code.
I am trying to use
<?php
// $Id: helper.php
defined('_JEXEC') or die;
jimport( 'joomla.plugin.plugin' );
jimport( 'joomla.environment.response' );
class modInsertCode
{
function onAfterRender($params)
{
$code = 'some code';
$documentbody = JResponse::getBody();
$documentbody = str_replace ("</body>", $code." </body>", $documentbody);
JResponse::setBody($documentbody);
return true;
}
}
?>
but JResponse::getBody(); returns an empty string. Any ideas, solutions of fixes to this code?
Thank you,
You have to do it using a plugin, you won't be able to do it using a module because the HTML response has not been generated by the time the code of the module gets executed.
I hope it helped!
I know this is a bit old but for future reference this can be done with jQuery:
$doc = JFactory::getDocument();
$js = 'jQuery(document).ready( function() {
jQuery("#module'.$module->id.'").appendTo(document.body);
})';
$doc->addScriptDeclaration($js);
This is assuming that you have wrapped the content in you module in something like the following, including the module id to support multiple instances of the module.
<div id="module<?php echo $module->id; ?>"> Your content </div>

Resources