You are here

class FormBuilderElementBase in Form Builder 7

Hierarchy

Expanded class hierarchy of FormBuilderElementBase

1 string reference to 'FormBuilderElementBase'
FormBuilderLoader::loadFormTypeInfo in ./form_builder.classes.inc

File

./form_builder.classes.inc, line 801

View source
class FormBuilderElementBase implements FormBuilderElementInterface {
  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;
  }

  /**
   * Add our pre-render function to the element-array.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    return $this
      ->addPreRender($this->element);
  }

  /**
   * {@inheritdoc}
   */
  public function getProperties() {
    $return = array();
    $properties = $this->form
      ->getProperties();

    // Order of the properties is important because of a form-API bug.
    // See: https://www.drupal.org/node/990218.
    foreach ($this->params['properties'] as $name) {
      if (isset($properties[$name])) {
        $return[$name] = $properties[$name];
      }
    }
    return $return;
  }

  /**
   * Set the value of a property.
   *
   * This method must update the $element for rendering as well as for
   * later storage.
   *
   * @param string $property
   *   Key of the property.
   * @param mixed $value
   *   New value for the property.
   */
  protected function setProperty($property, $value) {

    // Remove empty properties entirely.
    if ($value === '' || is_null($value)) {
      unset($this->element['#' . $property]);
    }
    else {
      $this->element['#' . $property] = $value;
    }
  }
  public function getSaveableProperties() {
    return $this->params['properties'];
  }

  /**
   * {@inheritdoc}
   */
  public function configurationForm($form, &$form_state) {
    $form['#_edit_element'] = $this->element;
    foreach ($this
      ->getProperties() as $property) {
      $form = array_merge($form, $property
        ->form($form_state, $this));
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function configurationSubmit(&$form, &$form_state) {

    // Allow each property to do any necessary submission handling.
    foreach ($this
      ->getProperties() as $property) {
      $property
        ->submit($form, $form_state);
    }

    // Update the field according to the settings in $form_state['values'].
    $saveable = $this
      ->getSaveableProperties();
    foreach ($form_state['values'] as $property => $value) {
      if (in_array($property, $saveable, TRUE)) {
        $this
          ->setProperty($property, $value);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function title() {
    return $this->element['#title'];
  }
  public function parentId() {
    return $this->element['#form_builder']['parent_id'];
  }
  public function key() {
    return $this->element['#key'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormBuilderElementBase::$element protected property
FormBuilderElementBase::$form protected property
FormBuilderElementBase::$loader protected property
FormBuilderElementBase::$params protected property
FormBuilderElementBase::addPreRender protected function Add our pre-render function to the element-array.
FormBuilderElementBase::configurationForm public function Get the configuration form for this element. Overrides FormBuilderElementInterface::configurationForm 1
FormBuilderElementBase::configurationSubmit public function Submit handler for the configuration form. Overrides FormBuilderElementInterface::configurationSubmit 1
FormBuilderElementBase::getProperties public function Get a list of properties available for this element. Overrides FormBuilderElementInterface::getProperties
FormBuilderElementBase::getSaveableProperties public function Get a list of properties that are supported in any way by this element. Overrides FormBuilderElementInterface::getSaveableProperties
FormBuilderElementBase::key public function
FormBuilderElementBase::parentId public function
FormBuilderElementBase::render public function (Re-)Render an element. Overrides FormBuilderElementInterface::render 1
FormBuilderElementBase::setProperty protected function Set the value of a property. 1
FormBuilderElementBase::title public function Get a human-readable title for this form element. Overrides FormBuilderElementInterface::title 1
FormBuilderElementBase::__construct public function Overrides FormBuilderElementInterface::__construct