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

How to replace XenForo core methods with addons?

Discussion in 'XenForo Development Discussions' started by Earl, Aug 1, 2016.

  1. Earl

    Earl Member

    This maybe a silly question, but I really need to know if there is a possible way to do this.

    for an instance lets say if I need to add a new inline mod option to $postModOptions in library/XenForo/Model/Post.php line: 959

    PHP:
    /**
    * Adds the canInlineMod value to the provided post and returns the
    * specific list of inline mod actions that are allowed on this post.
    *
    * @param array $post Post info
    * @param array $thread Thread the post is in
    * @param array $forum Forum the thread/post is in
    * @param array|null $nodePermissions
    * @param array|null $viewingUser
    *
    * @return array List of allowed inline mod actions, format: [action] => true
    */

    public function addInlineModOptionToPost(array &$post, array $thread, array $forum, array $nodePermissions null, array $viewingUser null)
    {
       
    $this->standardizeViewingUserReferenceForNode($thread['node_id'], $viewingUser$nodePermissions);

       
    $postModOptions = array();
       
    $canInlineMod = ($viewingUser['user_id'] && (
          
    XenForo_Permission::hasContentPermission($nodePermissions'deleteAnyPost')
          || 
    XenForo_Permission::hasContentPermission($nodePermissions'undelete')
          || 
    XenForo_Permission::hasContentPermission($nodePermissions'approveUnapprove')
          || 
    XenForo_Permission::hasContentPermission($nodePermissions'manageAnyThread')
       ));

       if (
    $canInlineMod)
       {
          if (
    $this->canDeletePost($post$thread$forum'soft'$null$nodePermissions$viewingUser))
          {
             
    $postModOptions['delete'] = true;
          }
          if (
    $this->canUndeletePost($post$thread$forum$null$nodePermissions$viewingUser))
          {
             
    $postModOptions['undelete'] = true;
          }
          if (
    $this->canApproveUnapprovePost($post$thread$forum$null$nodePermissions$viewingUser))
          {
             
    $postModOptions['approve'] = true;
             
    $postModOptions['unapprove'] = true;
          }
          if (
    $this->canMovePost($post$thread$forum$null$nodePermissions$viewingUser))
          {
             
    $postModOptions['move'] = true;
          }
          if (
    $this->canCopyPost($post$thread$forum$null$nodePermissions$viewingUser))
          {
             
    $postModOptions['copy'] = true;
          }
          if (
    $this->canMergePost($post$thread$forum$null$nodePermissions$viewingUser))
          {
             
    $postModOptions['merge'] = true;
          }
       }

       
    $post['canInlineMod'] = (count($postModOptions) > 0);

       return 
    $postModOptions;
    }

    what if I wanna add something like
    Code:
          if ($this->canDeleteRating($post, $thread, $forum, $null, $nodePermissions, $viewingUser))
          {
             $postModOptions['delete_rating'] = true;
          }
    to the method?

    Should I create the whole method with the same name? does it replace the XenForo core codes?
     
  2. Snog

    Snog Well-Known Member

    Extend the library/XenForo/Model/Post.php class and use something like this to extend the function...
    Code:
    public function addInlineModOptionToPost(array &$post, array $thread, array $forum, array $nodePermissions = null, array $viewingUser = null)
    {
          $parentResponse = parent::addInlineModOptionToPost($post, $thread, $forum, $nodePermissions, $viewingUser)
          $parentResponse['delete_rating'] = false;
    
          if ($this->canDeleteRating($post, $thread, $forum, $null, $nodePermissions, $viewingUser))
          {
             $parentResponse['delete_rating'] = true;
          }
    
          return $parentResponse;
    }
    
     
    Earl likes this.
  3. Earl

    Earl Member

    will it work with other xenforo core codes like: library/XenForo/Search/DataHandler/Post.php :201
    ???

    Code:
    public function addInlineModOption(array &$result)
    {
       return $this->_getPostModel()->addInlineModOptionToPost($result, $result, $result, $result['permissions']);
    }
     
  4. Snog

    Snog Well-Known Member

    If you extend the library/XenForo/Model/Post.php class, it will work anywhere the _getPostModel()->addInlineModOptionToPost is called.
     
    Earl likes this.
  5. Earl

    Earl Member

    I have one other quick question, can I use the same trick to replace a method that has written on another 3rd party add-on?
    If that is the case I must extend the 3rd party add-on's class as opposed to the parent class which extends by the 3rd party add-on?

    Thanks for the reply, this is gonna help a lot. I just saw many developers have done it in this way. can't believe I didn't notice :oops:
     
    Last edited: Aug 2, 2016
  6. Snog

    Snog Well-Known Member

    I've never tried it, but I don't see any reason it wouldn't work. Extending a class is extending a class.

    But, if the add-on's method your extending is already extending the XenForo class you would still just do the XenForo class not the add-on's class.

    And, unless there is some pressing reason to do so you don't want to 'replace' the XenForo class you want to 'extend' the class. Replacing it could very well break all other add-ons installed on a system.
     
    Xon and Earl like this.

Share This Page