Announcement

  •  » Extensions
  •  » Detect from theme if a certain plugin is active

#1 2013-11-26 10:11:39

marjolein
Member
Amsterdam, NL
2013-11-10
29

Detect from theme if a certain plugin is active

Still (happily) hacking away on my new theme... but still a lot to learn about Piwigo's "internals".

Now, I want to detect (in my theme's themeconf.inc.php file) if a certain theme is active (not just installed), and if so, manipulate/use a template variable it has set.

So far, I've thought up the following approach (taking UserCollections as an example):

- add an event handler for 'index':
 

Code:

add_event_handler('loc_begin_index', 'mk_plugin_compat');
function mk_plugin_compat()
{
  global $template;
  ...
}

- and then (replacing the ...) I could do:

Code:

  if (defined('USER_COLLEC_ID')) {              # check if UserCollections is active
    $myvar = $template->smarty->get_template_vars('pluginvar');
    if (!empty($myvar)) {
      // do something with the variable
    }
  }

My questions:
* Would this work (and not break my site)?
* Would there be a better event to hook into?
* Would there be a better approach (preserving the logic)?

-- Marjolein

Offline

 

#2 2013-11-26 10:23:29

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3271

Re: Detect from theme if a certain plugin is active

Hello

Using constants is a good approach but we generally use the global variable $pwg_loaded_plugins

Offline

 

#3 2013-11-26 10:24:56

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3271

Re: Detect from theme if a certain plugin is active

And don't use loc_begin_index it will only be triggered on index.php. use init instead

Offline

 

#4 2013-11-26 10:35:21

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: Detect from theme if a certain plugin is active

mistic100 wrote:

Hello

Using constants is a good approach but we generally use the global variable $pwg_loaded_plugins

Ah, that's very helpful! So much to learn... :)

Offline

 

#5 2013-11-26 10:39:43

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: Detect from theme if a certain plugin is active

mistic100 wrote:

And don't use loc_begin_index it will only be triggered on index.php. use init instead

I thought of using 'loc_begin_index' because the page where I want to use this (first implementation) is at /index.php/collections/ - and I reasoned that as early as 'init' the plugin's variable I want to use/manipulate may not have been set yet. (If I extend the mechanism I may use it at other pages not "below' index.php - which would then of course need a different event.)

But that's no more than an inspired guess...

Thanks for the fast reply!

Offline

 

#6 2013-11-26 11:23:37

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3271

Re: Detect from theme if a certain plugin is active

of course it depends on what you want

please note that I made heavy changes in the code on Sunday (as I said in another topic), and it will be published with Piwigo 2.6

I fear you'll have to start your changes again for the next version, If possible I would wait some weeks (Piwigo is planned for fall 2013)

see the SVN repo for current state of the code http://piwigo.org/svn/extensions/UserCollections
and the last big changeset http://piwigo.org/dev/changeset/25678

Offline

 

#7 2013-11-26 12:08:18

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: Detect from theme if a certain plugin is active

mistic100 wrote:

of course it depends on what you want

please note that I made heavy changes in the code on Sunday (as I said in another topic), and it will be published with Piwigo 2.6

I fear you'll have to start your changes again for the next version, If possible I would wait some weeks

I'm aware you're making changes :) Since from your previous replies I deduce we're working pretty much in the same direction (and I'm learning more how to work with Piwigo day by day) I do not expect major problems when you release - just work. :) But I'm definitely not upgrading anything now until I have what I'm working on now in a stable state.

(Piwigo is planned for fall 2013)

There's less than a month left of this fall... ;-)

see the SVN repo for current state of the code http://piwigo.org/svn/extensions/UserCollections
and the last big changeset http://piwigo.org/dev/changeset/25678

I'll have a peek soon - but not until I have my own code working!. (And I'll show you soon what I'm doing, too :-))

Cheers,
-- Marjolein

Last edited by marjolein (2013-11-26 13:12:48)

Offline

 

#8 2013-11-26 15:00:25

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: Detect from theme if a certain plugin is active

Getting a little lost with event handlers... this is how far I've got:

1. UserCollections'  file collections.inc.php is included in events.inc.php inside function user_collections_page()
2. main.inc.php sets this function as handler for 'loc_end_index' (so my original 'loc_begin_index' was too early, let alone 'init')
3. collections.inc.php sets some template variables - want to pick up on of then from PHP:
4. In my theme's themeconf.inc.php I set up my own handler for 'loc_end_index' with priority set to EVENT_HANDLER_PRIORITY_NEUTRAL+100 so - if I understand this correctly - it should execute after the user_collections_page() handler for 'loc_end_index'
5. In my own handler for 'loc_end_index', as a test, I am picking up a global variable and assigning this to $template - I can display this in my template just fine (so my handler 'works')
6. Now (still testing) I am attempting to pick up a variable set inside collections.inc.php with

