Publisher does not support the Fluid field type. Please do not contact asking when support will be available.

If you purchased an add-on from expressionengine.com, be sure to visit boldminded.com/claim to add the license to your account here on boldminded.com.

Ticket: Using two exp:channel:categories loops on one page, the categories from the first loop show in second loop

Status Resolved
Add-on / Version Publisher 3.2.0
Severity
EE Version 5.3.2

Hop Studios

Oct 20, 2020

Related to:
https://boldminded.com/support/ticket/722
- looks like the exact same issue is occurring. I couldn’t figure out where to insert the code suggested in that ticket. I suspect this patch didn’t make it into the latest version(s) since that bug report

When outputting two exp:channel:categories loops on one page, the categories from the first loop show up in the second loop. This is not the expected behaviour. This even happens if the loops are in separate embeds. It seems like an array or variable is not getting cleared at the beginning of the tag call.

**How To Reproduce**
Create two category groups, add some categories to each.
On a template, create a tag like this:

Show Group 1

`{exp:channel:categories category_group="1" }
{category_id} {category_name}
{/exp:channel:categories}

Show Group 2

‘{exp:channel:categories category_group="2" }
{category_id} {category_name}
{/exp:channel:categories}
`

**Environment Details:**
- Version:  5.3.2
- PHP Version: 7.2.33
- MySQL Version: 5.6.49
- Web Server: Apache/2.4.38 (Debian)

#1

BoldMinded (Brian)

On 771 of the Publisher_channel_categories.php file, add this:

public function category_tree($cdata = array())
    {
        $this->category_list = []; // <---- Add this line

        $default = array('group_id', 'channel_ids', 'path', 'template', 'depth', 'channel_array', 'parent_only', 'show_empty', 'strict_empty');
#2

Hop Studios

Hey thanks for that suggestion.

I tried this, and it did remove one of the occurances of the duplicate categories, but by the looks of it, there are actually two repeats of the first category.

For instance, without your patch above, here is what I see: https://www.dropbox.com/s/xx7c0f9cmifvx0u/Screen%20Shot%202020-10-20%20at%2010.00.58%20PM.png?dl=0

In the second loop there are two <ul>.. </ul> tags, one around ‘Key Historical Documents’ and the other around the next chunk of <li>s

With the patch above, it removes the first <ul>..</ul> tag, but the second repeat of the first category is still present.

https://www.dropbox.com/s/tybavjco1ki8trm/Screen%20Shot%202020-10-20%20at%2010.03.26%20PM.png?dl=0

Here’s the HTML output from the second loop:

<ul id="conversation_topics" class="nav_categories">
   <li>
      
      <input type="checkbox" name="category[]" id="category_9" value="9">
      <label for="category_9" title="Key Historical Documents">Key Historical Documents</label>
     
     </li>
  </ul>

     <hr>
     <h1>See repeated categories below</h1>
     <ul id="types_topics" class="nav_categories">
   <li>
      
      <input type="checkbox" name="category[]" id="category_9" value="9">
      <label for="category_9" title="Key Historical Documents">Key Historical Documents</label>
     
     </li>
...

I tried emptying $this->temp_array and $this->cat_array to no avail. There must be some other variable storing the first loop.

thanks, Rowan

#3

Hop Studios

I have simplified the code down also, you might want to see the tags.

{exp:channel:categories channel="resources" show_empty="no" category_group="5" }
      
      {category_name}
     
     {/exp:channel:categories}
     <hr>
     <h1>See repeated categories below</h1>
     {exp:channel:categories channel="resources" show_empty="no" category_group="7" }
      
      {category_name}
     
     {/exp:channel:categories}

Output:

<ul id="nav_categories" class="nav_categories">
   <li>
      
      Key Historical Documents
     
     </li>
  </ul>

     <hr>
     <h1>See repeated categories below</h1>
     <ul id="nav_categories" class="nav_categories">
   <li>
      
      Key Historical Documents
     
     </li>
   <li>
      
      Commentary (media article)
     
     </li>
   <li>
      
      Data repository
     
     </li>
   <li>
      
      Fact sheet
     
     </li>
   <li>
      
      Journal article
     
     </li>
   <li>
      
      Link
     
     </li>
   <li>
      
      Online Course
     
     </li>
   <li>
      
      Platform
     
     </li>
   <li>
      
      Podcast
     
     </li>
   <li>
      
      Policy brief
     
     </li>
   <li>
      
      Report/document
     
     </li>
   <li>
      
      Tool
     
     </li>
   <li>
      
      Video
     
     </li>
   <li>
      
      Webinar
     
     </li>
   <li>
      
      Website
     
     </li>
  </ul>
#4

BoldMinded (Brian)

I’m not seeing that repeat of the first category. I used the same template tags you first noted in the ticket.

https://www.dropbox.com/s/bhjdd94klv6fqdb/ticket-2177-category-groups.png?dl=0

#5

BoldMinded (Brian)

Are you sure you added the new line to the correct location? As soon as I remove that line, I see the behavior you originally reported. As soon as I add it back, its fixed, and displays what I posted in the previous comment.

#6

Hop Studios

Hey Brian, Yes I’m sure I added the new line in the right spot.

https://www.dropbox.com/s/qslwrldx56ute41/Screen%20Shot%202020-10-22%20at%2010.39.09%20AM.png?dl=0

As I tried to explain above, adding the

$this->category_list = [];

line does change the output.

For me it removes an entire <ul> block from the output (a duplicate of the first loop). However, as mentioned, all of the category (<li>s) are showing again in the second loop, inside the second <ul>.

I just had a hunch and changed style to ‘linear’ and this actually appears to be working as expected now. The bug is only happening (for me) when i do not set the style=”linear”.

I’ll adapt my code to use the ‘linear’ style and your patch. If you want to look in more detail at the issue on this particular site, I would be happy to show you or provide you with access.

Thanks, Rowan

#7

BoldMinded (Brian)

What happens if you clear the array at the very top of the categories() function?

public function categories()
    {
        // PUBLISHER ADDITION
        $this->temp_array = [];
        $this->category_list = []; // <---- add it here, line ~79
#8

BoldMinded (Brian)

Update… did you try the fix in my previous comment?

Login to reply