Gallery3:Developer Handbook:Gallery3 Modules - Gallery Codex
Personal tools

Gallery3:Developer Handbook:Gallery3 Modules

From Gallery Codex

Gallery3 Modules

A Gallery 3 module is a collection of models, views, controllers, helpers, libraries and config files gathered in a single directory located under gallery3/modules. All of those elements are optional and are used to add specific features and capabilities to Gallery 3. There is only one mandatory file in a Gallery 3 module and that is the module.info file that contains the name, description and version of the module.

Basic Module Layout

The simplest module looks like this:

 hello_world
 └── module.info

Here's what the module.info file could contain:

 name = "Hello world!"
 description = "This module does nothing!"
 version = 1

This module can be activated and deactivated in the Admin > Modules interface, but won't actually do anything. To add functionality to the module, you can add controllers, event hooks, rest hooks, search hooks, theme hooks, RSS hooks, task hooks, block hooks, an installer and more. Each of these bits of functionality are easy to add and mostly independent of each other so it's simple to extend Gallery 3 in many directions without writing much code.

Gallery 3 module hooks are very simple. Each hook system expects there to be a specially named helper with specially named functions in them. Once you understand the pattern it's simple and easy to add a new hook with just a few lines of code.

For example, if you'd like to print out the words Hello world at the top of every album, you would need to add a theme hook to your module. In the helpers directory, you'd create a file called hello_world_theme.php containing the following code:

<?
class hello_world_theme {
  static function album_top($theme) {
    return "Hi";
  }
}

A few things to note about this:

  • Gallery 3 expects that modules that implement a theme hook have a PHP file located at modules/<module_name>/helpers/<module_name>_theme.php containing a helper class called <module_name>_theme.
  • Whenever Gallery 3 shows an album page, it tries to call the hello_world_theme::album_top function at the top of the page, and hello_world_theme::album_bottom at the bottom.

There are many other theme hooks, this is just a simple example. But with these two files you can affect many useful changes in Gallery 3.

A more complex Gallery 3 module might look like this:

modules
└── search
    ├── controllers
    │   └── search.php
    ├── helpers
    │   ├── search_event.php
    │   ├── search_installer.php
    │   ├── search.php
    │   ├── search_task.php
    │   └── search_theme.php
    ├── models
    │   └── search_record.php
    ├── module.info
    └── views
        ├── search.html.php
        └── search_link.html.php

The search module has 1 controller, accessible at the /search url. It has an event hook at search_event.php, and installer at search_installer.php, an admin task at search_task.php and a theme hook at search_theme.php. It implements a single model in search_record.php and has two views.

Module Lifecycle

A module can be installed, activated and deactivated. In Gallery 3.0, a module cannot be uninstalled, but there are plans to implement that in Gallery 3.1 and beyond.

At install time, the module is responsible for creating and populating any necessary database tables, registering graphics rules and setting module variables. Activation and deactivation of a module is handled by the Gallery 3 framework. In the future when uninstalling is supported, the module can remove any database tables that it created.

Module Hooks

The following hooks are available for modules to implement. Any module can introduce its own hooks, but the ones listed below are the ones that are supported by the official Gallery 3 modules.

Event

These are general purpose hooks that are used in a variety of places throughout the system to indicate that an activity of some kind has happened, is about to happen, or has completed. Any module can initiate an event, and any module can handle an event. Multiple modules can handle a single event, it's up to the individual modules to make sure that they don't interfere with each other. The following events are triggered by the official Gallery 3 modules:

