Getting the forum number from the URI

AndyB

Well-known member
I'm creating an add-on which is extending the DataWriter_preSave().

When a member is creating a new thread, I would like to be able to throw a error message if they are in the incorrect forum, so I need to get the forum number from the URI.

I can do this using the PHP command:

PHP:
$var = $_SERVER['REQUEST_URI'];

After I get $var I parse for the forum number, but this is messy.

It would be great if I could use an existing XF command designed for this purpose. I have tried the following without success:

PHP:
$nodeId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);

Thank you.
 
I hope I'm not missing your point here, but if you are dealing with thread creation then why not extend XenForo_DataWriter_Discussion_Thread
You can then get the forum id thus:
PHP:
if ($this->isInsert())
{
    $forumId = $this->get('node_id');
}

I assume you are checking the message content in order to find out if they are posting in the correct forum?!

Edit:
Would it not be simpler to run your check in function actionAddThread() ?
 
Last edited by a moderator:
Hi Syndol,

Thank you for your assistance.

I ended up extending ControllerPublic_Forum::actionCreateThread. This way I can throw the error before the user wastes time typing in a message.

I still ended up parsing the URL using the $_SERVER['REQUEST_URI'] command, but I assume that's the only way to get the current forum ID.
 
Well if all you want to do is prevent them from creating the thread all together then why not extend function canPostThreadInForum() in XenForo_Model_Forum
That way the create thread button will not even appear if they cannot post.
 
Thank you, Syndol.

I sure do appreciate the very good suggestions.

The add-on which I have created is designed to force new users to make their first post be in an Introduction forum. My thinking is that once they are approved they really should see the same links (Reply, Create Thread etc..) as any other member. The error message is thrown once they try to create a new thread in anything other than the Introduction forum.
 
Thank you, Syndol.

I sure do appreciate the very good suggestions.

The add-on which I have created is designed to force new users to make their first post be in an Introduction forum. My thinking is that once they are approved they really should see the same links (Reply, Create Thread etc..) as any other member. The error message is thrown once they try to create a new thread in anything other than the Introduction forum.
Is this not possible by creating a user upgrade? <1 posts and you can only post in Introductions, >1 post and you upgrade them?
 
The add-on which I have created is designed to force new users to make their first post be in an Introduction forum. My thinking is that once they are approved they really should see the same links
That makes sense. I was just suggesting other stuff as I was not sure where you were going with this.
Is this not possible by creating a user upgrade? <1 posts and you can only post in Introductions, >1 post and you upgrade them?
True, but the user will not get a message explaining why they cannot post there (unless you create a notification of course).
 
Is this not possible by creating a user upgrade? <1 posts and you can only post in Introductions, >1 post and you upgrade them?

Hi James,

Thank you for the suggestion. I prefer to have one user group for all my members. It just keeps things simple that way.
 
PHP:
$nodeId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
Should work inside the controller.

my code won't work for everybody:whistle: (not everybody is including the node_id in the url)
This should do it:
PHP:
$forumId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
     $forumName = $this->_input->filterSingle('node_name', XenForo_Input::STRING);
    $ftpHelper = $this->getHelper('ForumThreadPost');
     $forum = $ftpHelper->assertForumValidAndViewable($forumId ? $forumId : $forumName);

     $nodeId = $forum['node_id'];
 
Back
Top Bottom