XF 2.0 Extending Forum or Node entity, best practice and creating a Node Tree

Marcel

Active member
Further to my previous question, Im still stuck.

I've added 5 custom columns to the Forum table, and extended the Forum entity to read these values

My problem comes when creating a Node Tree for a public template.
Using this, I create a tree with all viewable nodes :
PHP:
        /** @var \XF\Repository\Node $nodeRepo */
        $nodeRepo = \XF::repository('XF:Node');
        $nodeTree = $nodeRepo->createNodeTree($nodeRepo->getFullNodeList()->filterViewable());

I now need to trim that down based on one of the fields in the Forum entity.
I'm not sure how to access those fields as we're dealing with the node repo to create the tree, and that doesn't have access to the xf_forum table.

How do I use that nodeTree to filter out those Forums based on a custom column in the forum table?

I think I can do it one of two ways (I don't know how to do either though!)

Method One:
1. Add the custom fields to xf_forum
2. Use the nodeRepo to create a full nodeTree
4. Get a list of Forums based on the custom column
4. Using that, and the nodeTree filter out those nodes from the tree that aren't in the Forum list.

Method Two:
1. Add the custom fields to xf_node
2. Use the nodeRepo to create a full nodeTree
3. Using the nodeTree, filter out those based on the custom field.

I'm struggling with Method One, as I'm stuck on step 4.
I'm struggling with Method Two, because if I extend Admin\Controller\Node class, then I can't extend the saveTypeData() method as per the XF2 Development Guide example.

Not sure which way to turn now. Which would be the best practice way to do this, one or two?
 

Marcel

Active member
Aha! I think I've got it. Well, a different way of doing it anyway.

I've added the custom fields to the xf_forum table.
Added a Listener to entity_structure (Hint : Forum) to read the columns
Added a Listener to entity_structure (Hint : Node) to trigger the following

PHP:
public static function nodeEntityStructure(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure)
    {
        $structure->relations['Forum'] = [
                'entity' => 'XF:Forum',
                'type' => 1,
                'conditions' => 'node_id',
                'primary' => true
        ];

        $structure->getters['ut_archive_enabled'] = ['getter' => 'getArchiveOptions'];
    }

Then added a class extension, extending XF\Entity\Node
In UserTools\XF\Entity\Node.php have the following

PHP:
class Node extends XFCP_Node
{
    public function getArchiveOptions()
    {
        return $this->ut_archive_enabled;
    }
}

No doubt I've gone completely arse about face with it, however it works and the template pulls a list of viewable forums....and disables selection of the ones that don't have the correct custom_field set.

It gives me the end usability but not how I'd have preferred to go about it. I'd have preferred to only see a tree that reflects the selectable forums. Never mind. Could wrap an <xf:if> into the template.....
 
Top