J3.x

Creating a simple module/Developing a Basic Module/id: Difference between revisions

From Joomla! Documentation

Created page with "Membuat sebuah modul sederhana/Mengembangkan sebuah Modul Dasar"
 
FuzzyBot (talk | contribs)
Updating to match new version of source page
 
(97 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<noinclude><languages /></noinclude>
<noinclude><languages /></noinclude>
{{J3.x:Creating_a_simple_module/en}}
{{J3.x:Creating_a_simple_module/id}}
A module is a lightweight and flexible extension. They are used for small bits of the page that are generally less complex and are able to be seen across different components.
Modul merupakan ekstensi yang ringan dan fleksibel. Digunakan di suatu tempat di halaman yang biasanya tidak rumit dan dapat ditampilkan lintas komponen.


You can see many examples of modules in the standard Joomla! install: - menus - Latest News - Login form - and many more.
Anda dapat melihat ada banyak contoh modul di website standar Joomla: - Menu - Berita Terkini - Formulir login - dan banyak lagi.


This tutorial will explain how to go about creating a simple Hello World module. Through this tutorial you will learn the basic file structure of a module. This basic structure can then be expanded to produce more elaborate modules.
Tutorial ini akan menjelaskan bagaimana cara membuat modul Hello World sederhana. Melalui tutorial ini, anda akan mempelajari struktur file dasar dari sebuah modul. Struktur dasar ini kemudian dapat dikembangkan guna menghasilkan modul-modul yang lebih rumit.


== File Structure ==
== Struktur File ==


There are four basic files that are used in the standard pattern of module development:  
Terdapat empat file dasar yang dipakai dalam pola standar pengembangan modul:  
* <code>mod_helloworld.php</code> - This file is the main entry point for the module. It will perform any necessary initialization routines, call helper routines to collect any necessary data, and include the template which will display the module output.
* <code>mod_helloworld.php</code> - File ini adalah entri poin untuk modul. File ini akan melakukan rutinitas inisiasi yang dibutuhkan, memanggil helper rutin untuk mengumpulkan data yang dibutuhkan, dan menyertakan templat yang akan menampilkan output dari modul tersebut.


* <code>mod_helloworld.xml</code> - This file contains information about the module. It defines the files that need to be installed by the Joomla! installer and specifies configuration parameters for the module.
* <code>mod_helloworld.xml</code> - File ini mengandung informasi-informasi mengenai modul. File ini menentukan file-file apa saja yang perlu dipasang oleh Joomla! dan parameter konfigurasi untuk modul.


* <code>helper.php</code> - This file contains the helper class which is used to do the actual work in retrieving the information to be displayed in the module (usually from the database or some other source).
* <code>helper.php</code> - File ini mengandung kelas helper yang dipakai untuk melakukan tugas-tugas sesungguhnya, mendapatkan informasi yang akan ditampilkan di modul (biasanya berasal dari database atau sumber-sumber lainnya).


* <code>tmpl/default.php</code> - This is the module template. This file will take the data collected by mod_helloworld.php and generate the HTML to be displayed on the page.
* <code>tmpl/default.php</code> - Ini adalah templat dari modul. File ini akan mengambil data-data yang dikumpulkan oleh mod_helloworld.php untuk kemudian membuat tampilan HTML di halaman.


== Creating mod_helloworld.php ==
== Membuat mod_helloworld.php ==


The <tt>mod_helloworld.php</tt> file will perform three tasks:
File <tt>mod_helloworld.php</tt> akan melakukan tiga tugas:
* include the helper.php file which contains the class to be used to collect the necessary data
* menyertakan file helper.php yang mengandung kelas yang akan digunakan untuk mengumpulkan data-data yang dibutuhkan
* invoke the appropriate helper class method to retrieve the data
* meminta metode kelas helper tertentu untuk mendapatkan datanya
* include the template to display the output.
* menyertakan templat untuk menampilkan output


The helper class is defined in our helper.php file. This file is included with a require_once statement:
Kelas helper ditentukan di dalam file helper.php kita. File ini adalah salah satu yang dipanggil dalam deklarasi require_once:
   
   
<source lang="php">require_once dirname(__FILE__) . '/helper.php';</source>
<source lang="php">require_once dirname(__FILE__) . '/helper.php';</source>


<code>require_once</code> is used because our helper functions are defined within a class, and we only want the class defined once.
<code>require_once</code> digunakan karena fungsi helper kita ditentukan dalam sebuah kelas, dan kita hanya menginginkannya satu kali.


Our helper class has not been defined yet, but when it is, it will contain one method: getHello(). For our basic example, it is not really necessary to do this - the “Hello, World” message that this method returns could simply be included in the template. We use a helper class here to demonstrate this basic technique.
Kelas helper kita belum ditentukan, tetapi kalau sudah, ia akan mengandung satu metode: getHello(). Untuk contoh dasar kali ini, kita tidak perlu melakukan hal ini—pesan "Hello, World" yang dikembalikan oleh metode ini dapat dimasukkan ke dalam templat. Kita menggunakan kelas helper di sini hanya bertujuan menunjukkan teknik dasar.


Our module currently does not use any parameters, but we will pass them to the helper method anyway so that it can be used later if we decide to expand the functionality of our module.
Modul kita saat ini tidak menggunakan parameter apa pun, namun demikian kita akan mengirimkannya ke metode helper sehingga ia dapat dipakai nanti ketika kita memutuskan untuk mengembangkan fungsi dari modul.


The helper class method is invoked in the following way:
Metode kelas helper dibuat dengan cara berikut ini:
   
   
<source lang="php">$hello = modHelloWorldHelper::getHello($params);</source>
<source lang="php">$hello = modHelloWorldHelper::getHello($params);</source>


=== Completed mod_helloworld.php file ===
=== File mod_helloworld.php selengkapnya ===


The complete <tt>mod_helloworld.php</tt> file is as follows:
File <tt>mod_helloworld.php</tt> selengkapnya adalah sebagai berikut:
   
   
<source lang="php"><?php
<source lang="php"><?php
Line 64: Line 64:
$hello = modHelloWorldHelper::getHello($params);
$hello = modHelloWorldHelper::getHello($params);
require JModuleHelper::getLayoutPath('mod_helloworld');
require JModuleHelper::getLayoutPath('mod_helloworld');
</source>
</source>


The one line that we haven’t explained so far is the first line. This line checks to make sure that this file is being included from the Joomla! application. This is necessary to prevent variable injection and other potential security concerns.
Satu baris yang belum dijelaskan sejauh ini adalah <tt>defined('_JEXEC') or die;</tt>. Baris ini penting guna memastikan bahwa file ini dipanggil dari aplikasi Joomla. Ini dibutuhkan untuk mencegah injeksi variabel dan kerentanan keamanan lainnya.


== Creating helper.php ==
== Membuat helper.php ==


The <tt>helper.php</tt> file contains that helper class that is used to retrieve the data to be displayed in the module output. As stated earlier, our helper class will have one method: getHello(). This method will return the ‘Hello, World’ message.
File <tt>helper.php</tt> mengandung kelas helper yang digunakan untuk mendapatkan data yang akan ditampilkan di output modul tersebut. Seperti yang sudah disebutkan sebelumnya, di sini kelas helper kita hanya akan memiliki satu metode: getHello(). Metode ini akan mengembalikan pesan 'Hello, World'.


Here is the code for the helper.php file:
Berikut adalah kode untuk file helper.php:
   
   
<source lang="php"><?php
<source lang="php"><?php
Line 102: Line 101:
     }
     }
}
}
</source>
</source>


