User

Difference between revisions of "Garkell"

From Joomla! Documentation

(Initial creation - just learning)
 
 
Line 20: Line 20:
  
  
class CityField extends FormField {
+
class CityField extends FormField  
+
{
 
protected $type = 'City';
 
protected $type = 'City';
  

Latest revision as of 18:29, 13 February 2021

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>';
	}
}