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

$fetchOptions or $conditions for parent_node_id

LPH

Well-known member
#1
I'm trying to replace older code that does not use $fetchOptions and $conditions.

Here is the old code.

PHP:
/** @var $node_qry XenWordXFForums */
$node_qry = "
            SELECT node_id, title, display_in_list, node_type_id, parent_node_id FROM `xf_node`
            WHERE display_in_list = 1 AND node_type_id = 'Forum' AND parent_node_id = '$cat_id'
            ORDER BY `title` ASC
            LIMIT $number_of_forums
         ";

$nodes = XenForo_Application::get( 'db' )->fetchAll( $node_qry );
Here is the New code

PHP:
// Temporarily Assign as working through code
$instance['show_categories'] = 1;

$instance['show_number_of_forums']= 3;

//  End Temporary Settings

$cat_id = $instance['show_categories']; // parent_node_id

$number_of_forums = $instance['show_number_of_forums']; // limit

$conditions = array(
  'display_in_list' => 1,
  'parent_node_id' => $cat_id
);

$fetchOptions = array(
  'join' => XenForo_Model_Thread::FETCH_USER
          | XenForo_Model_Thread::FETCH_FORUM
          | XenForo_Model_Thread::FETCH_FIRSTPOST,
  'limit' => $number_of_forums,
  'order' => 'display_oder',
  'orderDirection' => 'desc'
);

/** @var  $forumModel XenForo_Model_Forum */
$forumModel = XenForo_Model_Forum::create('XenForo_Model_Forum');
$nodes = $forumModel->getForums( $conditions, $fetchOptions );

$visitor = XenForo_Visitor::getInstance();

foreach ( $nodes AS $node ) {

  $visitor->setNodePermissions( $node['node_id'], isset( $node['node_permission_cache'] ) );
  if ( $forumModel->canViewForum( $node ) ) {

    echo(
      "<div class='entry-meta'><a href='"
      . XenForo_Link::buildPublicLink( 'canonical:forums', $node )
      . "'>"
      . XenForo_Helper_String::wholeWordTrim( $node['title'], 40 )
      . "</a><br />"
    );
  }
}
Unfortunately the WHERE for parent_node_id isn't being honored. I looked in the model and don't see a way to set a condition to only return forums from a certain category (parent_node_id).

Does anyone know the correct condition or fetchOption to get the forums for a particular category?
 

Mike

XenForo developer
Staff member
#2
Have a look at the getForums() method you're calling, which internally calls prepareForumConditions() and you'll see what conditions are available. In this case, not many.

You'd either need to extend the condition function to add the conditions you want or (the simpler approach) use/create a function that does exactly what you need.
 

LPH

Well-known member
#3
@Mike - As always, thank you for the clarification. I decided to use a counter to limit what is shown and set a conditional for the parent_node_id. The end result seems cleaner than the original code.

PHP:
$cat_id = $instance['show_categories']; // parent_node_id

$number_of_forums = $instance['show_number_of_forums']; // limit

/** @var  $forumModel XenForo_Model_Forum */
$forumModel = XenForo_Model::create( 'XenForo_Model_Forum' );
$forums     = $forumModel->getForums( $conditions = array(), $fetchOptions = array() );

$visitor = XenForo_Visitor::getInstance();

$i = 0;

foreach ( $forums AS $forum ) {

   if ( $i == $number_of_forums ) {
      break;
   }

   if ( $forum['parent_node_id'] == $cat_id ) {

      $visitor->setNodePermissions( $forum['node_id'], isset( $forum['node_permission_cache'] ) );
      if ( $forumModel->canViewForum( $forum ) ) {

         echo(
            "<div class='entry-meta'><a href='"
            . XenForo_Link::buildPublicLink( 'canonical:forums', $forum )
            . "'>"
            . XenForo_Helper_String::wholeWordTrim( $forum['title'], 40 )
            . "</a><br />"
         );
      }
   }

   $i ++;
}