API methods of FLEXIcontent fields (events of plugin type flexicontent_fields))

  • Published by
    George Papadakis
  • Last modified
    17 April 2016
  • Up to date
    Yes
  • Profile concerned
    Developer
  • Concerns
    Plugins
  • Since Version
    1.5.6
  • Voting
    Average rating
    5 votes
    • 1
    • 2
    • 3
    • 4
    • 5
  • Favourites
    301 API methods of FLEXIcontent fields (events of plugin type flexicontent_fields)) /documentation/tutorials-english/70-developer-api-field-plugins/301-api-methods-of-flexicontent-fields.html

To create a FLEXIcontent field (a form field in item form and frontend views)
- you simply create joomla plugins that extend
a. either the Joomla plugin class JPlugin
b. or the class FCField

and these plugin reside in path like:

{jb_yellowbox}plugins/flexicontent_fields/my_field/my_field.php
plugins/flexicontent_fields/my_field/my_field.xml
{/jb_yellowbox}

List of the CLASS methods = (plugin events) triggered are:

Editing

{jb_brownbox}onDisplayField - editing HTML for item form
{/jb_brownbox}


Viewing

{jb_brownbox}onDisplayFieldValue - displaying HTML for viewing{/jb_brownbox}


Field value handling

{jb_brownbox}onBeforeSaveField - validating / changing field values, before saving them into DB
onBeforeDeleteField -
before deleting OLD field values from the DB
onAfterSaveField - after saving NEW field values into the DB
{/jb_brownbox}


Text-search / filtering for category view

{jb_brownbox}onIndexSearch - adding field value to search index of category view
getFiltered - finding item IDs that have a field value during search in category view
onDisplayFilter - create a filter (e.g. drop down selector) for filtering field value in category view{/jb_brownbox}


Text-search / filtering for search view

{jb_brownbox}onIndexAdvSearch - adding field value to search index of search view
getFilteredSearch
- finding item IDs that have a field value during search in search view
onAdvSearchDisplayFilter- create a filter (e.g. drop down selector) for filtering field value in search view{/jb_brownbox}


Following there is code of an empty field example,

  • usually you are better off to start by duplicating an existing FLEXIcontent field that better matches your purpose, read:
    How to Duplicate a Flexicontent Field to create a new Field Type


Field example

defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport('cms.plugin.plugin');
JLoader::register('FCField', JPATH_ADMINISTRATOR . '/components/com_flexicontent/helpers/fcfield/parentfield.php');

class plgFlexicontent_fieldsText extends FCField
{
	// ***********
	// CONSTRUCTOR
	// ***********
 
	function plgFlexicontent_fieldsText( &$subject, $params )
	{
		parent::__construct( $subject, $params );
		JPlugin::loadLanguage('plg_flexicontent_fields_text', JPATH_ADMINISTRATOR);
	}
 
 
 
	// *******************************************
	// DISPLAY methods, item form & frontend views
	// *******************************************
 
	// Method to create field's HTML display for item form
	function onDisplayField(&$field, &$item)
	{
		// execute the code only if the field type match the plugin type
		if($field->field_type != 'text') return;
 
		$field->label = JText::_($field->label);
		// ...
		$field->html = '...';
	}
 
 
	// Method to create field's HTML display for frontend views
	function onDisplayFieldValue(&$field, $item, $values=null, $prop='display')
	{
		// execute the code only if the field type match the plugin type
		if($field->field_type != 'text') return;
 
		$field->label = JText::_($field->label);
		//...
		$field->{$prop} = '...';
	}
 
 
 
	// **************************************************************
	// METHODS HANDLING before & after saving / deleting field events
	// **************************************************************
 
	// Method to handle field's values before they are saved into the DB
	function onBeforeSaveField( &$field, &$post, &$file, &$item )
	{
		// execute the code only if the field type match the plugin type
		if($field->field_type != 'text') return;
		if(!is_array($post) && !strlen($post)) return;
 
		// Make sure posted data is an array 
		$post = !is_array($post) ? array($post) : $post;
 
		// Reformat the posted data
		$newpost = array();
		foreach ($post as $n=>$v)
		{
			//...
		}
		$post = $newpost;
	}
 
 
	// Method to take any actions/cleanups needed after field's values are saved into the DB
	function onAfterSaveField( &$field, &$post, &$file, &$item ) {
	}
 
 
	// Method called just before the item is deleted to remove custom item data related to the field
	function onBeforeDeleteField(&$field, &$item) {
	}
 
 
 
