Actions

Difference between revisions of "Developer Area/Core Subsystems/Custom Profile Fields"

From Mahara Wiki

< Developer Area‎ | Core Subsystems
 
Line 1: Line 1:
 
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:
 
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:
  
<nowiki><?php
+
<code><?php
 
 
 
defined('INTERNAL') || die();
 
defined('INTERNAL') || die();
 
+
class PluginArtefactInternalLocal extends PluginArtefactInternal {}</code>
class PluginArtefactInternalLocal extends PluginArtefactInternal {
 
 
 
}</nowiki>
 
  
 
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:  
 
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:  
  
  <nowiki><?php
+
  <code><?php
 
 
 
defined('INTERNAL') || die();
 
defined('INTERNAL') || die();
 
 
class PluginArtefactInternalLocal extends PluginArtefactInternal {
 
class PluginArtefactInternalLocal extends PluginArtefactInternal {
 
 
     public static function get_artefact_types() {
 
     public static function get_artefact_types() {
 
         return array(
 
         return array(
Line 23: Line 16:
 
         );
 
         );
 
     }
 
     }
 
 
     public static function get_profile_artefact_types() {
 
     public static function get_profile_artefact_types() {
 
         return array(
 
         return array(
Line 30: Line 22:
 
         );
 
         );
 
     }
 
     }
 
 
     public static function get_contactinfo_artefact_types() {
 
     public static function get_contactinfo_artefact_types() {
 
         return array(
 
         return array(
Line 37: Line 28:
 
         );
 
         );
 
     }
 
     }
}</nowiki>
+
}</code>
  
 
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.
 
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.
Line 43: Line 34:
  
  
  <nowiki>class ArtefactTypeProfileLocal extends ArtefactTypeProfile {
+
  <code>class ArtefactTypeProfileLocal extends ArtefactTypeProfile {
 
     public static function get_all_fields() {
 
     public static function get_all_fields() {
 
         $out = array(
 
         $out = array(
Line 51: Line 42:
 
         return $out;
 
         return $out;
 
     }
 
     }
 
 
     public static function get_desired_fields() {
 
     public static function get_desired_fields() {
 
         return array('about' => array(2 => 'rainbowcolour'));
 
         return array('about' => array(2 => 'rainbowcolour'));
 
     }
 
     }
}</nowiki>
+
}</code>
  
 
For each new profile artefact types we have we need to set a specific ArtefactTypeProfileField and how they will save / display the information.
 
For each new profile artefact types we have we need to set a specific ArtefactTypeProfileField and how they will save / display the information.
Line 62: Line 52:
  
  
  <nowiki>class ArtefactTypePetname extends ArtefactTypeProfileField {}</nowiki>
+
  <code>class ArtefactTypePetname extends ArtefactTypeProfileField {}</code>
  
 
When adding a field that is not type 'text' we need to set what options will allowed to be used, eg:
 
When adding a field that is not type 'text' we need to set what options will allowed to be used, eg:
  
  <nowiki>class ArtefactTypeRainbowcolour extends ArtefactTypeProfileField {
+
  <code>class ArtefactTypeRainbowcolour extends ArtefactTypeProfileField {
 
 
 
     public function render_self($options) {
 
     public function render_self($options) {
 
         $colours = self::getoptions();
 
         $colours = self::getoptions();
 
         return array('html' => $colours[$this->title], 'javascript' => null);
 
         return array('html' => $colours[$this->title], 'javascript' => null);
 
     }
 
     }
 
 
     /**
 
     /**
 
     * Returns an assoc array of colours for use with the "select" form
 
     * Returns an assoc array of colours for use with the "select" form
Line 95: Line 83:
 
         return $colours;
 
         return $colours;
 
     }
 
     }
 
 
     function defaultoption() {
 
     function defaultoption() {
 
         // We want blue to be default value
 
         // We want blue to be default value
 
         return 'blue';
 
         return 'blue';
 
     }
 
     }
 
 
     function format_result($raw) {
 
     function format_result($raw) {
 
         // We want to adjust the viewing of the saved value to uppercase the first letter
 
         // We want to adjust the viewing of the saved value to uppercase the first letter
 
         return ucfirst($raw);
 
         return ucfirst($raw);
 
     }
 
     }
 
 
     function usersearch_column_structure() {
 
     function usersearch_column_structure() {
 
         // We want to add the ability to view this info in the Admin -> People list page
 
         // We want to add the ability to view this info in the Admin -> People list page
 
         return array('name' => 'rainbowcolour', 'sort' => true, 'template' => 'searchrainbowcolourcol//umn.tpl');
 
         return array('name' => 'rainbowcolour', 'sort' => true, 'template' => 'searchrainbowcolourcol//umn.tpl');
 
     }
 
     }
}</nowiki>
+
}</code>
  
 
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
 
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
Line 116: Line 101:
 
With the content
 
With the content
  
  <nowiki>
+
  <code>
 
{if $r.rainbowcolour}
 
{if $r.rainbowcolour}
 
   {ucfirst($r.rainbowcolour)}
 
   {ucfirst($r.rainbowcolour)}
{/if}</nowiki>
+
{/if}</code>

Latest revision as of 17:22, 12 June 2020

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' => 'searchrainbowcolourcol//umn.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}