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

Accessing viewParams via Parent?

Discussion in 'XenForo Development Discussions' started by James, Aug 5, 2011.

  1. James

    James Well-Known Member

    I'm extending the member controller and I'm trying to modify the $user array inside the viewparams.

    The simple question is: How can I access the $viewParams variable via parent::actionMember(), if at all?

    To put it simply: I want to override {$user.username} to {$user.somethingelse} without physically going through the template replacing {$user.username} with {$user.somethingelse}

    Hope I made it clear.
     
  2. Jeremy P

    Jeremy P Well-Known Member

    PHP:
    $response parent::actionMember();
    $viewParams $response->params;
    ?
     
  3. James

    James Well-Known Member

    I tried that method. Using that method, I should be able to replace {$user.username} with {$user.myString} by doing this:
    PHP:
    $viewParams['user']['username'] = $viewParams['user']['myString'];
    But that doesn't work.
     
  4. Jeremy P

    Jeremy P Well-Known Member

    Mm, it should if you're doing it right. Just modifying the $viewParams variable won't work because it's not being returned to the original response.

    PHP:
    $response parent::actionMember();
    $viewParams $response->params;

    $viewParams['user']['username'] = $viewParams['user']['myString'];

    $response->params $viewParams;
    return 
    $response;
     
  5. James

    James Well-Known Member

    Woops, I wasn't re-assigning the $response->params. Thanks Jeremy. Rookie mistake :oops:
     
    Jeremy P likes this.
  6. Jeremy P

    Jeremy P Well-Known Member

    No worries, glad to help :)
     
  7. James

    James Well-Known Member

    Now I'm getting a strange error when trying to access the member card:
    Undefined property: XenForo_ControllerResponse_Reroute::$params on line 8. The code:

    PHP:
    public function actionMember()
    {
    $response parent::actionMember();
    $viewParams $response->params;

    if(
    $viewParams['user']['myField'])
    {
    $viewParams['user']['username'] = $viewParams['user']['myField'];
    }

    $response->params $viewParams;
    return 
    $response;
    }
    I think it's too late for me to be working :whistle:
     
  8. Jeremy P

    Jeremy P Well-Known Member

    The card is accessed with the member action, but it internally just redirects to actionCard(); (or something, if memory serves right). Anyways, as such, it won't have params.

    Soo

    PHP:
    $response parent::actionMember();

    if (!isset(
    $response->params))
    {
        return 
    $response;
    }

    $viewParams $response->params;

    if(
    $viewParams['user']['myField'])
    {
        
    $viewParams['user']['username'] = $viewParams['user']['myField'];
    }

    $response->params $viewParams;
    return 
    $response;
     
    James likes this.
  9. James

    James Well-Known Member

    Yup, redirects to actionCard(). Thanks Jeremy.
     
    Jeremy P likes this.
  10. James

    James Well-Known Member

    I've just realised that I cannot modify any template where I have edited the viewParams via extending the relevant controllerpublic file.

    Example:
    PHP:
    class MyAddon_ControllerPublic_Thread extends XenForo_ControllerPublic_Thread
    {
    public function 
    actionIndex()
    {
    $response parent::actionIndex();

    foreach(
    $response->params['posts'] AS $postId => $content)
    {
    if(
    $content['myField'])
    {
    $response->params['posts'][$postId]['username'] = $content['myField'];
    }
    }

    return 
    $response;
    }
    }
    I now can't modify any of the content inside any template that relies on XenForo_ControllerPublic_Thread, not even when I disable the add-on.

    Upon trying to edit the template I get an error telling me to look at the JS console. When I do, it tells me this:
    Code:
    PHP Array
    handleServerError()xenfor...1d5099e (line 1148)
    xhr = XMLHttpRequest { responseText="Array", mozResponseArrayBuffer=ArrayBuffer, more...}
    responseText = "parsererror"
    errorThrown = "Invalid JSON: Array"
    error()xenfor...1d5099e (line 1099)
    xhr = undefined
    textStatus = undefined
    errorThrown = undefined
    handleError()jquery....min.js (line 142)
    a = Object { url="admin.php?templates/save-multiple.json", global=true, more...}
    b = XMLHttpRequest { responseText="Array", mozResponseArrayBuffer=ArrayBuffer, more...}
    d = "parsererror"
    e = "Invalid JSON: Array"
    onreadystatechange()jquery....min.js (line 141)
    m = readystatechange
    console.error('PHP ' + xhr.responseText);
    
     
  11. Jeremy P

    Jeremy P Well-Known Member

    Nothing else stands out? Other templates are fine? All extended ones fail?
    File health check in ACP?

    Haven't dug into the template system much but it looks like it's trying to make an ajax request to save multiple templates and it's returning an array unexpectedly. Are you sure you're not accidentally messing with the template engine?
     
  12. James

    James Well-Known Member

    Ah, I've found the source now it's alright. I forgot I was debugging in XenForo files :mad: I really should sleep.
     

Share This Page