Gallery2:Theme Override By Event - Gallery Codex
Personal tools

Gallery2:Theme Override By Event

From Gallery Codex

Theme Override By Event

If per-album theme and theme settings are not flexible enough for your needs, you can register an event listener to override the theme that should be used to display the current view (page).

Example

Define An Event Listener

 /* Define a event listener to decide what theme to use */
 class MyThemeEventListener /* extends GalleryEventListener */ {
     function handleEvent($event) {
         global $gallery;

         $data = $event->getData();
         $item = $event->getEntity();

         if ($gallery->isEmbedded() && $data['viewType'] == VIEW_TYPE_SHOW_ITEM) {
             $themeId = 'nzdi';
         } else if {$data['viewType'] == VIEW_TYPE_ADMIN) {
             $themeId = 'matrix';
         } else {
             // Let it use configured theme for this request
             $themeId = null;
         }

         return array(null, array('themeId' => $themeId));
     }
 }

Register The Event Listener

In A Module

 /* In module.inc */
 function performFactoryRegistrations() {
     return GalleryCoreApi::registerFactoryImplementation('GalleryEventListener',
	    'MyThemeEventListener ', 'mymoduleid', 'modules/mymoduleid/classes/MyThemeEventListener.class', 'mymoduleid',
	    array('Gallery::LoadThemeAndParameters'));
 }

For An Integration

 /* In your integration code */
 $ret = GalleryEmbed::init(...);
 ...
 /* The path is only relevant if the event listener (class MyThemeEventListener) isn't already defined. */
 $ret = GalleryCoreApi::registerFactoryImplementationForRequest('GalleryEventListener',
	    'MyThemeEventListener ', 'myintegration', '/../relative/path/to/my/integration/integrationCode.php', 'myintegration',
	    array('Gallery::LoadThemeAndParameters'));
 ...
 $data = GalleryEmbed::handleRequest();
 ...

Note that there is also the easier, but less flexible alternative:

  /* In your integration code */
 $ret = GalleryEmbed::init(...);
 ...
 $ret = GalleryEmbed::setThemeForRequest('siriux')
 ...
 $data = GalleryEmbed::handleRequest();
 ...

Gallery::LoadThemeAndParameters Event Definition

The event has been introduced in core API 7.40 / embed API 1.3.

Event Data

  • 'viewType': Is one of the following: VIEW_TYPE_MODULE|VIEW_TYPE_SHOW_ITEM|VIEW_TYPE_ADMIN|VIEW_TYPE_ERROR (constants)
  • 'viewName': Has the format of Gallery view names (same format as 'view' in GalleryUrlGenerator::generateUrl(), e.g. 'core.ShowItem'.

Event Entity

  • $event->getEntity() returns either null or the GalleryItem for the current page.

Return Value

The returned value is an associative array with the following keys:

  • 'themeId' string - The theme id of the theme to be used for this request. Omit the key or set it to null to let Gallery use the configured theme instead of an override.
  • 'params' array (optional) - Theme parameters, allowing to override theme parameters as well. If not specified, the default theme parameters for the specified theme are used. if specified, the parameters should be exhaustive.

Overriding Theme Parameters

To override theme parameters, it makes sense to let users configure the theme settings to be used as override. Take a look at the keyalbum module (modules/keyalbum/KeywordAlbumSiteAdmin.inc) to see how a theme settings page can be shown, handled and how the settings can be stored.