There is no rule stating that we must name our helper class as we have, but it is helpful to do this so that it is easily identifiable and locatable. Note that it is required to be in this format if you plan to use the com_ajax plugin.
Tidak ada aturan khusus yang mengharuskan untuk mengubah nama kelas helper, tetapi akan sangat membantu agar dapat dikenali dan dicari dengan mudah. Mohon dicatat bahwa penting ditulis dalam format seperti ini jika anda berencana untuk menggunakan komponen com_ajax.


More advanced modules might include database requests or other functionality in the helper class method.
Modul yang lebih canggih lainnya bisa saja menyertakan permintaan database atau fungsi-fungsi lain di dalam metode kelas helper.


== Creating tmpl/default.php ==
== Membuat tmpl/default.php ==


The <tt>default.php</tt> file is the template which displays the module output.
File <tt>default.php</tt> adalah templat yang menampilkan output modul.


The code for the default.php file is as follows:
Kode untuk file default.php adalah sebagai berikut:
   
   
<source lang="php">
<source lang="php">
Line 122: Line 120:
</source>
</source>


An important point to note is that the template file has the same scope as the mod_helloworld.php file. What this means is that the variable $hello can be defined in the mod_helloworld.php file and then used in the template file without any extra declarations or function calls.
Sebuah catatan penting yang harus diperhatikan adalah file templat ini memiliki cakupan yang sama dengan file mod_helloworld.php. Ini artinya variabel $hello dapat ditentukan di dalam file mod_helloworld.php untuk kemudian dipakai di file templat tanpa deklarasi tambahan atau pemanggilan fungsi lainnya.


