• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Template Modification: PHP Callback does not work

rellek

Well-known member
#1
Let's just assume you want to clear the template 'header' with "XenForo rocks" using a PHP callback in a template modification... I think this doesn't work.

Here's what I entered:
Template: header
Modification Key: removeheader
Description: test
Search Type: PHP Callback
Find: /.+/is
Replace: rellek_TMSTest::myMethod

and there's a file, ./library/rellek/TMSTest.php which looks like this:
PHP:
<?php

class rellek_TMSTest {


     public function myMethod(array $args) {

          return 'XenForo rocks';

     }

}
This won't work.

It doesn't match anything when I click "Test Modification" and has the status 0/0/1 in the TM list.

If I change PHP callback to Regular Expression, It shows me that it matches everything and replaces this with my callback name.

Question is, is this a bug or am I doing something wrong?
 

Dan

Well-known member
#3
the method needs to begin with

  • get
  • is has
  • render
  • view
  • return
  • print
  • show
  • display
and it needs to be static
 

rellek

Well-known member
#5
D'oh! Thanks.

But why is it callable then? I mean, I did look into the XF source code and it does check if it is callable...?
 

Jeremy

Well-known member
#6
Why is it callable after you make it static? Because the system doesn't create an instance of your class to run your code, so it needs to be static to allow that to work.
 

rellek

Well-known member
#8
@Jeremy
No it was callable (according to that function is_callable) before I made it static. I saw in the source code that XF checks if a method is callable and throws an error if not. But @xf_phantom answered this, it seems to be a PHP flaw then.