Fork me on GitHub

How to get a category id by using it's name ?  Bottom

  • Hi,

    I would like to known if there is an easy way to find a category id when I've only it's name.

    A solution (but not very usefull) is to get all the categories, compare their name, extract the correct id.
  • When you know the category property, you can get the category by using getCategoryByPath
    In News the following is used for example in pnuser.php, where $cat and $prop are inputs. So $cat can be the name of the category (or the ID) and $prop is the category property name

    Code

    ...
            // get the categories registered for the News stories
            $catregistry = CategoryRegistryUtil::getRegisteredModuleCategories('News', 'stories');
            $properties = array_keys($catregistry);
            $lang = pnUserGetLang();

            // validate the property
            // and build the category filter - mateo
            if (!empty($prop) && in_array($prop, $properties) && !empty($cat)) {
                if (!is_numeric($cat)) {
                    $rootCat = CategoryUtil::getCategoryByID($catregistry[$prop]);
                    $cat = CategoryUtil::getCategoryByPath($rootCat['path'].'/'.$cat);
                } else {
                    $cat = CategoryUtil::getCategoryByID($cat);
                }
    ...

    Does that help?

    --
    campertoday.nl, Module development, Dutch Zikula Community
  • Yes but with this solution you've to put the $prop into the URL, I would like to have some urls as short as possible so I prefered my first solution. But I think that it could be good to have a class like getCategoryByID but for category name getCategoryByName.
  • I you have a fixed property you can hard code that in of course, that would lead to a short URL. It seems that you're aiming for a solution where you already know the property beforehand.
    The flexibiltiy of categories and properties has the disadvantage that searching by name is not direct. The result can be multiple category id's when leaving out the properpty.

    --
    campertoday.nl, Module development, Dutch Zikula Community
  • Thanks for your answers, I think that getCategoryByPath is the answer to what I want to do.

    I thought that with getCategoryByPath we can only used the path with the ids of the categories but it's wrong, we can use the names of the categories.

    Just a last question, if the property is not set it will used the default one (Global category) ?
  • I think you will need the parent path as well. For instance when I look at the path of some category in my site (via category administration) I see:
    /__SYSTEM__/Modules/Global/Site
    Where Site is the actual category name. And since I'm using the Global category for this category within News, the global category path is also needed.
    When opening the category registry, you will see the path of the property you are using for a cerain module. So in the example above in my News I use the global category.
    That is what this part:

    Code

    $rootCat = CategoryUtil::getCategoryByID($catregistry[$prop]);
    $cat = CategoryUtil::getCategoryByPath($rootCat['path'].'/'.$cat);

    does. It finds the rootcategorypath by means of the property name, which you know already and then adds that to the requested catname for the full path. The full path is then fed to getCategoryByPath and gives the category as object.

    --
    campertoday.nl, Module development, Dutch Zikula Community
  • Yes I confirm with getCategoryByPath we must use the full path.

    Thanks a lot for your help erik, all is working fine like I want now! icon_smile
  • 0 users

This list is based on users active over the last 60 minutes.