How to create a customized field value by executing PHP code

  • Published by
    George Papadakis
  • Last modified
    08 November 2013
  • Up to date
    Yes
  • Profile concerned
    Developer
  • Since Version
    1.5.6
  • Voting
    Average rating
    • 1
    • 2
    • 3
    • 4
    • 5
  • Favourites
    437 How to create a customized field value by executing PHP code /documentation/tutorials-english/70-developer-api-field-plugins/437-how-to-create-a-customized-field-value-by-executing-php-code.html

This is an example of how to create a new field type that automatically creates a customized field value by executing PHP code

  1. duplicate text field to create a new field type
    "text" field as type "myreference"
    How to Duplicate a Flexicontent Field to create a new Field Type
  2. modify plugin function
    onBeforeSaveField(...)

    to be

    // 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 ( !in_array($field->field_type, self::$field_types) ) return;
    	if ( !is_array($post) && !strlen($post) ) return;
    	
    	$post = array();
    	$post[0] = 'GMP'.$item->catid.'-'.$item->id;
    }


  3. Create a field of type 'myreference' and set it to be hidden from for both frontend/backend item forms (the parameter is at the left when edit the field configuration)