== Creating mod_helloworld.xml ==
== Membuat mod_helloworld.xml ==


The <tt>mod_helloworld.xml</tt> is used to specify which files the installer needs to copy and is used by the Module Manager to determine which parameters are used to configure the module. Other information about the module is also specified in this file.
File <tt>mod_helloworld.xml</tt> digunakan untuk menentukan file-file apa saja yang perlu disalin oleh pemasang Joomla dan digunakan oleh Pengelolaan Modul untuk menentukan parameter yang dipakai dalam mengonfigurasikan modul tersebut. Informasi lain mengenai modul juga ditentukan di dalam file ini.


The code for <tt>mod_helloworld.xml</tt> is as follows:
Kode untuk <tt>mod_helloworld.xml</tt> adalah sebagai berikut:
   
   
<source lang="xml">
<source lang="xml">
Line 150: Line 148:
</source>
</source>


[[S:MyLanguage/Manifest files|Manifest files]] explains the technical details of the elements used in the XML file.
[[S:MyLanguage/Manifest files/id|File manifes]] menjelaskan detail teknis elemen-elemen apa saja yang dipakai di file XML.


You will notice that there are two additional files that we have not yet mentioned: index.html and tmpl/index.html. These files are included so that these directories cannot be browsed. If a user attempts to point their browser to these folders, the index.html file will be displayed. These files can be left empty or can contain the simple line:
Anda akan melihat bahwa ada dua file tambahan lain yang belum disebutkan: index.html dan tmp/index.html. File-file ini disertakan agar direktori ini tidak dapat ditelusuri oleh browser. Jika ada seseorang yang mencoba untuk mengarahkan browser ke folder ini, file index.html yang akan ditampilkan. File-file ini boleh dibiarkan kosong atau mengandung baris sederhana seperti ini:
   
   
<source lang="html4strict"><html><body bgcolor="#FFFFFF"></body></html></source>
<source lang="html4strict"><html><body bgcolor="#FFFFFF"></body></html></source>


which will display an empty page.
yang akan menampilkan halaman kosong.


Since our module does not use any [[S:MyLanguage/Form_field|form fields]], the config section is empty.
Karena modul kita tidak menggunakan [[S:MyLanguage/Form_field|bidang formulir]] apa pun, bagian konfigurasinya kosong.


