I am implementing a field which needs to present its information as a sortable table and I thought I would try this module to see if it would speed things up. While it mostly works, the field elements end up in the table and there is the hid/show weights text, the table rows are not sortable.

I started from http://drupal.org/node/1876710 to produce this

function reorder_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
	//drupal_set_message("Field:<pre>" . print_r($instance, TRUE) . '</pre>');
  $value = isset($items[$delta]['reorder']) ? $items[$delta]['reorder'] : array();
  drupal_set_message("Items:<pre>" . print_r($value, TRUE) . '</pre>');
  $i = 1;
  foreach ( $value as $index => $item ) {
  	if ( empty($value[$index]['weight']) ) {
  		$value[$index]['weight'] = $i++;
  	}
  	if ( empty($value[$index]['hidden']) ) {
  		$value[$index]['hidden'] = 0;
  	}
  }

	$settings = $instance['settings'];
	
	if ( $settings['allow_hiding_groups'] ) {
		$header = array(t('Weight'), t('Hide'), t('Label'));
	}
	else {
		$header = array(t('Weight'), t('Label'));
	}
	/*
        (
            [allow_label_edit] => 1
            [allow_hiding_groups] => 1
            [field_groups] => Array
                (
                    [group_bio] => group_bio
                    [group_1] => group_1
                    [group_2] => group_2
                    [group_3] => group_3
                    [group_parent] => 0
                )

            [user_register_form] => 
        )
   */   

  $element['reorder'] = array(
    '#type' => 'table',
    '#header' => $header,
    '#empty' => t('There are no field groups in the default display'),
    // TableDrag: Each array value is a list of callback arguments for
    // drupal_add_tabledrag(). The #id of the table is automatically prepended;
    // if there is none, an HTML ID is auto-generated.
    '#tabledrag' => array(
      array('order', 'sibling', 'reorder-field-weight'),
    ),
  );
  $element['reorder']['#delta'] = $delta;
  
	$group = field_group_info_groups($instance['entity_type'], $instance['bundle'], 'default');
	$i = 1;
	if ( !empty($group) ) {
		foreach ( $group as $index => $item ) {
			if ( empty($settings['field_groups'][$index]) ) {
				continue;
			}
			
			if ( empty($value[$index]) ) {
				$default = array('weight' => $i, 'label' => $item->label, 'hidden' => 0);
			}
			else {
				$default = $value[$index];
			}
			
	    // TableDrag: Mark the table row as draggable.
	    $element['reorder'][$index]['#attributes']['class'][] = 'draggable';
	    // TableDrag: Sort the table row according to its existing/configured weight.
	    $element['reorder'][$index]['#weight'] = $default['weight'];
	    
	    $element['reorder'][$index]['weight'] = array(
	      '#type' => 'weight',
	      '#title' => t('Weight for @title', array('@title' => $item->label)),
	      '#title_display' => 'invisible',
	      '#default_value' => $default['weight'],
	      // Classify the weight element for #tabledrag.
	      '#attributes' => array('class' => array('reorder-field-weight')),
	    );
	    $i++;
	        
			if ( $settings['allow_hiding_groups'] ) {
		    $element['reorder'][$index]['hidden'] = array(
		    	'#type' => 'checkbox',
		    	'#default_value' => $default['hidden'],
		    );				
			}
			/*
			else {
		    $element['reorder'][$index]['hidden'] = array(
		    	'#type' => 'value',
		    	'#value' => 0,
		    );
		  }				
			*/
			if ( $settings['allow_label_edit'] ) {
		    $element['reorder'][$index]['label'] = array(
		    	'#type' => 'textfield',
		    	'#default_value' => $default['label'],
		    );
			}
			else {
		    $element['reorder'][$index]['label'] = array(
		    	'#markup' => $default['label'],
		    );
			}

    
		}
	}
	
  return $element;
}

Should this work and if so, have I made an obvious error in trying to use the widget?

In case it helps, the purpose of the field is to allow user reordering of field groups. It gets the list of field groups and place each group in a table row.

Comments

nevets’s picture

Status: Active » Closed (works as designed)

Figured it out. I had placed the weight field first for convenience but it can not be first since the weight column is hidden and since the drag functionality is added to the first column placing the weight functionality first also hides the drag functionality.

Moved the weight field last and it works.