J3.x

Difference between revisions of "Creating a simple module/Adding an install-uninstall-update script file"

From Joomla! Documentation

< J3.x:Creating a simple module
(Start page)
 
m (Some punctuation and grammar corrections.)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Chunk30:Creating a Simple Module for Joomla!3.x - Contents}}
+
<noinclude><languages /></noinclude>
 
+
{{J3.x:Creating_a_simple_module/<translate><!--T:1-->
== Introduction ==
+
en</translate>}}
Installing, updating and uninstalling a module may require additional operations that cannot be achieved by the basic operations described in the main xml file. Joomla offers a new approach to solve this problem. It consists in using a php script file containing a class using five methods:
+
<translate>== Introduction == <!--T:2--></translate>
 +
<translate><!--T:3-->
 +
Installing, updating and uninstalling a module may require additional operations that cannot be achieved by the basic operations described in the ''.xml'' file. Joomla offers a new approach to solve this problem. It consists in using a PHP script file containing a class using five functions:</translate>
 +
<translate><!--T:4-->
 
* preflight which is executed before install and update
 
* preflight which is executed before install and update
 
* install
 
* install
 
* update
 
* update
 
* uninstall
 
* uninstall
* postflight which is executed after install and update
+
* postflight which is executed after install and update</translate>
  
== Creating the extension script file ==
+
<translate>== Creating the Extension Script File == <!--T:5-->
Writing an extension script consists in declaring an class whose name is ''mod_'''ModuleName'''InstallerScript'' with these 5 methods.
+
Write an extension script by declaring a class whose name is ''mod_'''ModuleName'''InstallerScript'' with these five functions.</translate>
  
 
<span id="script.php">
 
<span id="script.php">
''script.php''
+
<tt>''script.php''</tt>
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 
// No direct access to this file
 
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
+
defined('_JEXEC') or die;
  
 
/**
 
/**
Line 32: Line 35:
 
function install($parent)  
 
function install($parent)  
 
{
 
{
echo '<p>The module has been installed</p>';
+
echo '<p>The module has been installed.</p>';
 
}
 
}
  
Line 43: Line 46:
 
function uninstall($parent)  
 
function uninstall($parent)  
 
{
 
{
echo '<p>The module has been uninstalled</p>';
+
echo '<p>The module has been uninstalled.</p>';
 
}
 
}
  
Line 54: Line 57:
 
function update($parent)  
 
function update($parent)  
 
{
 
{
echo '<p>The module has been updated to version' . $parent->get('manifest')->version) . '</p>';
+
echo '<p>The module has been updated to version' . $parent->get('manifest')->version . '.</p>';
 
}
 
}
  
Line 66: Line 69:
 
function preflight($type, $parent)  
 
function preflight($type, $parent)  
 
{
 
{
echo '<p>Anything here happens before the installation/update/uninstallation of the module</p>';
+
echo '<p>Anything here happens before the installation/update/uninstallation of the module.</p>';
 
}
 
}
  
Line 78: Line 81:
 
function postflight($type, $parent)  
 
function postflight($type, $parent)  
 
{
 
{
echo '<p>Anything here happens after the installation/update/uninstallation of the module</p>';
+
echo '<p>Anything here happens after the installation/update/uninstallation of the module.</p>';
 
}
 
}
 
}
 
}
Line 84: Line 87:
 
</span>
 
</span>
  
In the update method we show the new version using <code>$parent->get('manifest')->version</code>. You can also redirect to a page of choice with <code>$parent->getParent()->setRedirectURL('index.php?option=com_modules');</code>.
+
<translate><!--T:6-->
 +
In the update method we show the new version number using <code>$parent->get('manifest')->version</code>. You can also redirect to a page of your choice with <code>$parent->getParent()->setRedirectURL('index.php?option=com_modules');</code>.</translate>
 +
 
 +
<translate><!--T:7-->
 +
Remember to add a call to your ''script.php'' in the module's ''.xml'' file:</translate>
 +
<code>
 +
<scriptfile>script.php</scriptfile>
 +
</code>
  
 +
<div class="row">
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:8-->
 +
S:MyLanguage/J3.x:Creating_a_simple_module/Adding_Auto_Update|Prev: Adding Auto Update</translate>|class=expand success}}</div>
 +
</div>
 +
__NOTOC__
 
<noinclude>
 
<noinclude>
 +
<translate>
 +
<!--T:9-->
 +
[[Category:Tutorials]]
 +
[[Category:Tutorials in a Series]]
 
[[Category:Module Development]]
 
[[Category:Module Development]]
 +
[[Category:Beginner Development]]
 +
[[Category:Joomla! 3.x]]
 +
[[Category:Joomla! 3.0]]
 +
[[Category:Joomla! 3.1]]
 +
[[Category:Joomla! 3.2]]
 +
[[Category:Joomla! 3.3]]
 +
[[Category:Joomla! 3.4]]
 +
</translate>
 
</noinclude>
 
</noinclude>

Latest revision as of 10:18, 27 June 2019

Other languages:
Bahasa Indonesia • ‎English • ‎Nederlands • ‎español • ‎français • ‎português • ‎中文(台灣)‎
Joomla! 
3.x
Tutorial
Creating a simple module

This is a multiple article series on how to create a module for Joomla! Version Joomla 3.x. You can navigate the articles in this series by using the navigation drop down menu.

Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (Articles in this series). There are 2 videos accompanying this tutorial which you can view at Basic Joomla Module Development video 1 and Basic Joomla Module Development video 2.




Introduction[edit]

Installing, updating and uninstalling a module may require additional operations that cannot be achieved by the basic operations described in the .xml file. Joomla offers a new approach to solve this problem. It consists in using a PHP script file containing a class using five functions:

  • preflight which is executed before install and update
  • install
  • update
  • uninstall
  • postflight which is executed after install and update

Creating the Extension Script File[edit]

Write an extension script by declaring a class whose name is mod_ModuleNameInstallerScript with these five functions.

script.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

/**
 * Script file of HelloWorld module
 */
class mod_helloWorldInstallerScript
{
	/**
	 * Method to install the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function install($parent) 
	{
		echo '<p>The module has been installed.</p>';
	}

	/**
	 * Method to uninstall the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function uninstall($parent) 
	{
		echo '<p>The module has been uninstalled.</p>';
	}

	/**
	 * Method to update the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function update($parent) 
	{
		echo '<p>The module has been updated to version' . $parent->get('manifest')->version . '.</p>';
	}

	/**
	 * Method to run before an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function preflight($type, $parent) 
	{
		echo '<p>Anything here happens before the installation/update/uninstallation of the module.</p>';
	}

	/**
	 * Method to run after an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function postflight($type, $parent) 
	{
		echo '<p>Anything here happens after the installation/update/uninstallation of the module.</p>';
	}
}

In the update method we show the new version number using $parent->get('manifest')->version. You can also redirect to a page of your choice with $parent->getParent()->setRedirectURL('index.php?option=com_modules');.

Remember to add a call to your script.php in the module's .xml file: <scriptfile>script.php</scriptfile>