You are here

class Loader in Form Builder 7.2

This class is a wrapper around all the hooks used for getting pluigns.

Currently supported plugin-types are:

Hierarchy

  • class \Drupal\form_builder\Loader

Expanded class hierarchy of Loader

12 files declare their use of Loader
DefaultComponentsTest.php in modules/webform/tests/DefaultComponentsTest.php
ExamplesTest.php in examples/tests/ExamplesTest.php
FormTest.php in modules/webform/tests/FormTest.php
form_builder.admin.inc in includes/form_builder.admin.inc
form_builder.admin.inc Administrative interface for editing forms.
form_builder.api.inc in includes/form_builder.api.inc
form_builder.api.inc Universally used API functions within the Form builder module.

... See full list

File

src/Loader.php, line 13

Namespace

Drupal\form_builder
View source
class Loader {
  protected static $instance = NULL;
  protected $formTypeInfo;
  protected $paletteGroupInfo = array();
  protected $elementTypeInfo = array();
  protected $propertyInfo = array();
  protected $formCache = array();

  /**
   * Get a singleton-like class instance.
   */
  public static function instance() {
    if (!static::$instance) {
      static::$instance = new static();
    }
    return static::$instance;
  }
  public function __construct() {
    module_load_include('api.inc', 'form_builder', 'includes/form_builder');
    $this
      ->loadFormTypeInfo();
  }
  protected function loadFormTypeInfo() {
    $defaults = array(
      'class' => FormBase::class,
      'property class' => PropertyBase::class,
      'element class' => ElementBase::class,
    );
    $form_types = module_invoke_all('form_builder_form_types');
    foreach ($form_types as $form_type => &$info) {
      $info += $defaults;
    }
    drupal_alter('form_builder_form_types', $form_types);
    $this->formTypeInfo = $form_types;
  }
  public function getElementTypeInfo($form_type, $form_id) {
    if (!isset($this->elementTypeInfo[$form_type][$form_id])) {
      $element_types = module_invoke_all('form_builder_element_types', $form_type, $form_id);
      $groups = $this
        ->getPaletteGroupInfo($form_type, $form_id);

      // Add default values for undefined properties.
      foreach ($element_types as $key => &$type) {
        $type += array(
          'class' => $this->formTypeInfo[$form_type]['element class'],
          'configurable' => TRUE,
          'removable' => TRUE,
          'palette_group' => 'default',
          'properties' => array(),
        );
        $type += array(
          'addable' => $type['removable'] && isset($type['default']),
        );
        $type['unique'] = !empty($type['unique']);
        $type['palette_group'] = isset($groups[$type['palette_group']]) ? $type['palette_group'] : 'default';

        // All fields must support weight.
        if (!in_array('weight', $type['properties'])) {
          $type['properties'][] = 'weight';
        }

        // Update the default elements with some defaults.
        // Note that if a field is not removable, it doesn't have a default.
        $type['default'] += array(
          '#form_builder' => array(),
        );
        if ($type['addable']) {
          $type['default']['#form_builder'] += array(
            'element_type' => $key,
          );
          if ($type['unique']) {
            $type['default']['#form_builder']['element_id'] = $key;
          }
        }
      }

      // Sort fields by weight and title.
      uasort($element_types, '_form_builder_sort');
      drupal_alter('form_builder_element_types', $element_types, $form_type, $form_id);
      $this->elementTypeInfo[$form_type][$form_id] = $element_types;
    }
    return $this->elementTypeInfo[$form_type][$form_id];
  }
  public function getPaletteGroupInfo($form_type, $form_id, $reset = FALSE) {
    if (!isset($this->paletteGroupInfo[$form_type]) || $reset) {
      $this->paletteGroupInfo[$form_type] = module_invoke_all('form_builder_palette_groups', $form_type, $form_id);
    }
    return $this->paletteGroupInfo[$form_type];
  }
  public function getPropertyInfo($form_type, $reset = FALSE) {
    if (!isset($this->propertyInfo[$form_type]) || $reset) {

      // Don't use module_invoke_all here as it uses array_merge_recursive()
      // which creates sub-arrays for duplicate array keys.
      $properties = array();
      foreach (module_implements('form_builder_properties') as $module) {
        $new_properties = module_invoke($module, 'form_builder_properties', $form_type);
        $properties += $new_properties;
        foreach ($new_properties as $k => $v) {
          $properties[$k] = array_merge($properties[$k], $new_properties[$k]);
        }
      }
      drupal_alter('form_builder_properties', $properties, $form_type);
      $defaults['class'] = $this->formTypeInfo[$form_type]['property class'];
      foreach ($properties as $property => &$params) {
        $params += $defaults;
      }
      $this->propertyInfo[$form_type] = $properties;
    }
    return $this->propertyInfo[$form_type];
  }

  /**
   * Get a form object.
   */
  public function getForm($form_type, $form_id, $sid, $form = array()) {
    if (!isset($this->formTypeInfo[$form_type])) {
      return FALSE;
    }
    $info = $this->formTypeInfo[$form_type];
    $class = $info['class'];
    return new $class($form_type, $form_id, $sid, $info, $form);
  }

  /**
   * Load a form from storage.
   */
  public function fromStorage($form_type, $form_id, $sid = NULL) {
    if (!isset($this->formTypeInfo[$form_type])) {
      return FALSE;
    }
    $info = $this->formTypeInfo[$form_type];
    $class = $info['class'];
    return $class::loadFromStorage($form_type, $form_id, $sid, $info);
  }

  /**
   * Load a form from the form_builder_cache.
   */
  public function fromCache($form_type, $form_id, $sid = NULL, $reset = FALSE) {
    if ($reset) {
      $this->formCache = array();
    }
    if ($form_type && $form_id) {
      if (empty($this->formCache[$form_type][$form_id])) {
        $this->formCache[$form_type][$form_id] = FALSE;
        if (isset($this->formTypeInfo[$form_type])) {
          $info = $this->formTypeInfo[$form_type];
          $class = $info['class'];
          $sid = $sid ? $sid : session_id();
          if ($form = $class::load($form_type, $form_id, $sid, $info)) {
            $this->formCache[$form_type][$form_id] = $form;
          }
        }
      }
      return $this->formCache[$form_type][$form_id];
    }
    return NULL;
  }

  /**
   * Get element instance.
   */
  public function getElement($form_type, $form_id, $element_type, $form, &$element) {
    $infos = $this
      ->getElementTypeInfo($form_type, $form_id);
    $info = $infos[$element_type];
    $class = $info['class'];
    return new $class($form, $info, $element, $this);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Loader::$elementTypeInfo protected property
Loader::$formCache protected property
Loader::$formTypeInfo protected property
Loader::$instance protected static property
Loader::$paletteGroupInfo protected property
Loader::$propertyInfo protected property
Loader::fromCache public function Load a form from the form_builder_cache.
Loader::fromStorage public function Load a form from storage.
Loader::getElement public function Get element instance.
Loader::getElementTypeInfo public function
Loader::getForm public function Get a form object.
Loader::getPaletteGroupInfo public function
Loader::getPropertyInfo public function
Loader::instance public static function Get a singleton-like class instance.
Loader::loadFormTypeInfo protected function
Loader::__construct public function