== Conclusion ==
== Installing and Viewing the Module ==
To install the module, zip up the parent directory of your source files, to create a file called <tt>mod_helloworld.zip</tt>. Note that it needs to be a .zip file because the server needs to be able to extract it using Apache's mod_zlib library.
Go into Joomla admin, to the Extensions / Manage / Install form, click on the Update Package File tab, and upload your zip file. You should find that the module installs ok. If it doesn't, then compare carefully your code with the code above, and ensure your source code files are named correctly.
To make the module visible on your site web pages go into the Extensions / Modules listing and click on the Hello World module to edit it.
# set the Status to Published
# select the Position where the module should appear (if you're unsure about which Position to set, then navigate to Extensions / Templates / Styles, check which template is assigned on your web pages, then follow [[S:MyLanguage/Finding module positions on any given page|this guide]] to finding the module positions).
# on the Menu Assignment tab set the Module Assignment to "On all pages"
Save and Close your edit form.
Now if you navigate to your site you should see "Hello, World!" displayed in your chosen module position.


Module development for Joomla! is a fairly simple, straightforward process. Using the techniques described in this tutorial, an endless variety of modules can be developed with little hassle.
== Kesimpulan ==
Pengembangan modul Joomla! cukup sederhana, proses yang mudah. Melalui teknik-teknik yang dijelaskan di tutorial ini, varietas modul tanpa batas dapat dikembangkan dengan sedikit berusaha.


<div class="row">  
<div class="row">  
<div class="large-6 columns">{{Basic button|S:MyLanguage/J3.x:Creating_a_simple_module/Introduction|Prev: Introduction|class=expand success}}</div>
<div class="large-6 columns">{{Basic button|S:MyLanguage/J3.x:Creating_a_simple_module/Introduction|Sebelumnya: Pendahuluan|class=expand success}}</div>
<div class="large-6 columns">{{Basic button|S:MyLanguage/J3.x:Creating_a_simple_module/Using_the_Database|Next: Using the Database|class=expand}}</div>
<div class="large-6 columns">{{Basic button|S:MyLanguage/J3.x:Creating_a_simple_module/Using_the_Database|Berikutnya: Menggunakan Database|class=expand}}</div>
</div>
</div>
__NOTOC__
__NOTOC__
<noinclude>
<noinclude>
[[Category:Tutorials]]
[[Category:Tutorials/id|Tutorial]]
[[Category:Tutorials in a Series]]
[[Category:Tutorials in a Series/id|Tutorial dalam Seri]]
[[Category:Module Development]]
[[Category:Module Development/id|Pengembangan Modul]]
[[Category:Beginner Development]]
[[Category:Beginner Development/id|Pengembangan Pemula]]
[[Category:Joomla! 3.x]]
[[Category:Joomla! 3.x/id|Joomla! 3.x]]
[[Category:Joomla! 3.0]]
[[Category:Joomla! 3.0/id|Joomla! 3.0]]
[[Category:Joomla! 3.1]]
[[Category:Joomla! 3.1/id|Joomla! 3.1]]
[[Category:Joomla! 3.2]]
[[Category:Joomla! 3.2/id|Joomla! 3.2]]
[[Category:Joomla! 3.3]]
[[Category:Joomla! 3.3/id|Joomla! 3.3]]
[[Category:Joomla! 3.4]]
[[Category:Joomla! 3.4/id|Joomla! 3.4]]
</noinclude>
</noinclude>

Latest revision as of 22:52, 21 July 2020

Joomla! 
3.x
Tutorial
Membuat modul sederhana

Ini adalah seri artikel mengenai cara membuat modul untuk Joomla! Versi Joomla 3.x. Anda dapat menelusuri artikel-artikel yang terdapat di dalam seri ini melalui menu dropdown.

Mulai dari Pendahuluan, lalu telusuri artikel-artikel di dalam seri ini melalui tombol navigasi di bagian bawah atau dari kotak di samping (Artikel dalam seri ini)




Modul merupakan ekstensi yang ringan dan fleksibel. Digunakan di suatu tempat di halaman yang biasanya tidak rumit dan dapat ditampilkan lintas komponen.