	// *********************************
	// CATEGORY/SEARCH FILTERING METHODS
	// *********************************
 
	// Method to display a search filter for the advanced search view
	function onAdvSearchDisplayFilter(&$filter, $value='', $formName='searchForm')
	{
		if($filter->field_type != 'text') return;
 
		plgFlexicontent_fieldsText::onDisplayFilter($filter, $value, $formName);
	}
 
 
	// Method to display a category filter for the category view
	function onDisplayFilter(&$filter, $value='', $formName='adminForm')
	{
		// execute the code only if the field type match the plugin type
		if($filter->field_type != 'text') return;
 
		// ** some parameter shortcuts
		$label_filter 		= $filter->parameters->get( 'display_label_filter', 0 ) ;
		if ($label_filter == 2) $text_select = $filter->label; else $text_select = JText::_('FLEXI_ALL');
		// ...
		$filter->html = '...';
	}
 
 
 
 	// Method to get the active filter result (an array of item ids matching field filter, or subquery returning item ids)
	// This is for content lists e.g. category view, and not for search view
	function getFiltered(&$filter, $value, $return_sql=true)
	{
		if ( !in_array($filter->field_type, self::$field_types) ) return;
		
		return FlexicontentFields::getFiltered($filter, $value, $return_sql);
	}
	
	
 	// Method to get the active filter result (an array of item ids matching field filter, or subquery returning item ids)
	// This is for search view
	function getFilteredSearch(&$filter, $value, $return_sql=true)
	{
		if ( !in_array($filter->field_type, self::$field_types) ) return;
		
		$filter->isindexed = true;
		return FlexicontentFields::getFilteredSearch($filter, $value, $return_sql);
	}


	// *************************
	// SEARCH / INDEXING METHODS
	// *************************
	
	// Method to create (insert) advanced search index DB records for the field values
	function onIndexAdvSearch(&$field, &$post, &$item)
	{
		if ( !in_array($field->field_type, self::$field_types) ) return;
		if ( !$field->isadvsearch && !$field->isadvfilter ) return;
		
		// a. Each of the values of $values array will be added to the advanced search index as searchable text (column value)
		// b. Each of the indexes of $values will be added to the column 'value_id',
		//    and it is meant for fields that we want to be filterable via a drop-down select
		// c. If $values is null then only the column 'value' will be added to the search index after retrieving 
		//    the column value from table 'flexicontent_fields_item_relations' for current field / item pair will be used
		// 'required_properties' is meant for multi-property fields, do not add to search index if any of these is empty
		// 'search_properties'   contains property fields that should be added as text
		// 'properties_spacer'  is the spacer for the 'search_properties' text
		// 'filter_func' is the filtering function to apply to the final text
		FlexicontentFields::onIndexAdvSearch($field, $post, $item, $required_properties=array(), $search_properties=array(), $properties_spacer=' ', $filter_func='strip_tags');
		return true;
	}
	
	
	// Method to create basic search index (added as the property field->search)
	function onIndexSearch(&$field, &$post, &$item)
	{
		if ( !in_array($field->field_type, self::$field_types) ) return;
		if ( !$field->issearch ) return;
		
		// a. Each of the values of $values array will be added to the basic search index (one record per item)
		// b. If $values is null then the column value from table 'flexicontent_fields_item_relations' for current field / item pair will be used
		// 'required_properties' is meant for multi-property fields, do not add to search index if any of these is empty
		// 'search_properties'   contains property fields that should be added as text
		// 'properties_spacer'  is the spacer for the 'search_properties' text
		// 'filter_func' is the filtering function to apply to the final text
		FlexicontentFields::onIndexSearch($field, $post, $item, $required_properties=array(), $search_properties=array(), $properties_spacer=' ', $filter_func='strip_tags');
		return true;
	}

}