View source
<?php
namespace Drupal\form_builder;
abstract class ElementBase implements ElementInterface {
protected $form;
protected $params;
protected $element;
protected $loader;
public function __construct($form, $params, &$element, $loader) {
$this->form = $form;
$this->params = $params;
$this->element =& $element;
$this->loader = $loader;
}
protected function addPreRender($element) {
if (isset($element['#type']) && (!isset($element['#pre_render']) || !in_array('form_builder_pre_render', $element['#pre_render']))) {
$element['#pre_render'] = array_merge(element_info_property($element['#type'], '#pre_render', array()), array(
'form_builder_pre_render',
));
}
return $element;
}
public function render() {
return $this
->addPreRender($this->element);
}
public function getProperties() {
$return = array();
$properties = $this->form
->getProperties();
foreach ($this->params['properties'] as $name) {
if (isset($properties[$name])) {
$return[$name] = $properties[$name];
}
}
return $return;
}
protected function setProperty($property, $value) {
if ($value === '' || is_null($value)) {
unset($this->element['#' . $property]);
}
else {
$this->element['#' . $property] = $value;
}
}
public function getSaveableProperties() {
return $this->params['properties'];
}
public function configurationSubmit(&$form, &$form_state) {
foreach ($this
->getProperties() as $property) {
$property
->submit($form, $form_state);
}
$saveable = $this
->getSaveableProperties();
foreach ($form_state['values'] as $property => $value) {
if (in_array($property, $saveable, TRUE)) {
$this
->setProperty($property, $value);
}
}
}
public function title() {
return $this->element['#title'];
}
public function parentId() {
return $this->element['#form_builder']['parent_id'];
}
public function key() {
return $this->element['#key'];
}
}