XF 2.2 RM -> How can i manipulate getCategoryListData for RM Cats List?

Robert9

Well-known member
Code:
    public function getCategoryListData(\XFRM\Entity\Category $category = null)
    {
        $categoryRepo = $this->getCategoryRepo();
        $categories = $categoryRepo->getViewableCategories();
        $categoryTree = $categoryRepo->createCategoryTree($categories);
        $categoryExtras = $categoryRepo->getCategoryListExtras($categoryTree);

        return [
            'categories' => $categories,
            'categoryTree' => $categoryTree,
            'categoryExtras' => $categoryExtras
        ];
    }


As an example, i want to get rid of the cat with ID == 1 from $categories

$categories = $categoryRepo->getViewableCategories();

What can i do here with $categories, please?

$categoryTree = $categoryRepo->createCategoryTree($categories);
 
Hmm, ok, finally it was easy:

Code:
foreach($notListedCategoryIds as $key => $value)
            {
                unset($categories[$value]);
            }
 
The ArrayCollection object implements the \ArrayAccess interface, which allows you to call offsetGet($key), offsetUnset($key), offsetExists($key) and offsetSet($key, $value). If you switch to a decent IDE, you can see the available methods on your object.
 
Thank you very much.
A normal unset helped me out here to get rid of the cats i dont want.

Code:
            foreach($notListedCategoryIds as $key => $value)
            {
                unset($categories[$value]);
            }

Next step is to find all children to unset them also. I guess i will need the tree for that and try this now.
 
Code:
namespace Author\Name\XFRM\ControllerPlugin;

class Overview extends XFCP_Overview
{


Let' say i have some rm_cats 12, 27, 99, that i dont like:

Code:
public function getCoreListData(array $sourceCategoryIds, \XFRM\Entity\Category $category = null)
{
   $notLikedCategoryIds = [12, 27, 99];
   // get rid of the $notLikedCategoryIds

   $sourceCategoryIds = array_diff($sourceCategoryIds, $notLikedCategoryIds);

   return parent::getCoreListData($sourceCategoryIds, $category);
}


but dont forget that there are maybe subcats!

Code:
public function getCoreListData(array $sourceCategoryIds, \XFRM\Entity\Category $category = null)
{
   $notLikedCategoryIds = [12, 27, 99];

   // we fetch all viewable cats
   $categoryRepo = $this->getCategoryRepo();
   $categories = $categoryRepo->getViewableCategories();
   $categoryTree = $categoryRepo->createCategoryTree($categories);

   // we fetch all subcats for my notLikedCats ...
   foreach($notListedCategoryIds AS $key => $resourceCategoryId)
   {
      $descendants = $categoryTree->getDescendants($resourceCategoryId);
      $subCategoryIds = array_keys($descendants);
      // ... and add them to my notLikedCategoryIds
       $notLikedCategoryIds= array_merge($notLikedCategoryIds, $subCategoryIds);
   }

   // get rid of the $notLikedCategoryIds
   $sourceCategoryIds = array_diff($sourceCategoryIds, $notLikedCategoryIds);

   return parent::getCoreListData($sourceCategoryIds, $category);
}


What i dont like here, is that we fetch the complete cats for our tree.

How can i do:

$notLikedCategoryIds = [12, 27, 99];
$categories = get my Categories with resource_category_id IN $notLikedCategoryIds;

Should i just use a finder here to come from my resCatIds to my Cats and then to the reduced tree?
Or can i send my $notLikedCategoryIds to a method to get the reduced tree?

Thank you very much for your advice.
 
Back
Top Bottom