You are here

function configuration_sync_data_form in Configuration Management 6

Based on a form api array, make automatic modifications to the form submission data.

What this can do:

1. Check all #options and try and match the data to them. Example of why this is needed:

XML syntax <types>page</types> OR <types> <type>page</type> <type>story</type> </types>

Original Results array( 'types' => 'page' ) OR array( 'types' => array( 'type' => array('page', 'store') ) )

Desired Results array( 'types' => array( 'page' => 'page' ) ) OR array( 'types' => array( 'page' => 'page', 'story' => 'story' ) )

However in some cases the first example (<types>page</types>) would be in the form <type>page</type> without the <types> parent and no need for the associative array. It would just be array('type' => 'page').

Based off of the form api #options, #tree, and #multiple values, it should be possible to convert any of the above xml objects to the right form api object.

1 call to configuration_sync_data_form()
configuration_drupal_execute in ./configuration.module
Do the actual drupal execute on an action

File

./configuration.module, line 1118
Provide a unified method for defining site configurations abstracted from their data format. Various data formats should be supported via a plugin architecture such as XML, YAML, JSON, PHP

Code

function configuration_sync_data_form(&$data, $form_id, $form_state, $params = null) {
  unset($form_state['values']);
  $args = array(
    $form_id,
    &$form_state,
  );
  if (is_array($params)) {
    $args = array_merge($args, $params);
  }

  // Get the fully built fapi object
  $form = call_user_func_array('drupal_retrieve_form', $args);
  drupal_prepare_form($form_id, $form, $form_state);
  $form = form_builder($form_id, $form, $form_state);
  $context = configuration_build_context($data);
  $form = configuration_build_context($form);

  // TODO Should we remove build data that does not exist in the form object?
  if ($form_matches = configuration_fetch('//#options', $form)) {
    for ($i = 0; isset($form_matches[$i]); $i++) {
      $form_match =& $form_matches[$i];

      // Check if we have this data
      $path = '/' . implode('/', $form_matches[$i]->parent->item['#parents']);
      if ($data_matches = configuration_fetch($path, $context)) {

        // Possibly manipulate our data to match these #options
        for ($j = 0; isset($data_matches[$j]); $j++) {
          $data_match =& $data_matches[$j];

          // Determine if this is a singular/scalar or arrayed element
          $scalar = false;
          if (!$form_match->parent->item['#tree'] || isset($form_match->parent->item['#multiple']) && !$form_match->parent->item['#multiple']) {
            $scalar = true;
          }

          // Setup the new value
          // TODO Actually implement the update vs overwrite functionality as opposed to the 100% update (true)
          if (true && !$scalar && !empty($form_match->parent->item['#default_value'])) {

            // Default values are not key=>key arrays like the post values. They are
            // indexed arrays. The below should catch an indexed array as well as a singular
            // scalar default value and turn it into a key=>key array
            $values = (array) $form_match->parent->item['#default_value'];
            $values = array_combine($values, $values);
          }
          else {
            $values = array();
          }
          foreach ((array) $data_match->item as $value) {

            // If the supplied value matches an options label, use that option
            if ($key = array_search($value, $form_match->item)) {
              $values[$key] = $key;
            }
            else {
              if (array_key_exists($value, $form_match->item)) {
                $values[$value] = $value;
              }
            }
          }

          // If we are not on a multiple/tree form, the value should be singular/not an array
          if ($scalar) {
            $data_match->item = reset($values);
          }
          else {
            $data_match->item = $values;
          }
        }
      }
    }
  }
}