XF 2.0 Get all prefixes grouped by forum

LPH

Well-known member
The following code loops through and gets all the prefix titles.

Code:
/** @var \XF\Entity\Forum $node */
$nodes = \XF::app()->finder( 'XF:Forum' )->fetch();

foreach ( $nodes AS $node ) {
   $prefix_cache = $node->prefix_cache;

   foreach ( $prefix_cache AS $prefix_id ) {
      $prefixTitle = \XF::app()->templater()->fn( 'prefix_title', [ 'thread', $prefix_id ] );
      echo $prefixTitle . '<br />';
   }
}

I don't like the nested loop and the final structure relies on dropdown select:

ForumA Title
-- Prefix1 Title
-- Prefix2 Title
ForumB Title
-- Prefix2 Title
-- Prefix4 Title

In other words, the forum is listed and the possible prefixes are listed below it.

Question 1
Is there a better way to grab the forum title then the possible prefixes for that forum? I saw a getPrefixesGrouped()

Question 2
Is there a way to organize these in the same order requested in the XenForo panel?
 
If nothing else, I figured out how to use array_column:

Code:
/** @var \XF\Entity\Forum $node */
$nodes = \XF::app()->finder( 'XF:Forum' )->fetch();
$prefix = array_column( $nodes->toArray(), 'prefix_cache' );

\XF::dump($prefix);

This leads to

XenForo Prefixes.webp

Pulling the forum title and prefix title from this information still eludes me. Oh well. If anyone has any suggestions then please let me know.
 
PHP:
/** @var \XF\Repository\ThreadPrefix $nodeRepo */
$nodeRepo = \XF::repository('XF:ThreadPrefix');
$nodeTree = $nodeRepo->getPrefixListData();

\XF::dump($nodeTree);

This provides an arrayCollection.
 

Attachments

  • Prefix Array Collection.webp
    Prefix Array Collection.webp
    63.5 KB · Views: 29
Code:
/** @var \XF\Entity\Forum $node */
$nodes = \XF::app()->finder( 'XF:Forum' )->fetch();

echo '<select id="xenword_prefix_id" name="xenword_prefix_id" class="postform" >';

echo '<option value="">Select A Prefix</option>';

foreach ( $nodes AS $node ) {

   echo $node->title , '<br />';

   foreach ( $node->prefix_cache AS $prefix_id ) {

      echo '<option value="' . $prefix_id . '"';

      if ( $prefix_id == $prefixId ) {
         echo 'selected="selected"';
      }

      echo '<span style="margin-left:20px;">' . \XF::app()->templater()->fn( 'prefix_title', [ 'thread', $prefix_id ] ) . '</span>';

      echo '</option>';
   }
}

echo '</select>';

This almost works. The titles of the Forums are not showing because they are outside the option value. I haven't figured out how to get that part to work.

Screen Shot 2017-12-19 at 9.22.39 PM.webp
 
Got it !
Code:
/** @var \XF\Entity\Forum $node */
$nodes = \XF::app()->finder( 'XF:Forum' )->fetch();

echo '<select id="xenword_prefix_id" name="xenword_prefix_id" class="postform" >';

echo '<option value="">Select A Prefix</option>';

foreach ( $nodes AS $node ) {

   echo '<option value="">' . $node->title . '</option>';

   foreach ( $node->prefix_cache AS $prefix_id ) {

      echo '<option value="' . $prefix_id . '"';

      if ( $prefix_id == $prefixId ) {
         echo 'selected="selected"';
      }

      echo ' class="_depth' . $node['depth'] . '">';

      echo str_repeat('&nbsp;', $node['depth'] * 4 );

      echo '<span style="margin-left:20px;">' . \XF::app()->templater()->fn( 'prefix_title', [ 'thread', $prefix_id ] ) . '</span>';

      echo '</option>';
   }
}

echo '</select>';

And it works. :)
 

Attachments

  • Screen Shot 2017-12-19 at 9.28.37 PM.webp
    Screen Shot 2017-12-19 at 9.28.37 PM.webp
    7.2 KB · Views: 27
Back
Top Bottom