XF 2.1 How to get the category list block to expand on individual items

Matt C.

Well-known member
How would I get the category list block to expand on individual items? As you can see from the picture, I am putting the category list block on individual items, but they don't expand. I'm not sure if they're meant to.

190295

I'm using the standard category list template code:
Code:
<xf:page option="sideNavTitle">{{ phrase('ah_cx_codex_categories') }}</xf:page>
<xf:sidenav>
    <xf:macro template="ah_cx_category_list_macros" name="list_block"
        arg-categoryTree="{$categoryTree}"
        arg-categoryExtras="{$categoryExtras}"
        arg-selected="{$category.category_id}" />
</xf:sidenav>

Thank you very much.
 
I assume you've based this off of either XFMG or XFRM's implementation, but you may need to trace through which values aren't coming out as expected to identify specifically what isn't working. The code you've shown is really just a basic wrapper -- there are a number of macros that are likely called within that.
 
Yeah it's based off XFRM. How would I trace the values?

Code:
<xf:macro name="category_list"
    arg-selected="0"
    arg-pathToSelected="{{ [] }}"
    arg-children="!"
    arg-extras="!"
    arg-isActive="{{ false }}">

    <ol class="categoryList toggleTarget{{ $isActive ? ' is-active' : '' }}">
        <xf:foreach loop="$children" key="$id" value="$child">
            <xf:macro name="category_list_item"
                arg-selected="{$selected}"
                arg-pathToSelected="{$pathToSelected}"
                arg-category="{$child.record}"
                arg-extras="{$extras.{$id}}"
                arg-children="{$child}"
                arg-childExtras="{$extras}" />
        </xf:foreach>
    </ol>
</xf:macro>

<xf:macro name="category_list_item"
    arg-selected="!"
    arg-pathToSelected="{{ [] }}"
    arg-category="!"
    arg-extras="!"
    arg-children="!"
    arg-childExtras="!">

    <xf:set var="$isSelected" value="{{ $category.category_id == $selected }}" />
    <xf:set var="$hasPathToSelected" value="{$pathToSelected.{$category.category_id}}" />
    <xf:set var="$isActive" value="{{ $isSelected OR $hasPathToSelected AND $children is not empty }}" />

    <li class="categoryList-item">
        <div class="categoryList-itemRow">
            <xf:if is="$children is not empty">
                <a class="categoryList-toggler{{ $isActive ? ' is-active' : '' }}"
                    data-xf-click="toggle" data-target="< :up :next"
                    role="button" tabindex="0" aria-label="{{ phrase('toggle_expanded') }}"></a>
            <xf:else />
                <span class="categoryList-togglerSpacer"></span>
            </xf:if>
            <a href="{{ link('codex/categories', $category) }}" class="categoryList-link{{ $isSelected ? ' is-selected' : '' }}">
                {$category.title}
            </a>
            <xf:comment>
            <span class="categoryList-label">
                <span class="label label--subtle label--smallest">{$category.entry_count|number_short}</span>
            </span>
            </xf:comment>
        </div>
        <xf:if is="$children is not empty">
            <xf:macro name="category_list"
                arg-selected="{$selected}"
                arg-pathToSelected="{$pathToSelected}"
                arg-children="{$children}"
                arg-extras="{$childExtras}"
                arg-isActive="{$isActive}" />
        </xf:if>
    </li>
</xf:macro>

<xf:macro name="list_block" arg-categoryTree="!" arg-categoryExtras="!" arg-selected="{{ 0 }}">
    <div class="block">
        <div class="block-container">
            <h3 class="block-header">{{ phrase('categories') }}</h3>
            <div class="block-body">
                <xf:if is="$categoryTree.count()">
                    <xf:macro name="category_list"
                        arg-children="{$categoryTree}"
                        arg-extras="{$categoryExtras}"
                        arg-isActive="{{ true }}"
                        arg-selected="{$selected}"
                        arg-pathToSelected="{{ $selected ? $categoryTree.getPathTo($selected) : [] }}" />
                <xf:else />
                    <div class="block-row">{{ phrase('n_a') }}</div>
                </xf:if>
            </div>
        </div>
    </div>
</xf:macro>
 
In category_list_item, look at the 3 <xf:set> lines. After that, you can {{ dump(...) }} info to see whether values are what you expect.
 
That's roughly what I figured would happen with those, but you need to figure out when it should be true and why it's not. For example, should the path to the selected element be returning true (or a truthy) value at some point? It's been a while since I implemented this code for the RM but certainly that should happen at some point (likely for any parents of the selected entry), but presumably it's not and you'd need to figure out why by debugging the underlying components of each check to determine why it's not behaving as you expect. You may find a typo/incorrect name somewhat (is it category_id? I don't know your schema). Or may you'll find unexpected data has been used to build the tree so the path isn't being generated as expected.

Since you based this off the RM, you can use that as a working implementation to compare.
 
Thank you very much Mike for your response. I will get to work on debugging it.

And yes, "category_id" is correct. Here is my schema:

Code:
$structure->table = 'xf_ah_cx_category';
$structure->shortName = 'AH\Codex:Category';
$structure->primaryKey = 'category_id';
$structure->columns = [
    'category_id' => ['type' => self::UINT, 'autoIncrement' => true, 'nullable' => true],
    'title' => ['type' => self::STR, 'maxLength' => 100,
        'required' => 'please_enter_valid_title'
    ],
    'description' => ['type' => self::STR, 'default' => ''],
    'entry_count' => ['type' => self::UINT, 'default' => 0, 'forced' => true],
    'last_update' => ['type' => self::UINT, 'default' => 0],
    'last_entry_title' => ['type' => self::STR, 'default' => '', 'maxLength' => 100],
    'last_entry_id' => ['type' => self::UINT, 'default' => 0]
];
 
If that's the schema, how are you building a tree of categories? That would need a parent ID stored.

If it's not a tree of categories, then you're going to need to make changes as that doesn't fit with the assumptions of the code you're adapting.
 
Oh there is a parent category id, I just forgot to add it to the scheme in the entity file. I add all the table columns manually.

Here it is from in PhpMyAdmin.
190324
 
find something like

<xf:macro name="simple_list_block" arg-categoryTree="!" arg-categoryExtras="!" arg-selected="{{ 9 }}">

Now the cat_id =9 is expanded in overview.


Now i try to find out, how to open two cats. [1,9] doesnt work.
 
Back
Top Bottom