Code:

  $U_CREATE_TEST = $template->get_template_vars('U_CREATE');  # get variable set by UC in collections.inc.php
  $template->assign('U_CREATE_TEST', $U_CREATE_TEST);

but this FAILS: I cannot display $U_CREATE_TEST in my template, it's just empty.

What am I missing? Is the priority I've set incorrect? syntax incorrect? do I need a later event even than 'loc_end_index'?

Offline

 

#9 2013-11-26 16:27:42

flop25
Piwigo Team
2006-07-06
6965

Re: Detect from theme if a certain plugin is active

Hi I'm just wondering what exactly you're trying to do: do you want to have a custom display of that plugin in your theme, or use the plugins and aadpat it by changing values, featrues etc?
I will let Mistic100 answering your questions ^^


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#10 2013-11-26 16:43:00

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3271

Re: Detect from theme if a certain plugin is active

@marjolein :
what is your template ? how is it included, where and what's its name ?

because if your template file is already parsed (for example header.tpl is parsed before loc_end_index) it won't work

Offline

 

#11 2013-11-26 17:25:13

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: Detect from theme if a certain plugin is active

flop25 wrote:

Hi I'm just wondering what exactly you're trying to do: do you want to have a custom display of that plugin in your theme, or use the plugins and aadpat it by changing values, featrues etc?
I will let Mistic100 answering your questions ^^

Yes, I want a better integration between the UserCollections plugin and the new theme I am developing.  One thing that means is no markup in PHP files: that belongs in templates. Once a plugin's markup sits in a template instead of PHP code, a theme (theme author) can either use that template or create an equivalent (with the same functionality) that matches the structure of the theme.

So, for now, I have squirreled away some markup of UC (that was in collections.inc.php) into a template and have it set a variable with the path of that template; my theme then wants to check if that variable exists, and if so either include the template or include its own alternative.

(UserCollections is simply a good test case for me - though I want to do similar with other plugins - because I definitely want its functionality on my site, and it's more than trivial :-))

Offline

 

#12 2013-11-26 17:33:15

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: Detect from theme if a certain plugin is active

mistic100 wrote:

@marjolein :
what is your template ? how is it included, where and what's its name ?

The template is mainpage_categories.tpl - part of the theme I am developing, but used by that name in UC (the same way the one in the default theme would be used).

because if your template file is already parsed (for example header.tpl is parsed before loc_end_index) it won't work

I'm trying to hook into 'loc_end_index' because that is what UC uses and where I have made it set the variable I need to pick up. I tried 'loc_begin_index' first, and did not get anything, then I traced it back and found UC uses 'loc_end_index'.

Effectively, I looked up the location where UC wants to inject code into mainpage_categories.tpl (via a pre_filter) and replaced that statement by one that just sets a template variable (which I want to pick up from my theme). At the point it can do a pre_filter, surely the whole template hasn't been rendered yet, so it should instead be able to just set a variable.

I'm sure I'm missing something, and that that is because still have an incomplete understanding of Piwigo's even model (and how UC is using it).

Last edited by marjolein (2013-11-26 17:35:13)

Offline

 

#13 2013-11-26 17:33:34

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3271

Re: Detect from theme if a certain plugin is active

which markup are you talking about ?

except the two prefilters there is no HTML in this file

Offline

 

#14 2013-11-26 17:38:35

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: Detect from theme if a certain plugin is active

mistic100 wrote:

which markup are you talking about ?

except the two prefilters there is no HTML in this file

Precisely the point. :)

The prefilter 1) assumes code to exist in the template (which in mine doesn't: this search-and-replace mechanism is very fragile!) and 2) the code has a structure that doesn't fit in with my theme.

BTW, if I directly access the variable in the template, I get it just fine. The point is that I want to avoid doing "business logic" in the template so instead I want to access that same variable from an event handler.

Last edited by marjolein (2013-11-26 17:46:34)

Offline

 

#15 2013-11-26 17:42:20

flop25
Piwigo Team
2006-07-06
6965

Re: Detect from theme if a certain plugin is active

Do you need the basic bricks, like urls, to build your own integration of that plugin: the plugin doesn't provide them and you're trying to create them?


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 
  •  » Extensions
  •  » Detect from theme if a certain plugin is active

Board footer

Powered by FluxBB

github twitter facebook google+ newsletter Donate Piwigo.org © 2002-2019 · Contact