<p>Is it possible to get a list of category groups without having to loop through all categories to get their parent group name?</p>

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

<p>Just making this question clearer, as it's related to using elementapi. Ideally, I'd like to get a list of category groups via elementapi.</p>

<p>This is what I currently have:</p>

<pre><code>'api/menu' =&gt; function() {
  return [
    'elementType' =&gt; ElementType::Category,
    'paginate' =&gt; false,
    'criteria' =&gt; [
      'group' =&gt; ['stages', 'series', 'disciplines'],
      'limit' =&gt; null,
      'with' =&gt; ['group']
    ],
    'transformer' =&gt; function(CategoryModel $category) {

      $group = $category-&gt;group ?? null;

      return [
        'id' =&gt; (int)$category-&gt;id,
        'title' =&gt; $category-&gt;title,
        'slug' =&gt; $category-&gt;slug,
        'summary' =&gt; $category-&gt;summary,
        'group' =&gt; [
          'id' =&gt; (int)$group-&gt;id,
          'handle' =&gt; (string)$group-&gt;handle,
          'name' =&gt; (string)$group-&gt;name
        ]
      ];
    }
  ];
},
</code></pre>

<p>I'd want to structure the data by categorygroup rather than cateogries, something like this (it doesn't work because CategoryGroup is not an elementType) but wondering if there is another way to get the same result?:</p>

<pre><code>'api/menu' =&gt; function() {
    return [
      'elementType' =&gt; ElementType::CategoryGroup,
      'paginate' =&gt; false,
      'limit' =&gt; null
    ],
    'transformer' =&gt; function(CategoryGroupModel $categoryGroup) {

      $categories = craft()-&gt;elements-&gt;getCriteria(ElementType::Category)-&gt;group($categoryGroup-&gt;handle);

      $cats = [];
      foreach($categories as $category) {
        $cats[] = [
          'id' =&gt; (int)$category-&gt;id,
          'title' =&gt; $category-&gt;title,
          'slug' =&gt; $category-&gt;slug
        ];
      }

      return [
        'id' =&gt; (int)$categoryGroup-&gt;id,
        'title' =&gt; $categoryGroup-&gt;name,
        'handle' =&gt; $categoryGroup-&gt;handle,
        'categories' =&gt; $cats
    }
  ];
},
</code></pre>

CC BY-SA 3.0
2017-11-20T13:06:21.327Z
What about `craft()->categories->getAllGroups()`?
Robin Schambach
CC BY-SA 3.0
2017-11-20T14:48:44.087Z
@RobinSchambach thanks for the quick answer! That will get me all the groups in a template, however, I'm having trouble getting category groups via elementapi, I've updated my question to be more specific.
samput
<plugin-elementapi><category>
How to get list of category groups?
# How to get list of category groups?

(Asked by: username_0 on 2017-11-20)

Is it possible to get a list of category groups without having to loop through all categories to get their parent group name?
UPDATE:
Just making this question clearer, as it's related to using elementapi. Ideally, I'd like to get a list of category groups via elementapi.
This is what I currently have:
'api/menu' => function() {
  return [
    'elementType' => ElementType::Category,
    'paginate' => false,
    'criteria' => [
      'group' => ['stages', 'series', 'disciplines'],
      'limit' => null,
      'with' => ['group']
    ],
    'transformer' => function(CategoryModel $category) {

      $group = $category->group ?? null;

      return [
        'id' => (int)$category->id,
        'title' => $category->title,
        'slug' => $category->slug,
        'summary' => $category->summary,
        'group' => [
          'id' => (int)$group->id,
          'handle' => (string)$group->handle,
          'name' => (string)$group->name
        ]
      ];
    }
  ];
},

I'd want to structure the data by categorygroup rather than cateogries, something like this (it doesn't work because CategoryGroup is not an elementType) but wondering if there is another way to get the same result?:
'api/menu' => function() {
    return [
      'elementType' => ElementType::CategoryGroup,
      'paginate' => false,
      'limit' => null
    ],
    'transformer' => function(CategoryGroupModel $categoryGroup) {

      $categories = craft()->elements->getCriteria(ElementType::Category)->group($categoryGroup->handle);

      $cats = [];
      foreach($categories as $category) {
        $cats[] = [
          'id' => (int)$category->id,
          'title' => $category->title,
          'slug' => $category->slug
        ];
      }

      return [
        'id' => (int)$categoryGroup->id,
        'title' => $categoryGroup->name,
        'handle' => $categoryGroup->handle,
        'categories' => $cats
    }
  ];
},

**Tags:** plugin-elementapi, category

> **Comments:**
> **username_1**: What about `craft()->categories->getAllGroups()`?  
> **username_0**: @RobinSchambach thanks for the quick answer! That will get me all the groups in a template, however, I'm having trouble getting category groups via elementapi, I've updated my question to be more specific.  

---

## 0 answers

