You are here

public function FlexiformFCHandler::calculateElementCompleteness in Flexiform 7

Calculate if a given element is complete.

Return value

TRUE|FALSE|NULL True/False if the fields is complete/not complete. Null if the field is ignored.

1 call to FlexiformFCHandler::calculateElementCompleteness()
FlexiformFCHandler::getElementCompleteness in flexiform_webform/flexiform_webform.fc.inc
Get Element completeness.

File

flexiform_webform/flexiform_webform.fc.inc, line 97
Contains the Field Comlete handler for the FlexiformWebform Module.

Class

FlexiformFCHandler
@file Contains the Field Comlete handler for the FlexiformWebform Module.

Code

public function calculateElementCompleteness($flexiform, $element_namespace) {

  // If we already know that this is ignored then return NULL;
  if ($this
    ->elementIgnored($flexiform, $element_namespace)) {
    return NULL;
  }
  $element = FlexiformElement::getElement($flexiform, $element_namespace);
  $element_settings = $flexiform->elements[$element_namespace];
  $field = $element
    ->getField();
  $instance = $element
    ->getInstance();
  $settings = !empty($instance['settings']['fc']) ? $instance['settings']['fc'] : array();

  // Get Necessary information.
  $entity = $this->entities[$element_settings['entity_namespace']];
  $entity_type = $flexiform->entities[$element_settings['entity_namespace']]['entity_type'];
  $field_name = $field['field_name'];

  // If the entity is empty and the field isn't allowed to be empty then
  // obviously this form isn't complete.
  if (empty($entity)) {
    if (!empty($settings['fc_allow_empty'])) {
      return TRUE;
    }
    return FALSE;
  }

  // Go through the field items if any are "not empty" then
  // we count that as complete (a bit simplistic but okay).
  $field_items = field_get_items($entity_type, $entity, $field_name);
  if (empty($field_items)) {
    if (!empty($settings['fc_allow_empty'])) {

      // Complex fields can be set so that they optionally
      // have content, and if they don't they must be
      // counted as complete
      return TRUE;
    }
    return FALSE;
  }

  // Choose the right plugin for the field type.
  $plugin = fc_get_plugin($field['type']);
  if ($function = ctools_plugin_get_function($plugin, 'completeness check')) {

    // Process the field to determine whether it's complete, normally we
    // just compare the number of complete field_items with the cardinality
    // but other fields might be more complex (like matrix fields).
    $cardinality = ctools_plugin_get_function($plugin, 'cardinality check');
    return $cardinality($function, $field_items, $instance, $field);
  }
  return FALSE;
}