Path

ez.no / exponential / documentation / development / extensions / module / module tutorial (part 1)


Module tutorial (Part 1)

In this example we make a module named "mymodule".
"mymodule" has 1 view called "list".
We have one template where we use fetch to list all our data from our database table called "mytable".

Folders and files

In your exponential directory there is "extension"-folder (If not, create one).
Inside that folder we make folder for our module "mymodule".
Inside that folder we make folder structure like this:

(/extension/mymodule)/modules/mymodule

(extension/mymodule)/settings

Then we create folder under the "standard" design.
/design/standard/templates/mymodule/
Inside folder: extension/mymodule/modules/mymodule,
we put all the files expect the "list.tpl" and "module.ini.append" -files.
Inside folder: extension/mymodule/settings,
we put the module.ini.append file.
Inside folder: /design/standard/templates/mymodule/,
we will put the list.tpl -file.

1. Our module.ini.append file

[ModuleSettings]

ExtensionRepositories[]=mymodule

2. The database

We create a table named "mytable" to ez database and make 2 cells, "id" and "name".
We add some data to the table:
id name
1 First row
2 Second row

3. Our module.php file

$Module = array( 'name' => 'mymodule' );

 

$ViewList = array();

$ViewList['list'] = array(

'script' => 'list.php',

"unordered_params" => array( "offset" => "Offset" ) );

What happens here is that we make a view "list" for the module,
witch points to file "list.php" and we send parameter "Offset" to it.

4. Our list.php file

$Module =& $Params['Module'];

 

$Offset = $Params['Offset'];

if ( !is_numeric( $Offset ) )

$Offset = 0;

 

$viewParameters = array( 'offset' => $Offset );

 

include_once( 'kernel/common/template.php' );

$tpl =& templateInit();

$tpl->setVariable( 'view_parameters', $viewParameters );

 

$Result = array();

$Result['content'] = $tpl->fetch( "design:mymodule/list.tpl" );

$Result['path'] = array( array( 'url' => false,

                       'text' => 'My Module' ),

                       array( 'url' => false,

                       'text' => 'List' ) );

In here we receive the parameters from module.php and send them forward to list.tpl with template "setVariable" function.
Then we load the list.tpl to screen ($tpl->fetch( "design:mymodule/list.tpl" );
)...

5. Our list.tpl file

{let data_limit=10

data_list=fetch('mymodule','list',hash(offset,$view_parameters.offset,limit,$data_limit))}

All Data

{section name=DATA loop=$data_list sequence=array(bglight,bgdark)}

{$:item.id}{$:item.name}

{/section}

{/let}

Actually we don't need that "data_limit" and "hash(offset...)" thing in our example now,
but it's there just to show how the Offset parameter goes around the files...
Hint: {$:item.xxx} is the name of our database tables cell...

6. Our fetch function

To archieve in this we need 2 files: mymodulefunctioncollection.php and function_definition.php.
First, function_definition.php

$FunctionList = array();

$FunctionList['list'] = array( 'name' => 'list',

                               'operation_types' => array( 'read' ),

                               'call_method' => array( 'include_file' => 

        'extension/mymodule/modules/mymodule/mymodulefunctioncollection.php',

                               'class' => 'MyModuleFunctionCollection',

                               'method' => 'fetchList' ),

                               'parameter_type' => 'standard',

                               'parameters' => array( array( 'name' => 'offset',

                                                             'required' => false,

                                                             'default' => false ),

                                                      array( 'name' => 'limit',

                                                             'required' => false,

                                                             'default' => false ) ) );

Second, mymodulefunctioncollection.php:

include_once( 'extension/mymodule/modules/mymodule/mymodule.php' );

 

class MyModuleFunctionCollection

{

 

function MyModuleFunctionCollection()

{

}

 

function &fetchList( $offset, $limit )

{

   $parameters = array( 'offset' => $offset,

   'limit' => $limit );

   $lista =& Mymodule::fetchListFromDB( $parameters );

 

   return array( 'result' => &$lista );

}

 

}

Third, mymodule.php:
This file is going to keep and handle all of our main functions.

include_once( 'kernel/classes/ezpersistentobject.php' );

 

class Mymodule extends eZPersistentObject

{

 

   function Mymodule( $row )

   {

       $this->eZPersistentObject( $row );

   }

 

   function &definition()

   {

       return array( 'fields' => array(

                   'id' => array(

                   'name' => 'id',

                   'datatype' => 'integer',

                   'default' => 0,

                   'required' => true ),

                   'name' => array(

                   'name' => 'name',

                   'datatype' => 'string',

                   'default' => '',

                   'required' => true ) ),

                   'keys' => array( 'id' ),

                   'increment_key' => 'id',

                   'class_name' => 'mymodule',

                   'name' => 'mytable' );

   }

 

   function &fetchListFromDB( $parameters = array() )

   {

       return Mymodule::handleList( $parameters, false );

   }

 

   function &handleList( $parameters = array(), $asCount = false )

