User

Garkell

From Joomla! Documentation

Revision as of 18:29, 13 February 2021 by Garkell (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Joomla 4 Custom FormFields

This will help with creating your own custom formfield for your component ready for Joomla 4.

An Example Custom Field Type

Suppose you're working on your component named com_phonebook and you want to define a field that contains cities. Create the file administrator/components/com_phonebook/src/Field/CityField.php and write something similar to the following:

<?php

namespace yourname\Component\Phonebook\Administrator\Field;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

use \Joomla\CMS\Factory;
use \Joomla\CMS\Form\FormField;
use \yourname\Component\Phonebook\Administrator\Helper\PhonebookHelper;


class CityField extends FormField 
{
	protected $type = 'City';

	// getLabel() left out

	public function getInput() {
		return '<select id="'.$this->id.'" name="'.$this->name.'">'.
		       '<option value="1" >New York</option>'.
		       '<option value="2" >Chicago</option>'.
		       '<option value="3" >San Francisco</option>'.
		       '</select>';
	}
}