Class AdminPageFramework_FieldType
The base class for the users to create their custom field types.
When a framework user implements a custom field type into his/her work, this class may be extended to create a field definition class.
Creating a Custom Field Type
- Decide a unique field type slug that is going to be set in field definition
arrays consisting of alphabets and underscores, such as
my_custom_field_type
. For example, theautocomplete
custom field type uses the slugautocomplete
and the date&time picker custom field type usesdate_time
. This sulg is the one the user sets in field definition arrays to thetype
argument. - Define a PHP class that extends the
AdminPageFramework_FieldType
abstract class.- Extend the
AdminPageFramework_FieldType
class.class MyCustomFieldType extends AdminPageFramework_FieldType { // your code that defines the field type goes here }
- Set the field type slug decided in the above step such as
my_custom_field_type
to the$aFieldTypeSlugs
property.public $aFieldTypeSlugs = array( 'my_custom_field_type', );
- Define the default key-pairs of the field arguments. When you define a
field, you create a definition array like this.
array( 'field_id' => 'my_field_id', 'type' => 'my_custom_field_type', 'your_cusotm_key' => ..., 'another_cusotm_key' => ..., )
You can accept custom arguments by defining the key-value pairs in the$aDefaultKeys
property array.Example
protected $aDefaultKeys = array( 'label_min_width' => 80, 'attributes' => array( 'size' => 10, 'maxlength' => 400, ), 'label' => array( 'url' => 'URL', 'title' => 'Title', // Add more as you need ), );
- (optional) Write additional code of procedural subroutines in the
setUp()
method. ThesetUp()
method is performed when the field type definition is parsed by the framework.Example
protected function setUp() { wp_enqueue_script( 'jquery-ui-datepicker' ); }
- (optional) Add assets like scripts and styles with
getEnqueuingScripts()
,getEnqueuingStyles()
.Example
protected function getEnqueuingScripts() { return array( array( 'src' => dirname( __FILE__ ) . '/js/datetimepicker-option-handler.js', ), ); } protected function getEnqueuingStyles() { return array( dirname( __FILE__ ) . '/css/jquery-ui-1.10.3.min.css', ); }
- (optional) Add inline scripts and styles with
getScripts()
,getStyles()
.Example
protected function getScripts() { return " jQuery( document ).ready( function(){ console.log( 'debugging: loaded' ); }); " . PHP_EOL; } protected function getStyles() { return ".admin-page-framework-input-label-container.my_custom_field_type { padding-right: 2em;' }"; }
- Construct the output HTML structure with the passed
$aField
field definition array in thegetField()
method.Example
protected function getField( $aField ) { return $aField['before_label'] . $aField['before_input'] . "<div class='repeatable-field-buttons'></div>" // the repeatable field buttons . $this->_getInputs( $aField ) . $aField['after_input'] . $aField['after_label']; } private function _getInputs( $aField ) { $_aOutput = array(); foreach( ( array ) $aField['label'] as $_sSlug => $_sLabel ) { $_aAttributes = isset( $aField['attributes'][ $_sSlug ] ) && is_array( $aField['attributes'][ $_sSlug ] ) ? $aField['attributes'][ $_sSlug ] + $aField['attributes'] : $aField['attributes']; $_aAttributes = array( 'tyle' => 'text', 'name' => "{$_aAttributes['name']}[{$_sSlug}]", 'id' => "{$aField['input_id']}_{$_sSlug}", 'value' => isset( $aField['attributes']['value'][ $_sSlug ] ) ? $aField['attributes']['value'][ $_sSlug ] : '', ) + $_aAttributes; $_aOutput[] = "<div class='admin-page-framework-input-label-container my_custom_field_type'>" . "<label for='{$aField['input_id']}_{$_sSlug}'>" . "<span class='admin-page-framework-input-label-string' style='min-width:" . $aField['label_min_width'] . "px;'>" . $_sLabel . "</span>" . PHP_EOL . "<input " . $this->getAttributes( $_aAttributes ) . " />" . "</label>" . "</div>"; } return implode( PHP_EOL, $_aOutput ); }
- Extend the
- Instantiate the field type class by passing the instantiated class name of the framework class. See the below section to see how to include a custom field type.
Including a Custom Field Type
- Include the definition file and instantiate the class in the script
(plugin,theme etc.).
// pass the PHP class name that extends the framework's class to the first parameter. new MyCustomFieldType( 'MY_FRAMEWORK_CLASS_NAME' );
- Define fields with the custom field type with the
addSettingFields()
method in the framework extending class.$this->addSettingFields( array( 'field_id' => 'my_field_id', 'section_id' => 'my_section_id', 'type' => 'my_custom_field_type', // <-- here put the field type slug '...' => '...' ) );
Package: AdminPageFramework\Common\Form\FieldType
Since: 2.1.5
Since: 3.0.0 Changed the name from
AdminPageFramework_CustomFieldType
to
AdminPageFramework_FieldType
.Remark: The user will extend this class to define their custom field types.
Located at factory/_common/form/field_type/_common/_abstract/AdminPageFramework_FieldType.php
Methods summary
protected
|
#
construct( )
The user constructor. When the user defines a field type, they may use this instead of the real constructor so that they don't have to care about the internal parameters. Since
3.1.3
Remark
The user will override this method in their class definition.
OverridesAdminPageFramework_FieldType_Base::construct |
protected
|
#
setUp( )
Loads the field type necessary components. This method is triggered when a field definition array that calls this field type is parsed. Since
3.0.0
Remark
The user will override this method in their class definition.
|
protected
|
#
getScripts( )
Returns the JavaScript output inside the
Since
3.0.0
Remark
The user will override this method in their class definition.
|
protected
|
#
getIEStyles( )
Returns the CSS output specific to Internet Explorer inside the
Since
3.0.0
Remark
The user will override this method in their class definition.
|
protected
|
#
getStyles( )
Returns the field type specific CSS output inside the
Since
3.0.0
Remark
The user will override this method in their class definition.
|
protected
|
#
getField( mixed $aField )
Returns the field output. Since
3.0.0
Remark
The user will override this method in their class definition.
|
protected
|
#
getEnqueuingScripts( )
Returns an array holding the urls of enqueuing scripts. The returning array should consist of all numeric keys. Each element can be either a string( the url or the path of the source file) or an array of custom argument. Custom Argument Array
Examplesprotected function getEnqueuingScripts() { return array( array( // if you need to set a dependency, pass as a custom argument array. 'src' => dirname( __FILE__ ) . '/asset/my_script.js', // path or url 'dependencies' => array( 'jquery' ) ), dirname( __FILE__ ) . '/asset/my_another.js', // a string value of the target path or url will work as well. ); } Remark
The user will override this method in their class definition.
|
protected
|
#
getEnqueuingStyles( )
Returns an array holding the urls of enqueuing styles. The returning array should consist of all numeric keys. Each element can be either a string( the url or the path of the source file) or an array of custom argument. Custom Argument Array
Examplesprotected function getEnqueuingStyles() { return array( dirname( __FILE__ ) . '/asset/my_style.css', array( 'src' => dirname( __FILE__ ) . '/asset/my_style2.css', 'handle_id' => 'my_style2', ), ); } Remark
The user will override this method in their class definition.
|
protected
|
#
doOnFieldRegistration( mixed $aField )
Called when the given field of this field type is registered. Since
3.5.0
3.5.1 Removed a type hint in the first parameter. Remark
The user will override this method in their class definition.
|
Magic methods summary
Properties summary
public
array
|
$aFieldTypeSlugs |
#
Defines the field type slugs used for this field type. The slug is used for the type key in a field definition array. $this->addSettingFields( array( 'section_id' => '...', 'type' => 'my_filed_type_slug', // <--- THIS PART 'field_id' => '...', 'title' => '...', ) ); Examplepublic $aFieldTypeSlugs = array( 'my_field_type_slug', 'alternative_field_type_slug' ); |
protected
array
|
$aDefaultKeys |
#
Defines the default key-values of this field type. The users will set the values to the defined keys and if not set, the value
set in this property array will take effect. The merged array of the user's
field definition array and this property array will be passed to the first
parameter of the $this->addSettingFields( array( 'section_id' => '...', // built-in key 'type' => '...', // built-in key 'field_id' => '...', // built-in key 'my_custom_key' => 'the default value for this key', // <-- THIS PART 'another_custom)key' => 'the default value for this key', // <-- THIS PART ) ); Example$aDefaultKeys = array( 'my_custom_key' => 'my default value', 'attributes' => array( 'size' => 30, 'maxlength' => 400, ), ); Remark
$_aDefaultKeys defined by the system internally holds shared default
key-values defined in the base class.
|
Related
Direct known subclasses
AdminPageFramework_FieldType_checkbox
,
AdminPageFramework_FieldType_color
,
AdminPageFramework_FieldType_textarea
,
AdminPageFramework_FieldType_hidden
,
AdminPageFramework_FieldType_image
,
AdminPageFramework_FieldType_radio
,
AdminPageFramework_FieldType_select
,
AdminPageFramework_FieldType_submit
,
AdminPageFramework_FieldType_system
,
AdminPageFramework_FieldType_table
,
AdminPageFramework_FieldType_text
Indirect known subclasses
AdminPageFramework_FieldType_contact
,
AdminPageFramework_FieldType_export
,
AdminPageFramework_FieldType_taxonomy
,
AdminPageFramework_FieldType_file
,
AdminPageFramework_FieldType_import
,
AdminPageFramework_FieldType_inline_mixed
,
AdminPageFramework_FieldType_media
,
AdminPageFramework_FieldType_number
,
AdminPageFramework_FieldType_posttype
,
AdminPageFramework_FieldType_section_title
,
AdminPageFramework_FieldType_size