Adding Custom Fields On Extension Install
Custom Fields may be useful in many Joomla extensions. They can be added by calling a custom script file upon an extension install or uninstall. The script will use Joomla's database driver to manually insert new custom field records.
I created this code to go along with my guide on loading custom article fields within layout files, however, it could apply to any type of extension.
Setup
Begin by navigating to your extension's XML manifest file and defining a scriptfile element if one does not exist already. We usually use "script.php" as the filename.
In my "joomStarter" template, the top of the manifest file looks like this now:
1<extension type="template" client="site" method="upgrade">
2 <name>joomstarter</name>
3 <version>1.30</version>
4 <creationDate>June 2023</creationDate>
5 <author>Your Name</author>
6 <authorEmail>Your Email</authorEmail>
7 <copyright>(C) 2023 KG</copyright>
8 <description>TPL_J4STARTER_XML_DESCRIPTION</description>
9 <inheritable>1</inheritable>
10 <scriptfile>script.php</scriptfile>
11
12 -- continued --
Then create your script.php file if one does not exist. It must contain a class named "yourextensionInstallerScript" - this is required or it won't load on install/update/uninstall.
The InstallerScript class can use 3 functions on install, uninstall, and update. Other functions are available but I'm not covering them here.
1<?php
2
3defined ( '_JEXEC' ) or die;
4
5class myextensionInstallerScript
6{
7
8 public function install($parent)
9 {
10
11 echo '<p>Extension installed/p>';
12 }
13
14 public function uninstall($parent)
15 {
16
17 echo '<p>Extension uninstalled</p>';
18
19 }
20
21 public function update($parent)
22 {
23
24 echo '<p>Extension Updated</p>';
25
26
27 }
28
29}
Copy/paste the above code and remember to change the class name to reflect whatever the extension's name is.
Load/Remove Custom Fields
Now we just need to add some script to define and insert the custom fields we need into the database. I would suggest manually creating the field yourself within Joomla's admin panel, and then copying the field information directly from phpMyAdmin or whatever database explorer you use. Some of the fields are formatted with JSON so this will make it easier to ensure everything is correct.
In this example, I create a pretty generic text field for the articles fields. If you are using fields outside articles, such as a custom user field, you'll need to change the context field accordingly.
You should provide a value for every property of the field listed below even if you don't think it's important for your use case - there are no default values for some of the required columns in the database table structure.
1<?php
2
3defined ( '_JEXEC' ) or die;
4use Joomla\CMS\Factory;
5
6//if using a template, you must prefix this with the template's name as defined in templateDetails.xml
7//if running from a component, you must prefix this with the component's name as defined in the component's manifest file, usually com_componentname
8//same applies to any other type of extension... myextensionInstallerScript, etc
9
10class joomstarterInstallerScript
11{
12
13 public function install($parent)
14 {
15
16 //get id of current user
17 $userid = Factory::getApplication()->getIdentity()->id;
18
19 // Create a new custom field
20 $field = new stdClass();
21 $field->context = 'com_content.article'; //change this to the context needed
22 $field->group_id = 0; //change this to the group id if needed. if you want to create a custom group you'll have to do that first and look up the new group id
23 $field->title = 'Your Field Title';
24 $field->name = 'your_field_name';
25 $field->label = 'Your Field Label';
26 $field->description = 'Fields Description Here';
27 $field->type = 'text';
28 $field->default_value = '';
29 $field->state = 1;
30 $field->required = 0;
31 $field->params=''; //in json format
32 $field->fieldparams=''; // in json format
33 //you'll probably be okay leaving the rest of the params alone
34 $field->access = 1;
35 $field->created_user_id = $userid;
36 $field->created_time = date('Y-m-d H:i:s');
37 $field->modified_user_id = $userid;
38 $field->modified_time = date('Y-m-d H:i:s');
39 $field->language = '*';
40
41 // Insert the new field in the database
42 $db = Factory::getContainer()->get('DatabaseDriver');
43 $db->insertObject('#__fields', $field, 'id');
44
45 echo '<p>Custom field created</p>';
46 }
47
48 public function uninstall($parent)
49 {
50
51 //remove custom field from database
52 $db = Factory::getContainer()->get('DatabaseDriver');
53 $query = $db->getQuery(true)
54 ->delete($db->quoteName('#__fields'))
55 ->where($db->quoteName('name') . ' = ' . $db->quote('your_field_name'));
56 $db->setQuery($query);
57 $db->execute();
58
59 echo '<p>Custom field removed</p>';
60
61 }
62
63 public function update($parent)
64 {
65
66 //You would need to do a version check here to see if the you need to create a custom field or not, or check if the custom field exists and change accordingly
67
68
69 }
70
71
72}
Now that the field's been created, I can see it under the article fields, and the option is available for each article.
You can learn how I access the values of my custom fields from within articles by reading the snippet on that.
Spotted a mistake? Select the sentence in the guide and choose “Suggest a fix”.
Related guides
Adding Custom Options to your Joomla Template
In this guide, you will learn how to add custom fields/options to the Joomla template you designed. This guide assumes you already know the basics of Joomla…
Adding Custom Joomla Module Chrome (Styles)
Module Chromes are added by creating a new php file for each chrome/style under your template's directory at…
Creating Joomla Content Plugins (With Examples)
In this guide, you will learn the structure of a basic Joomla Content Plugin. We'll explore several examples, so you can create your own content plugins from…

Comments
Found an error? instead, it reaches me faster.No comments yet. Questions and corrections are welcome.