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

Extending a class, but only need to change one line of code

Discussion in 'XenForo Development Discussions' started by SchmitzIT, May 12, 2014.

  1. SchmitzIT

    SchmitzIT Well-Known Member

    Hi,

    I'm attempting to write an add-on that changes a single line in one of the standard XenForo files (ControllerPublic/Forum.php).

    The functionality I intend to change can be altered by changing a single line. I figured the easiest would be for me to build an add-on that extends ControllerPublic_Forum. Would I need to duplicate the entire function and change the line I need to change in order to achieve what I'd need, or is there a more elegant solution that doesn't involve copying a lot of code that just repeats what was already there?

    Thanks!

    /Peter
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    It's difficult to say without knowing the line of code you want to change and how.

    There's different ways of doing things in certain circumstances.
     
    SchmitzIT likes this.
  3. SchmitzIT

    SchmitzIT Well-Known Member

    It's this bit here:

    Code:
    public function actionForum()
        {
           ...
           $threadsPerPage = XenForo_Application::get('options')->discussionsPerPage;
          ...
       }
     
  4. Jeremy P

    Jeremy P Well-Known Member

    If I were you, I would setup a listener for front_controller_pre_dispatch and do:

    PHP:
    public static function frontControllerPreDispatch() {
        
    $options XenForo_Application::get('options');
        
    $options->set('discussionsPerPage'999999);
    }
    You can wrap it in a conditional if you only want to change it under certain conditions, it will overwrite the set option.
     
    Chris D and SchmitzIT like this.
  5. SchmitzIT

    SchmitzIT Well-Known Member

    I'll give that a go. Thanks, Jeremy!
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    Sorry, forgot to come back and reply :)

    Jeremy's solution looks to be the one.

    The only other way is to modify the viewParams in the controller but it means duplicating the query to get the correct number of discussions etc and then replacing the existing params with the updated ones. It's quite messy, actually, because you might need to still reproduce a fair bit of the code to achieve it. Sometimes that would be ok, but in this case, Jeremy's looks like it should be perfect.
     
    SchmitzIT likes this.

Share This Page