Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtarleton/52941b676eb4ada9b290 to your computer and use it in GitHub Desktop.
Save jtarleton/52941b676eb4ada9b290 to your computer and use it in GitHub Desktop.
Drupal Form Builder class using ArrayAccess and Builder pattern
<?php
class DrupalFormBuilder implements ArrayAccess
{
private $data = array();
public function __construct(array $fieldsets = array('fs1'=>'fs1')) {
foreach($fieldsets as $key=>$fieldset){
$this->addFieldset($fieldset);
}
}
public static function get(){
$called=get_called_class();
return new $called();
}
public function toArray(){
return $this->data;
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->data[] = $value;
}
else {
$this->data[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetUnset($offset) {
unset($this->data[$offset]);
}
public function offsetGet($offset) {
return isset($this->data[$offset])
? $this->data[$offset]
: null;
}
public function addTextfield($key, $fs = 'fs1'){
$this->data[$fs][$key] = array(
'#type' => 'textfield',
'#title' => $key,
);
return $this;
}
public function addItem($key, $fs = 'fs1'){
$this->data[$fs][$key] = array(
'#type' => 'item',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addSelect($key, $fs = 'fs1', array $opts = array('Option 1'=>'Option 1')){
$this->data[$fs][$key] = array(
'#type' => 'select',
'#options' => $opts,
'#default_value' => array('Option 1'=>'Option 1'),
'#empty_option' => '- Select -',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addCheckbox($key,$fs = 'fs1'){
$this->data[$fs][$key] = array(
'#type' => 'checkbox',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addCheckboxes($key, $fs = 'fs1', array $opts = array('Option 1'=>'Option 1','Option 2'=>'Option 2','Option 3'=>'Option 3')){
$this->data[$fs][$key] = array(
'#type' => 'checkboxes',
'#options' => $opts,
'#default_value' => array('Option 1'=>'Option 1'),
'#empty_option' => '- Select -',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addRadio($key, $fs = 'fs1', array $opts = array('Option 1'=>'Option 1')){
$this->data[$fs][$key] = array(
'#type' => 'radio',
'#options' => $opts,
'#default_value' => array('Option 1'=>'Option 1'),
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
protected function addFieldset($key){
$this->data[$key] = array(
'#collapsible' => false,
'#title' => t($key),
'#type' => 'fieldset',
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addFile($key , $fs = 'fs1'){
$this->data[$fs][$key] = array(
'#type' => 'file',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addTextarea($key){
$this->data[$fs]['fs1'][$key] = array(
'#type' => 'textarea',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addPassword($key, $fs = 'fs1'){
$this->data[$fs][$key] = array(
'#type' => 'password',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addPasswordConfirm($key, $fs = 'fs1'){
$this->data[$fs][$key] = array(
'#type' => 'password_confirm',
'#title' => $key,
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addButton($key, $fs = 'fs1'){
$this->data[$fs][$key] = array(
'#type' => 'button',
'#value' => ucfirst($key),
'#prefix' => '',
'#suffix' => ''
);
return $this;
}
public function addSubmit($key){
$this->data[$key] = array(
'#type' => 'submit',
'#value' => ucfirst($key),
);
return $this;
}
}
@jtarleton
Copy link
Author

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment