<p>I would just split that category group, if possible, and offer one group for the region and one for the “A1” type of categories.</p>

<p>If this is out of question, you need to find a way to link your categories. Give them the same slug or use another field to make the connection (e.g. a Categories field) and use the corresponding criteria param to filter for the entry in the new locale.</p>

<pre class="lang-php prettyprint-override"><code>$currentLocaleCategory = $entry-&gt;myCategoriesField-&gt;last();

if ($currentLocaleCategory) {
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
    $criteria-&gt;locale = $locale;
    $criteria-&gt;descendantOf = $descendantOfParam;
    $criteria-&gt;slug = $currentLocaleCategory-&gt;slug;

    $newLocaleCategory = $criteria-&gt;first();
}
</code></pre>

CC BY-SA 3.0
2017-05-29T12:09:51.357Z
I can't split them up. That decision was made at the beginning and I didn't want to do that, because than I needed to to write a custom plugin to check if a selected product category was linked to a selected region category and add custom warnings and validation when this was not the case. On hindsight, maybe that was the better option but we're to far in the project now to make that kind of big changes.
Tom De Smet
CC BY-SA 3.0
2017-05-29T12:11:27.223Z
I'm all for the same slug idea, that was what I was allready doing, but I have no clue how to proceed with that as explained in my post. How can I get the slug of a subcategory from an entry? All I get back is an array with all categories flat listed.
Tom De Smet
CC BY-SA 3.0
2017-05-29T12:52:49.217Z
@TomDeSmet updated the answer with some example code.
carlcs
CC BY-SA 3.0
2017-05-29T13:51:00.150Z
Thanks but ->last() doesn't give me the correct subcategory but the last item in the category array which is always the bottom category of the last tree in the category group.
Tom De Smet
CC BY-SA 3.0
2017-05-29T14:20:07.130Z
Do you select more than one category? Otherwise it should be the correct one, no?
carlcs
CC BY-SA 3.0
2017-05-29T14:44:48.897Z
Yes, it's possible to select more than one category. That's the difficulty of my issue I guess :)
Tom De Smet
CC BY-SA 3.0
2017-05-29T15:15:32.290Z
*“Finding corresponding category […]”* and *“I need to find the exact same subcategory […]”* – what’s the one category are you talking about here when there are many?
carlcs
CC BY-SA 3.0
2017-05-30T06:18:25.970Z
Well. If the entry has a category 'US A 1' I need to find (if it is related to the entry) category 'Europe A 1'. They are both categories of the same group, both are on the same level and will have the same slug.
Tom De Smet
CC BY-SA 3.0
2017-05-29T12:03:28.623Z
2017-05-29T13:04:20.677Z
2017-05-29T13:04:20.677Z
carlcs
<p>I'm building a language switcher for a clients website. The website consists of multiple regions which can have multiple locales. As regions aren't supported yet in Craft 2, I've worked my way around that in a variety of ways.</p>

<p>For my products section, I have a category group of product categories. The top level of each tree within that group is the region a product belongs to. So you can have something like this:</p>

<ul>
<li>US

<ul>
<li>Category US A

<ul>
<li>Category US A 1</li>
</ul></li>
</ul></li>
<li>Europe

<ul>
<li>Category Europe A

<ul>
<li>Category Europe A 1</li>
</ul></li>
</ul></li>
</ul>

<p>So when selecting category 'US A 1' I know the region this product needs to be shown in is US and I have the correct category too.</p>

<p>Now when switching languages, I check if the region also changes. If so, I have the ID of the new region (which is the first level category of a tree within that group). So, I check if this entry exists related to that category ID. 
If so <strong>I need to find the exact same subcategory in the Europe tree as was selected in the US tree</strong> (so find 'Europe A 1') and check if the entry is also related to that category. If so, send the user to that URL. <strong>Problem is I don't know how to do that last step as all I'm getting is an array of categories without any hierachy.</strong></p>

<p>Important: I need this in a plugin in PhP so no Twig.</p>

<p><strong>UPDATE</strong></p>

