You are here

public function FlexiformBuilderFlexiform::form in Flexiform 7

Build the form for this flexiform.

Overrides FlexiformBuilder::form

File

includes/builder/flexiform.builder.inc, line 119
Contains the FlexiformBuilderFlexiform Class

Class

FlexiformBuilderFlexiform
Flexiform Builder Class for the complex flexiforms.

Code

public function form($form, &$form_state) {
  $form = parent::form($form, $form_state);

  // Set up our form bits
  if (!isset($form['#parents'])) {
    $form['#parents'] = array();
  }

  // Get the flexiform_state
  $flexiform_state =& $this
    ->getFlexiformState($form, $form_state);

  // Get the form entities and store the manager in the flexiform state.
  $form['#flexiform_entities'] = $this
    ->getEntityManager($flexiform_state)
    ->getEntities();
  $this
    ->setFlexiformState($form, $form_state);

  // Iterate over all elements.
  foreach ($this->flexiform->elements as $element_namespace => $settings) {
    $element = FlexiformElement::getElement($this->flexiform, $element_namespace);

    // Don't show form elements for entities that don't exist.
    if (!$this
      ->getFormEntity($element
      ->getEntityNamespace(), $flexiform_state)) {
      watchdog('flexiform', 'The "@element" element\'s entity is not gettable, so nothing to show for it on the form.', array(
        '@element' => $element
          ->getElementNamespace(),
      ), WATCHDOG_WARNING);
      continue;
    }

    // Make sure this element has the correct parents.
    array_push($form['#parents'], $element
      ->getEntityNamespace());
    $form += $element
      ->form($form, $form_state, $this
      ->getFormEntity($element
      ->getEntityNamespace(), $flexiform_state));
    array_pop($form['#parents']);
  }
  $this
    ->invoke($form, $form_state);
  return $form;
}