Jump to content

Using custom Page class methods on repeater items


johnstephens
 Share

Recommended Posts

Hi!

I'm using ProcessWire's ability to recognize custom Page classes, and I have added some very useful methods to my DefaultPage class.

I just tried using one of the methods on a Repeater field item and got the error saying, "Method RepeaterPage::meta_description does not exist or is not callable in this context".

Makes sense! My method is attached to DefaultPage, not RepeaterPage. That surfaces two questions:

  1. How do I extend RepeaterPage? By creating a DefaultRepeaterPage.php file in the /site/classes directory?
  2. What is the proper way to abstract methods that I want to use for both DefaultPage and RepeaterPage, so I don't have to repeat the same code in both classes? I think I understand how to do it if they both extended the same parent class, but I don't see how that can be the case here.

Thank you in advance!

  • Like 1
Link to comment
Share on other sites

15 minutes ago, johnstephens said:

What is the proper way to abstract methods that I want to use for both DefaultPage and RepeaterPage, so I don't have to repeat the same code in both classes? I think I understand how to do it if they both extended the same parent class, but I don't see how that can be the case here.

Not sure if "the most proper" but one way I can think of is creating a module that adds the required methods/properties through hooks and just reuse the same method for both hooks.

  • Like 1
Link to comment
Share on other sites

3 minutes ago, johnstephens said:

I hope I can figure out extending RepeaterPage first. 🙂

I hope someone could shine some light if something like this is possible (don't know if you tried it?):

CustomRepeaterPage extends RepeaterPage { ... }

What I do a  lot is on site/ready.php

$wire->addHookMethod("RepeaterPage:newMethod", function(){ ..  })

 

  • Like 1
Link to comment
Share on other sites

20 hours ago, elabx said:

I hope someone could shine some light if something like this is possible (don't know if you tried it?):


class CustomRepeaterPage extends RepeaterPage { /* … */ }

 

I tried that, and I tried DefaultRepeaterPage too, placing them both in the /site/classes directory like my custom DefaultPage class which works. Alas, calling my Repeater field content still instantiates ProcessWire\RepeaterPage.

I may try your addHookMethod() suggestion shortly. Thanks for the tip!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...