<p>The only link between for example: 'US A 1' and 'Europe A 1' will be the slug. I'll have to enforce the client to make sure similar categories share the same slug. (Thanks to carlcs to ask this important info I forgot to mention :)</p>

CC BY-SA 3.0
2017-05-29T11:37:16.420Z
What’s the link between “US A 1” and “Europe A 1”? Just a naming convention for the slugs?
carlcs
CC BY-SA 3.0
2017-05-29T11:41:23.717Z
Yes, that's the only link I can think of I can use.
Tom De Smet
<plugin-development><categories><php>
Finding corresponding category in different subtrees of a category group
# Finding corresponding category in different subtrees of a category group

(Asked by: username_0 on 2017-05-29)

I'm building a language switcher for a clients website. The website consists of multiple regions which can have multiple locales. As regions aren't supported yet in Craft 2, I've worked my way around that in a variety of ways.

For my products section, I have a category group of product categories. The top level of each tree within that group is the region a product belongs to. So you can have something like this:

* US
  + Category US A
    - Category US A 1
* Europe
  + Category Europe A
    - Category Europe A 1

So when selecting category 'US A 1' I know the region this product needs to be shown in is US and I have the correct category too.

Now when switching languages, I check if the region also changes. If so, I have the ID of the new region (which is the first level category of a tree within that group). So, I check if this entry exists related to that category ID.
If so **I need to find the exact same subcategory in the Europe tree as was selected in the US tree** (so find 'Europe A 1') and check if the entry is also related to that category. If so, send the user to that URL. **Problem is I don't know how to do that last step as all I'm getting is an array of categories without any hierachy.**

Important: I need this in a plugin in PhP so no Twig.

**UPDATE**

The only link between for example: 'US A 1' and 'Europe A 1' will be the slug. I'll have to enforce the client to make sure similar categories share the same slug. (Thanks to username_1 to ask this important info I forgot to mention :)

**Tags:** plugin-development, categories, php

> **Comments:**
> **username_1**: What’s the link between “US A 1” and “Europe A 1”? Just a naming convention for the slugs?  
> **username_0**: Yes, that's the only link I can think of I can use.  

---

## 1 answers

### Answer by username_1 on 2017-05-29. Score: 1

I would just split that category group, if possible, and offer one group for the region and one for the “A1” type of categories.
If this is out of question, you need to find a way to link your categories. Give them the same slug or use another field to make the connection (e.g. a Categories field) and use the corresponding criteria param to filter for the entry in the new locale.
$currentLocaleCategory = $entry->myCategoriesField->last();

if ($currentLocaleCategory) {
    $criteria = craft()->elements->getCriteria(ElementType::Category);
    $criteria->locale = $locale;
    $criteria->descendantOf = $descendantOfParam;
    $criteria->slug = $currentLocaleCategory->slug;

    $newLocaleCategory = $criteria->first();
}

> **Comments:**
> **username_0**: I can't split them up. That decision was made at the beginning and I didn't want to do that, because than I needed to to write a custom plugin to check if a selected product category was linked to a selected region category and add custom warnings and validation when this was not the case. On hindsight, maybe that was the better option but we're to far in the project now to make that kind of big changes.  
> **username_0**: I'm all for the same slug idea, that was what I was allready doing, but I have no clue how to proceed with that as explained in my post. How can I get the slug of a subcategory from an entry? All I get back is an array with all categories flat listed.  
> **username_1**: @TomDeSmet updated the answer with some example code.  
> **username_0**: Thanks but ->last() doesn't give me the correct subcategory but the last item in the category array which is always the bottom category of the last tree in the category group.  
> **username_1**: Do you select more than one category? Otherwise it should be the correct one, no?  
> **username_0**: Yes, it's possible to select more than one category. That's the difficulty of my issue I guess :)  
> **username_1**: *“Finding corresponding category […]”* and *“I need to find the exact same subcategory […]”* – what’s the one category are you talking about here when there are many?  
> **username_0**: Well. If the entry has a category 'US A 1' I need to find (if it is related to the entry) category 'Europe A 1'. They are both categories of the same group, both are on the same level and will have the same slug.  

---

