newb questions about views & controllers

Aosh

Joined: 2007-04-20
Posts: 14
Posted: Sun, 2007-04-22 17:18

Hello =)

I've spent a few hours trying to write a module, but i'm running into a brick wall. I just have a few simple questions:

1. When does the view's loadTemplate() get called? Does it only happen if i'm explicitely going to "main.php?g2_view=mymodule.myview"?

2. Is it possible to embed a module into some other page? Say, if within my theme's photo.tpl, I simply do a {include file="modules/mymodule/templates/myview.tpl"}, will the view's loadTemplate() get called? (corollary to ques 1) If not, is it possible to embed at all?

3. How do I pass variables to the controller if I'm not using a form? All the modules that I've seen that pass variables back to the controller use a <form> with a submit action. But I only want a simple:

<a href="{g->url arg1="controller=mymodule.MyController" arg2="somevar=someval"}>
click this link for something to happen!
</a>

But I can't seem to access $somevar or form['somevar'] from the controller's handleRequest() function. Any way to do it???

many thanks!!

 
Aosh

Joined: 2007-04-20
Posts: 14
Posted: Fri, 2007-04-27 16:25

not a very active forum here, huh? =P

I have to say, it took me awhile to get familiar w/ the API & architecture of Gallery2 (I've been looking at and tracing through the code for a few weeks now) -but once you get it, it's not too complicated at all to write themes & modules without having to resort to hack-ish "fixes" and know what is and isn't doable. After going through the Module Development Tutorial I had a general idea of how the other components fit together, but it took a lot of experimentation to confirm or correct my conceptions. (Turning debug to 'buffer' in config.php does WONDERS!)

I really feel the info under Module Directory Structure is absolutely crucial but very badly needs to be expanded on for new developers. It kind of just glosses over the details.

Anyways, I'll answer my own question in case any1 might find it useful. A View is not the only way to display a module (the tutorial does not go much further than briefly showing a view & controller). A GalleryView is more like a standalone page. The way to "embed" a module into another view is, create a Block by defining it in mymodule/templates/block/NameOfMyBlock.tpl. This can be called from the template you want to embed it into using {g->block type="mymodule.NameOfMyBlock"} as long as you activate the module through the admin Plugin page.

From here, all the usual things available to a module are available (Preload, Callback, Controller, etc..). If the module needs to do some kind of work before doing something, just add a {g->callback type='mymodule.SomeFunction'} to the NameOfMyBlock.tpl. Then define the callback by creating Callback.inc and writing something along the lines of:

class MyModuleCallbacks {
function callback( ..) {
switch($callback) {
case 'SomeFunction':
// Do something
}
}
}

There are many ways to call a controller to respond so some user action/input. It is simply a matter of passing the variable controller="somemodule.somecontroller" through either a POST or a GET. The most common way is using a form as per the Tutorial linked above (usually causes it to be POSTed). Using a link works just as well (GET): <a href="{g->url}" arg1="controller=mymodule.MyController" arg2="someVar={$someVar}" ..}">Click Me</a>

To access the parameters passed into the controller is straightforward. Gallery2's usual behavior is to append a gallery2 specific prefix to the name of the variable (the default is "g2_"). Thus, if it was submitted using a GET, instead of $_GET['someVar'] it becomes a $_GET['g2_someVar']. Fortunately we don't have to worry about what the prefix is, since we have a utility function:

$x = GalleryUtilities::getRequestVariables('someVar');

It'll look through $_GET & $_FORM and look for 'g2_someVar' so we don't have to care about how it was submitted.

Hope someone else finds this info helpful.

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Sun, 2007-12-16 13:26

Yes, I found this quite helpful. Thanks.