An attempt to detail all of the events currently defined in Gallery3 can be found on the Event Hooks page.

  • add_photos_form
  • add_photos_form_completed
  • admin_menu
  • album_add_form
  • album_add_form_completed
  • album_menu
  • batch_complete
  • captcha_protect_form
  • comment_add_form
  • comment_created
  • comment_updated
  • context_menu
  • gallery_ready
  • gallery_shutdown
  • graphics_composite
  • graphics_composite_completed
  • graphics_resize
  • graphics_resize_completed
  • graphics_rotate
  • graphics_rotate_completed
  • group_before_delete
  • group_deleted
  • group_created
  • group_updated
  • identity_provider_changed
  • item_before_create
  • item_created
  • item_before_delete
  • item_deleted
  • item_edit_form
  • item_edit_form_completed
  • item_index_data
  • item_moved
  • item_related_update
  • item_updated
  • item_updated_data_file
  • module_change
  • movie_menu
  • photo_menu
  • pre_deactivate
  • show_user_profile
  • site_menu
  • tag_menu
  • theme_edit_form
  • theme_edit_form_completed
  • user_add_form_admin
  • user_add_form_admin_completed
  • user_auth
  • user_auth_failed
  • user_before_delete
  • user_deleted
  • user_change_email_form (this event will be introduced when pull request #74 is completed)
  • user_change_email_form_completed
  • user_change_password_form
  • user_change_password_form_completed
  • user_created
  • user_edit_form_admin
  • user_edit_form_admin_completed
  • user_edit_form
  • user_edit_form_completed
  • user_login
  • user_login_failed
  • user_logout
  • user_menu
  • user_password_change
  • user_profile_contact_form
  • user_updated

Theme

Theme hooks are called directly by the theme itself. Not every theme will call every hook, and not every theme page will call every hook. The theme will call each hook as appropriate. For example, when rendering an album page the theme may call the head, header_top, header_bottom, page_top, album_top, thumb_top, thumb_info, thumb_bottom, album_bottom, page_bottom, and credits hooks in that order. Some of those hooks (for example thumb_top) might be called multiple times.

Each implementation of a theme hook returns a bit of HTML that will be rendered on the page at the spot where the theme calls the hook. If multiple modules render HTML at that same spot, the module loading order will determine the order of the HTML and it's up to the modules to not interfere with each other.

  • admin_credits
  • admin_footer
  • admin_header_top
  • admin_header_bottom
  • admin_page_bottom
  • admin_page_top
  • admin_head
  • album_blocks
  • album_bottom
  • album_top
  • body_attributes
  • credits
  • dynamic_bottom
  • dynamic_top
  • footer
  • head
  • header_bottom
  • header_top
  • page_bottom
  • page_top
  • photo_blocks
  • photo_bottom
  • photo_top
  • resize_bottom
  • resize_top
  • sidebar_bottom
  • sidebar_top
  • thumb_bottom
  • thumb_info
  • thumb_top

Block

Blocks are visual elements that a module can provide for the theme. In Gallery 3.0 there are two types: site blocks and admin blocks. Site blocks are typically shown in the sidebar for the site theme, and admin blocks are typically shown in the sidebar for the admin site.

To register blocks for a module, create a <module_name>_block helper with the following methods. Note that in 3.1, these hooks will likely get turned into module events.

  • get_admin_list
  • get_site_list
  • get

RSS

The RSS module allows other modules to provide their own feeds. To add an RSS feed, create a <module_name>_rss helper with the following methods. Note that in 3.1, these hooks will likely get turned into module events.

  • available_feeds
  • feed

Task

The Admin > Maintenance interface provides a way to run long lived tasks. Each task is a tiny state machine that is triggered many times. Each time it's triggered, a task should spend no more than 1-2 seconds doing a small amount of work, then return. It will get called again when appropriate. Usually the task is triggered by Javascript in the web browser so that it can display results to the end user and make sure that no single operation takes too much time and resources on the server

To add a task, create a <module_name>_task helper with the following methods. Note that in 3.1, these hooks will likely get turned into module events.

  • available_tasks

Installer

Module installation and upgrading is handled by a special hook. If a module does not provide its own code to handle installing and upgrading, the framework will perform the bare necessities required. In many modules, this is perfectly sufficient, however if your module requires additional operations such as creating or altering database tables or module variables, then an installer is required.

To create an installer, create a <module_name>_installer helper with the following methods:

  • install
  • upgrade

REST

The rest module provides a RESTful API to Gallery 3 that provides much of the fundamental Gallery 3 functionality via a remote API. This is reasonably well documented in Gallery3:API:REST