You are here

class FlexiformBuilderFlexiform in Flexiform 7

Flexiform Builder Class for the complex flexiforms.

Hierarchy

Expanded class hierarchy of FlexiformBuilderFlexiform

6 string references to 'FlexiformBuilderFlexiform'
flexiform_element_group_flexiform_build_alter in flexiform_element_group/flexiform_element_group.module
Implements hook_flexiform_build_alter().
flexiform_element_group_form_flexiform_form_alter in flexiform_element_group/flexiform_element_group.module
Implements hook_form_flexiform_form_alter().
flexiform_flexiform_builder_info in ./flexiform.flexiform.inc
Implements hook_flexiform_builder_info().
flexiform_schema in ./flexiform.install
Implements hook_schema().
flexiform_webform_field_create_instance in flexiform_webform/flexiform_webform.module
Implements hook_field_create_instance().

... See full list

File

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

View source
class FlexiformBuilderFlexiform extends FlexiformBuilder {

  /**
   * The Entity Manager for this form.
   * @var FlexiformFormEntityManagerInterface
   */
  protected $entityManager;

  /**
   * The Flexiform State for this form.
   * @var Array
   */
  protected $flexiformState;

  /**
   * Get the entity manager.
   *
   * @return FlexiformFormEntityManagerInterface
   *   The entity manager for this form builder.
   */
  public function getEntityManager(&$state = array()) {
    if (!empty($this->entityManager)) {
      return $this->entityManager;
    }
    if (!empty($state['flexiform_entity_manager'])) {
      $this->entityManager = $state['flexiform_entity_manager'];
      return $this->entityManager;
    }
    $this->entityManager = new FlexiformFormEntityManagerDefault($this->flexiform->entities, $this);
    $this->entityManager
      ->setBaseEntity($this->base_entity);
    $state['flexiform_entity_manager'] = $this->entityManager;
    return $this->entityManager;
  }

  /**
   * Set a entity callback.
   */
  public function registerEntityCallback($type, $namespace, $callback, $extra = array(), &$state = array()) {
    $this
      ->getEntityManager($state)
      ->registerCallback($type, $namespace, $callback, $extra);
  }

  /**
   * Get a form entity.
   *
   * @param $namespace
   *  The namespace of the entity required.
   *
   * @return
   *  The entity with that namespace.
   */
  public function getFormEntity($namespace, &$state = array()) {
    return $this
      ->getEntityManager($state)
      ->getEntity($namespace);
  }

  /**
   * Get the flexiform state.
   *
   * @param array $form
   *   The form array being built. Must have a parents key.
   * @param array $form_state
   *   The form_State array.
   *
   * @return array
   *   The flexiform state.
   */
  public function &getFlexiformState($form, &$form_state) {

    // Shortcut if we've already got the array.
    if (is_array($this->flexiformState)) {
      return $this->flexiformState;
    }

    // Set up a place to safely store information in the form state.
    if (empty($form_state['flexiform_state'])) {
      $form_state['flexiform_state'] = array();
    }

    // Get the flexiform state for this particular form. This is a bit involved
    // as we need to be able to nest flexiforms.
    $this->flexiformState = drupal_array_get_nested_value($form_state['flexiform_state'], $form['#parents']);
    if (empty($this->flexiformState)) {
      $this->flexiformState = array();
    }
    return $this->flexiformState;
  }

  /**
   * Set the flexiform state.
   *
   * @param array $form
   *   The form array being built. Must have a parents key.
   * @param array $form_state
   *   The form_State array.
   */
  public function setFlexiformState($form, &$form_state) {

    // Set up a place to safely store information in the form state.
    if (empty($form_state['flexiform_state'])) {
      $form_state['flexiform_state'] = array();
    }

    // Set the flexiform state.
    drupal_array_set_nested_value($form_state['flexiform_state'], $form['#parents'], $this->flexiformState);
  }