   {

       $parameters = array_merge( array( 'as_object' => true,

       'offset' => false,

       'limit' => false ),

       $parameters );

       $asObject = $parameters['as_object'];

       $offset = $parameters['offset'];

       $limit = $parameters['limit'];

       $limitArray = null;

       if ( !$asCount and $offset !== false and $limit !== false )

       $limitArray = array( 'offset' => $offset,

                           'length' => $limit );

 

       return eZPersistentObject::fetchObjectList( Mymodule::definition(),

                                                   null, null, null, $limitArray,

                                                   $asObject );

   }

 

}

Now we just login to our admin site and goto url: /mymodule/list.
We see our list.tpl showing the 2 variables from the database... (Hopefully)

In part 2 we will add few more views (create/edit) and another fetch function.

Comments

Part 2

Will there be a part 2 of this tutorial?

variable_params in module definition

Multiple modules in one extension

Some information about module policies

The requested page could not be displayed

hi all,i did all you speak about and i go to the adress:
http://127.0.0.1/index.php/cciat_admin/mymodule/list.php
http://127.0.0.1/index.php/cciat_...n/mymodule/modules/mymodule/list.php
but he told me page could not be displayed...
why???
i creat a folder in extension called mymodule an i put insidt it 2 folders:modules and settings,and i put in the modules folder named mymodule and i put in mymodule folder 5 files.
function_definition.php,list.php,module.php,mymodule.php,mymodulefunctioncollection.php.and i put in the designe folder all you said.why i have that problem??and thx all.

Part 2

Does anyone know where Part 2 of this tutorial is? How is a second function called from the template file?

If you have "Undefined module: mymodule" in debug mode....

I use version 3.4.1 (3.4.1) and I got "Undefined module: mymodule" error (in debug mode) however i was able to work it around by setting
ExtensionRepositories[]=mymodule
in /exponential/settings/modules.ini .

In my case, the exponential does not seem to take module.ini.append file into account. Any thoughts ?

Changing your extension or module name without having to change code.

If you want to change your extension or module name afterwards, you also have to change the 'include_file' path in the functionList and operationList arrays in function_definition.php and operation_definition.php.

By using the PHP function get_defined_vars( ) in function_definition.php and operation_definition.php, and printing the resulting array, I found out that you can access the module's path by using $path, and the modules name by using $this->ModuleName in both files.

Example code:

$OperationList['operation']= array
(
'name' => 'operation',
'default_call_method' => array
(
'include_file' => $path . '/' . $this->ModuleName . '/operationcollection.php',
'class' => 'OperationCollection'
),
...

If you code the paths like in this example, you won't have to change any code if you ever want to change the module name or if you put your module in another extension.

mymodule

"mymodule" as a name for the extension, would be better replaced with "myextension", so the difference between the extension and the module inside the extension is more clear.

The module.ini.append file would then look like this:

[ModuleSettings]
ExtensionRepositories[]=myextension

extension templates

To have the templates for your nnew module's views in the same extension,
add a design.ini.append next to your module.ini.append
with the content:

[ExtensionSettings]
DesignExtensions[]=mymodule

Then put the tpl(s) here:
/extension/mymodule/design/standard/templates/mymodule

If module works but no results appear try this:

If you have debug set to enabled you may see something like:
No template could be loaded for "mymodule/list.tpl" using resource "design'

And you are certain that file exists, I would try manually deleting all cache in that design's /var folder and any ini cache and codepage cache. I'm not certain which deletion will get you right, as I did all three and it began to work. At any rate, these symptoms above indicate something was cached that you need renewed.

ExtensionRepositories...

I tried the example and it works great.
I tried many ways to activate this module:

- You may activate it for one site (mysite) and in this case I found that the module is activated either by adding
ExtensionRepositories[]=mymodule
to the [ModuleSettings] section in the file
/settings/siteacces/mysite/module.ini.append

or by adding
ActiveAccessExtensions[]=mymodule
to the [ExtensionSettings] section in the file
/settings/siteacces/mysite/site.ini.append


- You may activate it for all sites (remember to create the table in all databases) and in this case the module is activated either by adding
ActiveExtensions[]=mymodule ("ActiveExtensions" and not "ActiveAccessExtensions")
to the [ExtensionSettings] section in the file
/settings/override/site.ini.append

I hope this will help

ExtensionRepositories...

Maybe coz of version or revision !
am using 3.2 r2881

ExtensionRepositories...

Hmm, I didn't need to do that... But I think it's just that I haven't or have done something else on somewhere... :)

Jerry

Module tutorial (Part 1)

What I found that in order to make the extention work you need also to modify settings/module.ini and make 'ExtensionRepositories' as comment :

#ExtensionRepositories[]

Log in or create a user account to comment.

Contents

Development

Extensions
    Translation
    Introduction to extensions
    Building an Exponential module
    Design extension
    Datatypes
    Workflow events
    Module
       Hello world
       Permissions
       Module tutorial (Part 1)
    Template operator
    Template function
Exponential datamodel
Exponential tuning and stability
Importing attribute data
Kernel
Libraries
Scripting
Standards
System overview
Test Suite
Using Doxygen to create API documenta...


Created

28/07/2003
9:56:37 am
by Jerry Jalava

Last updated

29/07/2003
11:56:36 am
by Jerry Jalava

Authors

Jerry Jalava
liu spider
Kai Duebbert



This page is part of the Exponential documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.