Archived

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

From Joomla! Documentation

< Archived:Creating a simple module
(Add in another example)
(Fix the access check)
(One intermediate revision by the same user not shown)
Line 17: Line 17:
 
<?php
 
<?php
 
// No direct access to this file
 
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
+
defined('_JEXEC') or die;
  
 
/**
 
/**

Revision as of 11:57, 27 September 2014

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.


Introduction[edit]

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:

  • 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]

Writing an extension script consists in declaring an class whose name is mod_ModuleNameInstallerScript with these 5 methods.

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 using $parent->get('manifest')->version. You can also redirect to a page of choice with $parent->getParent()->setRedirectURL('index.php?option=com_modules');.