Anda dapat melihat ada banyak contoh modul di website standar Joomla: - Menu - Berita Terkini - Formulir login - dan banyak lagi.

Tutorial ini akan menjelaskan bagaimana cara membuat modul Hello World sederhana. Melalui tutorial ini, anda akan mempelajari struktur file dasar dari sebuah modul. Struktur dasar ini kemudian dapat dikembangkan guna menghasilkan modul-modul yang lebih rumit.

Struktur File

Terdapat empat file dasar yang dipakai dalam pola standar pengembangan modul:

  • mod_helloworld.php - File ini adalah entri poin untuk modul. File ini akan melakukan rutinitas inisiasi yang dibutuhkan, memanggil helper rutin untuk mengumpulkan data yang dibutuhkan, dan menyertakan templat yang akan menampilkan output dari modul tersebut.
  • mod_helloworld.xml - File ini mengandung informasi-informasi mengenai modul. File ini menentukan file-file apa saja yang perlu dipasang oleh Joomla! dan parameter konfigurasi untuk modul.
  • helper.php - File ini mengandung kelas helper yang dipakai untuk melakukan tugas-tugas sesungguhnya, mendapatkan informasi yang akan ditampilkan di modul (biasanya berasal dari database atau sumber-sumber lainnya).
  • tmpl/default.php - Ini adalah templat dari modul. File ini akan mengambil data-data yang dikumpulkan oleh mod_helloworld.php untuk kemudian membuat tampilan HTML di halaman.

Membuat mod_helloworld.php

File mod_helloworld.php akan melakukan tiga tugas:

  • menyertakan file helper.php yang mengandung kelas yang akan digunakan untuk mengumpulkan data-data yang dibutuhkan
  • meminta metode kelas helper tertentu untuk mendapatkan datanya
  • menyertakan templat untuk menampilkan output

Kelas helper ditentukan di dalam file helper.php kita. File ini adalah salah satu yang dipanggil dalam deklarasi require_once:

require_once dirname(__FILE__) . '/helper.php';

require_once digunakan karena fungsi helper kita ditentukan dalam sebuah kelas, dan kita hanya menginginkannya satu kali.

Kelas helper kita belum ditentukan, tetapi kalau sudah, ia akan mengandung satu metode: getHello(). Untuk contoh dasar kali ini, kita tidak perlu melakukan hal ini—pesan "Hello, World" yang dikembalikan oleh metode ini dapat dimasukkan ke dalam templat. Kita menggunakan kelas helper di sini hanya bertujuan menunjukkan teknik dasar.

Modul kita saat ini tidak menggunakan parameter apa pun, namun demikian kita akan mengirimkannya ke metode helper sehingga ia dapat dipakai nanti ketika kita memutuskan untuk mengembangkan fungsi dari modul.

Metode kelas helper dibuat dengan cara berikut ini:

$hello = modHelloWorldHelper::getHello($params);

File mod_helloworld.php selengkapnya

File mod_helloworld.php selengkapnya adalah sebagai berikut:

<?php
/**
 * Hello World! Module Entry Point
 * 
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @license    GNU/GPL, see LICENSE.php
 * @link       http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

// No direct access
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';

$hello = modHelloWorldHelper::getHello($params);
require JModuleHelper::getLayoutPath('mod_helloworld');

Satu baris yang belum dijelaskan sejauh ini adalah defined('_JEXEC') or die;. Baris ini penting guna memastikan bahwa file ini dipanggil dari aplikasi Joomla. Ini dibutuhkan untuk mencegah injeksi variabel dan kerentanan keamanan lainnya.

Membuat helper.php

File helper.php mengandung kelas helper yang digunakan untuk mendapatkan data yang akan ditampilkan di output modul tersebut. Seperti yang sudah disebutkan sebelumnya, di sini kelas helper kita hanya akan memiliki satu metode: getHello(). Metode ini akan mengembalikan pesan 'Hello, World'.

Berikut adalah kode untuk file helper.php:

<?php
/**
 * Helper class for Hello World! module
 * 
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
class ModHelloWorldHelper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */    
    public static function getHello($params)
    {
        return 'Hello, World!';
    }
}