  /**
   * Build the form for this flexiform.
   */
  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;
  }

  /**
   * Validate the input for the form.
   */
  public function formValidate($form, &$form_state) {

    // Get the flexiform state for this particular form.
    $flexiform_state =& $this
      ->getFlexiformState($form, $form_state);
    foreach (element_children($form) as $element_namespace) {
      if (empty($form[$element_namespace]['#flexiform_element'])) {
        continue;
      }
      $element = $form[$element_namespace]['#flexiform_element'];
      $entity = $this
        ->getFormEntity($element
        ->getEntityNamespace(), $flexiform_state);
      $element
        ->formValidate($form, $form_state, $entity);
    }
    $this
      ->invoke($form, $form_state, 'validate');
  }

  /**
   * Submit the form.
   */
  public function formSubmit($form, &$form_state, $save = TRUE) {
    $flexiform_state =& $this
      ->getFlexiformState($form, $form_state);
    foreach (element_children($form) as $element_namespace) {
      if (empty($form[$element_namespace]['#flexiform_element'])) {
        continue;
      }
      $element = $form[$element_namespace]['#flexiform_element'];
      $entity = $this
        ->getFormEntity($element
        ->getEntityNamespace(), $flexiform_state);
      $element
        ->formSubmit($form, $form_state, $entity);
    }
    $this
      ->invoke($form, $form_state, 'submit');
    if ($save) {
      $this
        ->save($this
        ->getEntityManager($flexiform_state), $form_state);
    }
    $this
      ->setFlexiformState($form, $form_state);
  }

  /**
   * Save everything.
   *
   * @param \FlexiformFormEntityManagerInterface $entity_manager
   * @param array $form_state
   */
  public function save(FlexiformFormEntityManagerInterface $entity_manager, $form_state = array()) {
    $entity_manager
      ->saveEntities();
    if (module_exists('rules')) {
      $base_entity_wrapper = entity_metadata_wrapper($this->flexiform->base_entity, $entity_manager
        ->getEntity('base_entity'));
      $args = array(
        'base_entity' => $base_entity_wrapper,
      );

      // All Arguments: Rules needs nulls as null rather than false.
      $all_args = $entity_manager
        ->getEntities();
      foreach ($all_args as &$ent) {
        if (empty($ent)) {
          $ent = NULL;
        }
      }

      // Fire the most specific rules first and track back to the less specific.
      $rule_name = 'flexiform_submit_' . $this->flexiform->form;
      drupal_alter('flexiform_rules_arguments', $all_args, $form_state, $rule_name);
      rules_invoke_event_by_args('flexiform_submit_' . $this->flexiform->form, $all_args);
      $rule_name = 'flexiform_group_submit_' . $this->flexiform->form_group;
      drupal_alter('flexiform_rules_arguments', $args, $form_state, $rule_name);
      rules_invoke_event_by_args('flexiform_group_submit_' . $this->flexiform->form_group, $args);
      $rule_name = 'flexiform_any_submit';
      drupal_alter('flexiform_rules_arguments', $args, $form_state, $rule_name);
      rules_invoke_event_by_args('flexiform_any_submit', $args);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function supportsComponent($component) {
    return in_array($component, array(
      'entities',
      'elements',
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlexiformBuilder::$base_entity protected property The base entity.
FlexiformBuilder::$base_entity_type protected property The base entity type.
FlexiformBuilder::$flexiform protected property The flexiform object.
FlexiformBuilder::formSubmitRedirect public function Submit the form and set up the redirect.
FlexiformBuilder::getCtoolsContexts public function Get an array of ctools context for the flexiform.
FlexiformBuilder::getCtoolsSubstitutionsList public function Build a list of possible ctools substitutions.
FlexiformBuilder::getFlexiform public function Get the Flexiform.
FlexiformBuilder::invoke public function Invoke hooks to alter the build of this form.
FlexiformBuilder::replaceCtoolsSubstitutions public function Replace ctools substitutions with their values.
FlexiformBuilder::__construct public function Construct the Flexiform Builder.
FlexiformBuilderFlexiform::$entityManager protected property The Entity Manager for this form.
FlexiformBuilderFlexiform::$flexiformState protected property The Flexiform State for this form.
FlexiformBuilderFlexiform::form public function Build the form for this flexiform. Overrides FlexiformBuilder::form
FlexiformBuilderFlexiform::formSubmit public function Submit the form. Overrides FlexiformBuilder::formSubmit
FlexiformBuilderFlexiform::formValidate public function Validate the input for the form. Overrides FlexiformBuilder::formValidate
FlexiformBuilderFlexiform::getEntityManager public function Get the entity manager.
FlexiformBuilderFlexiform::getFlexiformState public function Get the flexiform state.
FlexiformBuilderFlexiform::getFormEntity public function Get a form entity. Overrides FlexiformBuilder::getFormEntity
FlexiformBuilderFlexiform::registerEntityCallback public function Set a entity callback.
FlexiformBuilderFlexiform::save public function Save everything.
FlexiformBuilderFlexiform::setFlexiformState public function Set the flexiform state.
FlexiformBuilderFlexiform::supportsComponent public function Support Components. Overrides FlexiformBuilder::supportsComponent