1
extension title alias
com_content Uncategorised uncategorised
com_banners Sample Data-Banners sample-data-banners
com_banners Sample Data-Newsfeeds sample-data-newsfeeds
com_newsfeeds Sample Data-Contact sample-data-contact
com_contact Joomla! joomla
com_productdeal.products products Category one products-category

If I try to print categories for Content, Banners, or Contact then getInstance function returns data correctly.

$categories = Categories::getInstance('Content');

When trying to print the categories of another custom extension, it returns nothing.

$categories = Categories::getInstance('productdeal.products');

Why can't I use custom extensions with Categories::getInstance()?

1 Answer 1

0

To use categories in your component, you need to register the 'CategoryFactory' in the 'provider' file

Example from administrator/component/com_content/services/provider.php

public function register(Container $container)
    {
        $container->set(AssociationExtensionInterface::class, new AssociationsHelper());
        $container->registerServiceProvider(new CategoryFactory('\\Joomla\\Component\\Content'));
        $container->registerServiceProvider(new MVCFactory('\\Joomla\\Component\\Content'));
        $container->registerServiceProvider(new ComponentDispatcherFactory('\\Joomla\\Component\\Content'));
        $container->registerServiceProvider(new RouterFactory('\\Joomla\\Component\\Content'));

        $container->set(
            ComponentInterface::class,
            function (Container $container) {
                $component = new ContentComponent($container->get(ComponentDispatcherFactoryInterface::class));

                $component->setRegistry($container->get(Registry::class));
                $component->setMVCFactory($container->get(MVCFactoryInterface::class));
                $component->setCategoryFactory($container->get(CategoryFactoryInterface::class));
                $component->setAssociationExtension($container->get(AssociationExtensionInterface::class));
                $component->setRouterFactory($container->get(RouterFactoryInterface::class));

                return $component;
            }
        );
    }

Also take a look at the file: administrator/component/com_content/src/Extension/ContentComponent.php

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.