You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -11,48 +11,55 @@ All the category data is held in the Categories table; you just have specify a f
11
11
## Add a field to capture the category
12
12
Where you allow users to add new records or edit existing records you'll want to extend the form to capture the associated category. You can add something like what's below to the XML file for adding or editing your component records:
Provided you call the `name` of the field the same as the name of your database column Joomla will map the data from the submitted form through to the database table for you.
27
29
28
30
## Add entries in the administrator submenu
29
31
You should add an entry in your component's submenu to enable administrators to manage the category data. You can do this within the administrator `<submenu>` tags in your component manifest XML file:
On the administrator Categories form it's usual to display for each category record the number of your component records which have Published / Unpublished / Archived / Trashed state. To implement this `com_categories` boots your component and calls the `countItems()` function (defined in Joomla\CMS\Categories\CategoryServiceInterface) on your Extension class instance. You can either provide the `countItems()` function directly or (if your category field is called `catid`) use the Joomla\CMS\Categories\CategoryServiceTrait and override
@@ -79,123 +86,129 @@ The preferred way of getting access to the Categories object is by instantiating
79
86
80
87
The services/provider.php file below is for a component `com_example`, with namespace `Mycompany\Component\Example` (as defined in the xml manifest file). The lines relating to obtaining the CategoryFactory have been commented to explain what's going on.
81
88
82
-
<?php
89
+
```php
90
+
<?php
83
91
84
-
defined('_JEXEC') or die;
92
+
defined('_JEXEC') or die;
85
93
86
-
use Joomla\CMS\Dispatcher\ComponentDispatcherFactoryInterface;
87
-
use Joomla\CMS\Extension\ComponentInterface;
88
-
use Joomla\CMS\Extension\Service\Provider\ComponentDispatcherFactory as ComponentDispatcherFactoryServiceProvider;
89
-
use Joomla\CMS\Extension\Service\Provider\CategoryFactory as CategoryFactorServiceProvider;
90
-
use Joomla\CMS\Extension\Service\Provider\MVCFactory as MVCFactoryServiceProvider;
91
-
use Joomla\CMS\Extension\Service\Provider\RouterFactory as RouterFactoryServiceProvider;
92
-
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
93
-
use Joomla\DI\Container;
94
-
use Joomla\DI\ServiceProviderInterface;
95
-
use Joomla\CMS\Categories\CategoryFactoryInterface;
96
-
use Joomla\CMS\Component\Router\RouterFactoryInterface;
97
-
use Mycompany\Component\Example\Administrator\Extension\ExampleComponent;
98
-
use Joomla\Database\DatabaseInterface;
94
+
use Joomla\CMS\Dispatcher\ComponentDispatcherFactoryInterface;
95
+
use Joomla\CMS\Extension\ComponentInterface;
96
+
use Joomla\CMS\Extension\Service\Provider\ComponentDispatcherFactory as ComponentDispatcherFactoryServiceProvider;
97
+
use Joomla\CMS\Extension\Service\Provider\CategoryFactory as CategoryFactorServiceProvider;
98
+
use Joomla\CMS\Extension\Service\Provider\MVCFactory as MVCFactoryServiceProvider;
99
+
use Joomla\CMS\Extension\Service\Provider\RouterFactory as RouterFactoryServiceProvider;
100
+
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
101
+
use Joomla\DI\Container;
102
+
use Joomla\DI\ServiceProviderInterface;
103
+
use Joomla\CMS\Categories\CategoryFactoryInterface;
104
+
use Joomla\CMS\Component\Router\RouterFactoryInterface;
105
+
use Mycompany\Component\Example\Administrator\Extension\ExampleComponent;
106
+
use Joomla\Database\DatabaseInterface;
99
107
100
108
101
-
return new class implements ServiceProviderInterface {
109
+
return new class implements ServiceProviderInterface {
110
+
111
+
public function register(Container $container): void
112
+
{
102
113
103
-
public function register(Container $container): void
104
-
{
105
-
106
-
/* The line below will call register() in libraries/src/Extension/Service/Provider/CategoryFactory.php
107
-
* That function will create an entry in our component's child DIC with:
The `CategoryServiceTrait` which is used is what contains the function `setCategoryFactory` which was used in services/provider.php to save the returned CategoryFactory. The function saves it in an instance variable `$categoryFactory`.
173
184
174
185
Whenever Joomla wants to execute the component it runs the services/provider.php file (which creates the Extension instance) and then calls `boot()` on this Extension object. The code in `boot()` above accesses the stored CategoryFactory and calls `createCategory()` on it to create the `Categories` instance. The result of this is stored in a static variable which can be accessed anywhere in the component using
175
186
176
-
$categories = ExampleComponent::$categories;
187
+
`$categories = ExampleComponent::$categories;``
177
188
178
189
The CategoryFactory `createCategory()` code will use the namespace which was passed into it (originally in services/provider.php above) and will try to instantiate a class <namespace>\Site\Service\Category, raising an exception if it does not exist. So you need to provide a file within your site area src/Service/Category.php similar to the following:
In this way the array of options for the [Categories API](https://manual.joomla.org/docs/using-core-functions/categories/using-categories-api) are defined. (You can pass these options as a parameter to the `createCategory()` call but you still have to provide your Category.php class file).
201
214
@@ -213,30 +226,36 @@ There are some other changes you'll also need to implement.
to create the 2 categories menuitems in the administrator submenu for your component.
220
235
221
236
### Administrator edit item
222
237
In the administrator edit product xml form (where you define each product, and its associated size and price categories) your fields for capturing the categories must distinguish between them, eg
and similarly for any filter fields in `filter_products.xml` for filtering the list of products shown in the administrator products form.
228
245
229
246
### Category Summary.
230
-
In your Extension class you'll need to provide the `countItems()` function which is in the trait Joomla\CMS\Categories\CategoryServiceTrait. `com_categories` will call this function passing in the category `section``, and if you're using `ContentHelper::countRelations()` to provide the summary then you'll need to set the appropiate category id field for each section – eg 'catid1' for section 'size' and 'catid2' for section 'price'. An example is below.
231
-
232
-
public function countItems(array $items, string $section)
In your Extension class you'll need to provide the `countItems()` function which is in the trait `Joomla\CMS\Categories\CategoryServiceTrait`. `com_categories` will call this function passing in the category `section`, and if you're using `ContentHelper::countRelations()` to provide the summary then you'll need to set the appropiate category id field for each section – eg 'catid1' for section 'size' and 'catid2' for section 'price'. An example is below.
248
+
249
+
```php
250
+
public function countItems(array $items, string $section)
0 commit comments