You are here

function flexiform_field_table_pre_render in Flexiform 7

Pre-render callback for flexiform_field_table elements.

1 string reference to 'flexiform_field_table_pre_render'
flexiform_element_info in ./flexiform.module
Implements hook_element_info().

File

./flexiform.admin.inc, line 1672
Model type editing UI.

Code

function flexiform_field_table_pre_render($elements) {
  $js_settings = array();

  // For each region, build the tree structure from the weight and parenting
  // data contained in the flat form structure, to determine row order and
  // indentation.
  $regions = $elements['#regions'];
  $tree = array(
    '' => array(
      'name' => '',
      'children' => array(),
    ),
  );
  $trees = array_fill_keys(array_keys($regions), $tree);
  $parents = array();
  $list = drupal_map_assoc(element_children($elements));

  // Iterate on rows until we can build a known tree path for all of them.
  while ($list) {
    foreach ($list as $name) {
      $row =& $elements[$name];
      $parent = $row['parent_wrapper']['parent']['#value'];

      // Proceed if parent is known.
      if (empty($parent) || isset($parents[$parent])) {

        // Grab parent, and remove the row from the next iteration.
        $parents[$name] = $parent ? array_merge($parents[$parent], array(
          $parent,
        )) : array();
        unset($list[$name]);

        // Determine the region for the row.
        $function = $row['#region_callback'];
        $region_name = $function($row);

        // Add the element in the tree.
        $target =& $trees[$region_name][''];
        foreach ($parents[$name] as $key) {
          $target =& $target['children'][$key];
        }
        $target['children'][$name] = array(
          'name' => $name,
          'weight' => $row['weight']['#value'],
        );

        // Add tabledrag indentation to the first row cell.
        if ($depth = count($parents[$name])) {
          $cell = current(element_children($row));
          $row[$cell]['#prefix'] = theme('indentation', array(
            'size' => $depth,
          )) . (isset($row[$cell]['#prefix']) ? $row[$cell]['#prefix'] : '');
        }

        // Add row id and associate JS settings.
        $id = drupal_html_class($name);
        $row['#attributes']['id'] = $id;
        if (isset($row['#js_settings'])) {
          $row['#js_settings'] += array(
            'rowHandler' => $row['#row_type'],
            'name' => $name,
            'region' => $region_name,
          );
          $js_settings[$id] = $row['#js_settings'];
        }
      }
    }
  }

  // Determine rendering order from the tree structure.
  foreach ($regions as $region_name => $region) {
    $elements['#regions'][$region_name]['rows_order'] = array_reduce($trees[$region_name], '_flexiform_field_form_reduce_order');
  }
  $elements['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array(
      'fieldUIRowsData' => $js_settings,
    ),
  );
  return $elements;
}