Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
demonstrates using a plugin to add a custom form element that simply prints out any HTML
<?php
/**
* Plugin Name: PDB HTML Form Element
* Description: a general-purpose form element that simply displays HTML
*
*/
class PDb_html_form_element_example {
/**
* @var string element slug
*/
protected $name = 'html';
/**
* @var string title of the form element
*/
protected $title = 'HTML';
/**
* instantiates the class
*/
public function __construct()
{
// register the form element
add_filter( 'pdb-set_form_element_types', array($this, 'register_element') );
// build the element html in a form context
add_action( 'pdb-form_element_build_' . $this->name, array($this, 'element_build') );
// build the form element html in a non-form context
add_filter( 'pdb-before_display_form_element', array($this, 'element_value_display'), 10, 2 );
// hide the element from non-enabled shortcodes
add_filter( 'pdb-before_field_added_to_iterator', array($this, 'element_alter_field_list') );
}
/**
* adds the form element to the list of form elements
*
* @param array $elements
* @return html
*/
public function register_element( $elements )
{
$elements[$this->name] = $this->title;
return $elements;
}
/**
* supplies the form element HTML in a form context
*
* @param PDb_FormElement $field
* @return null
*/
public function element_build( $field )
{
$field->output = '<span class="html-element-container">' . $this->get_html( $field ) . '</span>';
}
/**
* defines the HTML to show in a non-form context
*
* @param string $value the raw value of the field
* @param PDb_Field_Item $field
* @return string HTML
*/
public function element_value_display( $value, $field )
{
// if the current field is our form element, provide the html
if ( $field->form_element === $this->name ) {
$value = '<span class="html-element-value">' . $this->get_html( $field ) . '</span>';
}
return $value;
}
/**
* removes the field from the list of field to show
*
* this works by changing the field type to "hidden" so it won't be shown in any
* module not in the list of enabled modules
*
* @param object $field current field object
* @return object the field object
*/
public function element_alter_field_list( $field )
{
/*
* if the current field is our form element and the current module is not one
* that has been enabled, hide the element
*/
if ( $field->form_element === $this->name && !in_array( $field->module, $this->get_enabled_modules( $field ) ) ) {
$field->form_element = 'hidden';
}
return $field;
}
/**
* provides the value in string form
*
* @param object $field the field object
* @return string
*/
private function get_html( $field )
{
// get the field definition
$field_def = Participants_Db::$fields[$field->name];
/*
* we go through all this because the values property could come in
* as an array, a serialized array or a string. By doing this we ensure
* the result will in all cases be a string.
*/
return current( (array) maybe_unserialize( $field_def->values ) );
}
/**
* supplies a list of modules where this form element is enabled
*
* @param object $field the current field object
* @return array of module names
*/
protected function get_enabled_modules( $field )
{
// determine the setting value ot use a default
$default = empty( $field->default ) ? 'signup' : $field->default;
// make the setting into an array of values
return explode( ',', str_replace( ' ', '', $default ) );
}
}
// instantiate the class so it will be active
new PDb_html_form_element_example();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment