You are here

function draggableviews_view_draggabletable_form in DraggableViews 7

Same name and namespace in other branches
  1. 6.3 draggableviews.module \draggableviews_view_draggabletable_form()
  2. 6 draggableviews.inc \draggableviews_view_draggabletable_form()
  3. 6.2 draggableviews.inc \draggableviews_view_draggabletable_form()

Build the form

1 string reference to 'draggableviews_view_draggabletable_form'
draggableviews_forms in ./draggableviews.module
Implements hook_forms().

File

./draggableviews.module, line 92
Draggableviews module provides a style plugin for views. With this plugin rows become draggable and can be organized in complex structures.

Code

function draggableviews_view_draggabletable_form($form, $form_state, $style_plugin) {
  $view = $style_plugin->view;
  $options = $view->style_plugin->options;
  $form['submit'] = array(
    '#type' => 'submit',
    '#name' => 'save_order',
    '#value' => t(isset($options['draggableviews_button_text']) ? $options['draggableviews_button_text'] : t('Save order')),
  );
  if ($options['draggableviews_ajax']) {
    $form['submit']['#ajax'] = array(
      'callback' => 'draggableviews_view_draggabletable_form_ajax',
    );
  }
  $form['#submit'] = array(
    'draggableviews_view_draggabletable_form_submit',
  );

  // Use pager information and all exposed input data to build the query.
  $query = 'page=' . (!empty($_GET['page']) ? $_GET['page'] : '');
  if (isset($view->exposed_input)) {
    foreach ($view->exposed_input as $filter => $data) {
      if ($filter == 'order') {
        continue;
      }
      if (is_array($data)) {
        foreach ($data as $key1 => $value1) {
          if (is_array($value1)) {
            foreach ($value1 as $key2 => $value2) {
              $query .= '&' . $filter . '[' . $key1 . '][' . $key2 . ']=' . $value2;
            }
          }
          else {
            $query .= '&' . $filter . '[' . $key1 . ']=' . $value1;
          }
        }
      }
      else {
        $query .= '&' . $filter . '=' . $data;
      }
    }
  }
  $form['#theme'] = 'draggableviews_view_draggabletable_form';
  $info =& $view->draggableviews_info;
  foreach ($view->result as $i => $result) {
    $nid = $result->{$view->base_field};

    // This for loop should prevent copy->paste'ing code.
    foreach (array(
      'hierarchy',
      'order',
    ) as $modus) {
      if (isset($info[$modus])) {
        if ($modus == 'hierarchy') {
          $value = $info['nodes'][$nid]['parent'];

          // Get the field that should be rendered.
          $field =& $info['hierarchy']['field'];
        }
        else {

          // Get depth of current field.
          $depth = isset($info['nodes'][$nid]['depth']) ? $info['nodes'][$nid]['depth'] : 0;
          $value = $info['nodes'][$nid]['order'][$depth];

          // Get the field that should be rendered.
          $field =& $info['order']['field'];
        }

        // Get the form element.
        $form_element = $field['handler']
          ->get_form_element($value, array(
          'field_name' => $field['field_name'] . '_' . $nid,
          'class' => array(
            $field['field_name'],
          ),
        ));

        // Render the new form element. The field_ prefix prevents a possible conflict
        // with the hidden_nid field.
        $form['draggableviews_form_elements'][$i]['field_' . $field['field_name']] = $form_element;
      }
    }
    if (isset($info['hierarchy'])) {
      $form['draggableviews_form_elements'][$i]['hidden_nid'] = array(
        '#type' => 'hidden',
        '#value' => $nid,
        '#attributes' => array(
          'class' => 'hidden_nid',
        ),
      );
    }
  }
  return $form;
}