You are here

function theme_webform_components_form in Webform 7.3

Same name and namespace in other branches
  1. 5.2 webform_components.inc \theme_webform_components_form()
  2. 6.3 includes/webform.components.inc \theme_webform_components_form()
  3. 6.2 webform_components.inc \theme_webform_components_form()
  4. 7.4 includes/webform.components.inc \theme_webform_components_form()

Theme the node components form. Use a table to organize the components.

Parameters

$form: The form array.

Return value

Formatted HTML form, ready for display.

File

includes/webform.components.inc, line 158
Webform module component handling.

Code

function theme_webform_components_form($variables) {
  $form = $variables['form'];
  $form['components']['#attached']['library'][] = array(
    'webform',
    'admin',
  );

  // TODO: Attach these. See http://drupal.org/node/732022.
  drupal_add_tabledrag('webform-components', 'order', 'sibling', 'webform-weight');
  drupal_add_tabledrag('webform-components', 'match', 'parent', 'webform-pid', 'webform-pid', 'webform-cid');
  $node = $form['#node'];
  $header = array(
    t('Label'),
    t('Type'),
    t('Value'),
    t('Mandatory'),
    t('Weight'),
    array(
      'data' => t('Operations'),
      'colspan' => 3,
    ),
  );
  $rows = array();

  // Add a row containing form elements for a new item.
  unset($form['add']['name']['#title'], $form['add_type']['#description']);
  $form['add']['name']['#attributes']['rel'] = t('New component name');
  $form['add']['name']['#attributes']['class'] = array(
    'webform-default-value',
  );
  $form['add']['cid']['#attributes']['class'] = array(
    'webform-cid',
  );
  $form['add']['pid']['#attributes']['class'] = array(
    'webform-pid',
  );
  $form['add']['weight']['#attributes']['class'] = array(
    'webform-weight',
  );
  $row_data = array(
    drupal_render($form['add']['name']),
    drupal_render($form['add']['type']),
    '',
    drupal_render($form['add']['mandatory']),
    drupal_render($form['add']['cid']) . drupal_render($form['add']['pid']) . drupal_render($form['add']['weight']),
    array(
      'colspan' => 3,
      'data' => drupal_render($form['add']['add']),
    ),
  );
  $add_form = array(
    'data' => $row_data,
    'class' => array(
      'draggable',
      'webform-add-form',
    ),
  );
  $form_rendered = FALSE;
  if (!empty($node->webform['components'])) {
    $component_tree = array();
    $page_count = 1;
    _webform_components_tree_build($node->webform['components'], $component_tree, 0, $page_count);
    $component_tree = _webform_components_tree_sort($component_tree);

    /**
     * Build the table rows.
     */
    function _webform_components_form_rows($node, $cid, $component, $level, &$form, &$rows, &$add_form) {

      // Create presentable values.
      if (drupal_strlen($component['value']) > 30) {
        $component['value'] = drupal_substr($component['value'], 0, 30);
        $component['value'] .= '...';
      }
      $component['value'] = check_plain($component['value']);

      // Remove individual titles from the mandatory and weight fields.
      unset($form['components'][$cid]['mandatory']['#title']);
      unset($form['components'][$cid]['pid']['#title']);
      unset($form['components'][$cid]['weight']['#title']);

      // Add special classes for weight and parent fields.
      $form['components'][$cid]['cid']['#attributes']['class'] = array(
        'webform-cid',
      );
      $form['components'][$cid]['pid']['#attributes']['class'] = array(
        'webform-pid',
      );
      $form['components'][$cid]['weight']['#attributes']['class'] = array(
        'webform-weight',
      );

      // Build indentation for this row.
      $indents = '';
      for ($n = 1; $n <= $level; $n++) {
        $indents .= '<div class="indentation">&nbsp;</div>';
      }

      // Add each component to a table row.
      $row_data = array(
        $indents . filter_xss($component['name']),
        $form['add']['type']['#options'][$component['type']],
        $component['value'] == '' ? '-' : $component['value'],
        drupal_render($form['components'][$cid]['mandatory']),
        drupal_render($form['components'][$cid]['cid']) . drupal_render($form['components'][$cid]['pid']) . drupal_render($form['components'][$cid]['weight']),
        l(t('Edit'), 'node/' . $node->nid . '/webform/components/' . $cid, array(
          'query' => drupal_get_destination(),
        )),
        l(t('Clone'), 'node/' . $node->nid . '/webform/components/' . $cid . '/clone', array(
          'query' => drupal_get_destination(),
        )),
        l(t('Delete'), 'node/' . $node->nid . '/webform/components/' . $cid . '/delete', array(
          'query' => drupal_get_destination(),
        )),
      );
      $row_class = array(
        'draggable',
      );
      if (!webform_component_feature($component['type'], 'group')) {
        $row_class[] = 'tabledrag-leaf';
      }
      if ($component['type'] == 'pagebreak') {
        $row_class[] = 'tabledrag-root';
        $row_class[] = 'webform-pagebreak';
        $row_data[0] = array(
          'class' => array(
            'webform-pagebreak',
          ),
          'data' => $row_data[0],
        );
      }
      $rows[] = array(
        'data' => $row_data,
        'class' => $row_class,
      );
      if (isset($component['children']) && is_array($component['children'])) {
        foreach ($component['children'] as $cid => $component) {
          _webform_components_form_rows($node, $cid, $component, $level + 1, $form, $rows, $add_form);
        }
      }

      // Add the add form if this was the last edited component.
      if (isset($_GET['cid']) && $component['cid'] == $_GET['cid'] && $add_form) {
        $add_form['data'][0] = $indents . $add_form['data'][0];
        $rows[] = $add_form;
        $add_form = FALSE;
      }
    }
    foreach ($component_tree['children'] as $cid => $component) {
      _webform_components_form_rows($node, $cid, $component, 0, $form, $rows, $add_form);
    }
  }
  else {
    $rows[] = array(
      array(
        'data' => t('No Components, add a component below.'),
        'colspan' => 9,
      ),
    );
  }

  // Append the add form if not already printed.
  if ($add_form) {
    $rows[] = $add_form;
  }
  $output = '';
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => 'webform-components',
    ),
  ));
  $output .= drupal_render_children($form);
  return $output;
}