Developer Area/Core Subsystems/Custom Profile Fields
From Mahara Wiki
< Developer Area | Core Subsystems
To extend the options of what can be recorded against and displayed for a person's profile you can add a PluginArtefactInternalLocal class at htdocs/local/lib/artefact_internal.php, like so:
<?php
defined('INTERNAL') || die();
class PluginArtefactInternalLocal extends PluginArtefactInternal {
}
This PluginArtefactInternalLocal class can install / list one or many new profile artefact type options as artefact types, profile types and contact info types. We need to set all three options for things to work correctly. So if we wanted to add two fields, petname, rainbowcolour to record a pet's name and favourite rainbow colour you'd need to do this:
<?php
defined('INTERNAL') || die();
class PluginArtefactInternalLocal extends PluginArtefactInternal {
public static function get_artefact_types() { return array( 'petname', 'rainbowcolour', ); }
public static function get_profile_artefact_types() { return array( 'petname', 'rainbowcolour', ); }
public static function get_contactinfo_artefact_types() { return array( 'petname', 'rainbowcolour', ); }
}
We also need to list them in an ArtefactTypeProfileLocal class that also lists what new artefact types are available to appear on the profile page and what form field type will be used to record the data.
If we want the rainbow colour field to appear on the second tab of the profile page we add the get_desired_fields() function and return an array of where we want the field to appear. So after the first class we need to add this class like so:
class ArtefactTypeProfileLocal extends ArtefactTypeProfile {
public static function get_all_fields() { $out = array( 'petname' => 'text', 'rainbowcolour' => 'select', ); return $out; }
public static function get_desired_fields() { return array('about' => array(2 => 'rainbowcolour')); }
}
For each new profile artefact types we have we need to set a specific ArtefactTypeProfileField and how they will save / display the information.
When adding a field that is type 'text' we can set the minimal for things to work, eg:
class ArtefactTypePetname extends ArtefactTypeProfileField {}
When adding a field that is not type 'text' we need to set what options will allowed to be used, eg:
class ArtefactTypeRainbowcolour extends ArtefactTypeProfileField {
public function render_self($options) { $colours = self::getoptions(); return array('html' => $colours[$this->title], 'javascript' => null); }
/** * Returns an assoc array of colours for use with the "select" form * element * * @return array Associative array of colour ids => colour names */ function getoptions() { static $colours; if (!empty($colours)) { return $colours; } $codes = array( 'red' => 'Red', 'orange' => 'Orange', 'yellow' => 'Yellow', 'green' => 'Green', 'blue' => 'Blue', 'indigo' => 'Indigo', 'violet' => 'Violet', ); return $colours; }
function defaultoption() { // We want blue to be default value return 'blue'; }
function format_result($raw) { // We want to adjust the viewing of the saved value to uppercase the first letter return ucfirst($raw); }
function usersearch_column_structure() { // We want to add the ability to view this info in the Admin -> People list page return array('name' => 'rainbowcolour', 'sort' => true, 'template' => 'searchrainbowcolourcolumn.tpl'); }
}
If you are wanting the usersearch_column_structure() option you will need to set up a template file in the htdocs/local/theme/templates/ directory eg htdocs/local/theme/templates/searchrainbowcolourcolumn.tpl
With the content
{if $r.rainbowcolour}
{ucfirst($r.rainbowcolour)}
{/if}