Tidak ada aturan khusus yang mengharuskan untuk mengubah nama kelas helper, tetapi akan sangat membantu agar dapat dikenali dan dicari dengan mudah. Mohon dicatat bahwa penting ditulis dalam format seperti ini jika anda berencana untuk menggunakan komponen com_ajax.

Modul yang lebih canggih lainnya bisa saja menyertakan permintaan database atau fungsi-fungsi lain di dalam metode kelas helper.

Membuat tmpl/default.php

File default.php adalah templat yang menampilkan output modul.

Kode untuk file default.php adalah sebagai berikut:

<?php 
// No direct access
defined('_JEXEC') or die; ?>
<?php echo $hello; ?>

Sebuah catatan penting yang harus diperhatikan adalah file templat ini memiliki cakupan yang sama dengan file mod_helloworld.php. Ini artinya variabel $hello dapat ditentukan di dalam file mod_helloworld.php untuk kemudian dipakai di file templat tanpa deklarasi tambahan atau pemanggilan fungsi lainnya.

Membuat mod_helloworld.xml

File mod_helloworld.xml digunakan untuk menentukan file-file apa saja yang perlu disalin oleh pemasang Joomla dan digunakan oleh Pengelolaan Modul untuk menentukan parameter yang dipakai dalam mengonfigurasikan modul tersebut. Informasi lain mengenai modul juga ditentukan di dalam file ini.

Kode untuk mod_helloworld.xml adalah sebagai berikut:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>Hello, World!</name>
    <author>John Doe</author>
    <version>1.0.0</version>
    <description>A simple Hello, World! module.</description>
    <files>
        <filename>mod_helloworld.xml</filename>
        <filename module="mod_helloworld">mod_helloworld.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <config>
    </config>
</extension>

File manifes menjelaskan detail teknis elemen-elemen apa saja yang dipakai di file XML.

Anda akan melihat bahwa ada dua file tambahan lain yang belum disebutkan: index.html dan tmp/index.html. File-file ini disertakan agar direktori ini tidak dapat ditelusuri oleh browser. Jika ada seseorang yang mencoba untuk mengarahkan browser ke folder ini, file index.html yang akan ditampilkan. File-file ini boleh dibiarkan kosong atau mengandung baris sederhana seperti ini:

<html><body bgcolor="#FFFFFF"></body></html>

yang akan menampilkan halaman kosong.

Karena modul kita tidak menggunakan bidang formulir apa pun, bagian konfigurasinya kosong.

Installing and Viewing the Module

To install the module, zip up the parent directory of your source files, to create a file called mod_helloworld.zip. Note that it needs to be a .zip file because the server needs to be able to extract it using Apache's mod_zlib library. Go into Joomla admin, to the Extensions / Manage / Install form, click on the Update Package File tab, and upload your zip file. You should find that the module installs ok. If it doesn't, then compare carefully your code with the code above, and ensure your source code files are named correctly. To make the module visible on your site web pages go into the Extensions / Modules listing and click on the Hello World module to edit it.

  1. set the Status to Published
  2. select the Position where the module should appear (if you're unsure about which Position to set, then navigate to Extensions / Templates / Styles, check which template is assigned on your web pages, then follow this guide to finding the module positions).
  3. on the Menu Assignment tab set the Module Assignment to "On all pages"

Save and Close your edit form. Now if you navigate to your site you should see "Hello, World!" displayed in your chosen module position.

Kesimpulan

Pengembangan modul Joomla! cukup sederhana, proses yang mudah. Melalui teknik-teknik yang dijelaskan di tutorial ini, varietas modul tanpa batas dapat dikembangkan dengan sedikit berusaha.