0

I want to achieve something like the following:

$categoriesData = [];
if ($type === "helpIndex") {
    $index = -1;
    $catGroups = Category::find()->group()->all();
    foreach ($catGroups as $catGroup) {
        $index++;
        $catGroupTitle = $catGroup->name;
        $categoriesData[$index]['group'] = [
            'groupTitle' => $catGroupTitle
        ];                            
        $categoriesData[$index]['group'][] = [
            'catTitle' => $catGroup->title
        ];
        $relatedEntries = Entry::find()->relatedTo($catGroup)->all();
        foreach ($relatedEntries as $relatedEntry) {
            $categoriesData[$index]['group']['entries'][] = [
                'title' => $relatedEntry->title,
                'url' => $relatedEntry->url
            ];
        }
    }
}

But this doesn't work $catGroups = Category::find()->group()->all();

The error is that I need to pass an argument into group(). Is there another way of getting the category groups and in particular the titles?

$catGroupTitle = $catGroup->name; doesn't work as $catGroup looking at the categories within a group rather than looking at the group.

I don't want to pass in the handles as I want to automate the output for any number of groups. To get something like the following output:

      "categories": [
        {
          "group": [
            { 
              "groupTitle": "Category group title 1"
            },
            {
              "catTitle": "Category title 1"
            },
            "entries": [
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              },
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              }
            ],
            {
              "catTitle": "Category title 2"
            },
            "entries": [
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              },
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              }
            ]
          ]
        },
        {
          "group": [
            { 
              "groupTitle": "Category group title 2"
            },
            {
              "catTitle": "Category title 3"
            },
            "entries": [
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              },
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              }
            ],
            {
              "catTitle": "Category title 4"
            },
            "entries": [
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              },
              {
                "title": "Entry title",
                "url": "http://localhost/foo"
              }
            ]
          ]
        }
      ]

Or at least something like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.