? 818112.views_hierarchy.plugin-validation.patch
Index: views_hierarchy_plugin_style_hierarchy.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views_hierarchy/views_hierarchy_plugin_style_hierarchy.inc,v
retrieving revision 1.1
diff -u -p -r1.1 views_hierarchy_plugin_style_hierarchy.inc
--- views_hierarchy_plugin_style_hierarchy.inc	4 Jun 2010 11:13:26 -0000	1.1
+++ views_hierarchy_plugin_style_hierarchy.inc	6 Jun 2010 09:27:35 -0000
@@ -26,8 +26,18 @@ class views_hierarchy_plugin_style_hiera
    * The options form.
    */
   function options_form(&$form, &$form_state) {
+    $handlers = $this->display->handler->get_handlers('field');
+    if (empty($handlers)) {
+      $form['error_markup'] = array(
+        '#value' => t('You need at least one field before you can configure your hierarchy settings'),
+        '#prefix' => '<div class="error form-item description">',
+        '#suffix' => '</div>',
+      );
+      return;
+    }
+
     parent::options_form($form, $form_state);
-    
+
     //dsm($this);
     //dsm($this->view->display_handler);
     //dsm($this->display);
@@ -42,6 +52,17 @@ class views_hierarchy_plugin_style_hiera
     $field_data = views_fetch_fields(array_keys($base_tables), 'field');
     //dsm($field_data);
 
+    // Add a special default option when nothing is set or the saved value is
+    // invalid; otherwise the first item in the dropdown is selected and the
+    // user could think they don't need to update the form.
+    if (is_null($this->options['parent_data']) || !isset($fields[$this->options['parent_data']])) {
+      $options[] = '<Not set>';
+      $default_value = 0;
+    }
+    else {
+      $default_value = $this->options['parent_data'];
+    }
+
     // Just to keep our head straight: 
     // - $field_id is the id of the field in the current view, and may be an alias
     //   such as 'name_1'.
@@ -62,14 +83,58 @@ class views_hierarchy_plugin_style_hiera
       
       $options[$field_id] = $label;
     }
-        
+       
     $form['parent_data'] = array(
       '#type' => 'select',
       '#title' => t('Parent field'),
       '#description' => t("The field that holds the ID of an item's parent."),
       '#options' => $options,
-      '#default_value' => $this->options['parent_data'],
+      '#default_value' => $default_value,
+      '#required' => TRUE,
     );
+    
+  }
+
+  /**
+   * The options form validation handler.
+   */
+  function options_validate($form, &$form_state) {
+    // Don't let the user choose the special 'Not set' value.
+    if ($form_state['values']['style_options']['parent_data'] == '0') {
+      form_error($form['parent_data'], t('You must choose a parent field.'));
+    }
+  }
+
+  /**
+   * Validates the handler against the complete View.
+   *
+   * @return
+   *   Empty array if the handler is valid; an array of error strings if it is not.
+   */
+  function validate() {
+    $errors = parent::validate();
+
+    // The row style plugin must use fields.
+    if (!$this->row_plugin->definition['uses fields']) {
+      $errors[] = t('The hierachy style must be used with a row style which uses fields.');
+    }
+
+    // Parent field must be set.
+    if (is_null($this->options['parent_data'])) {
+      $errors[] = t('The hierarchy style requires a parent field to be set.');
+    }
+    else {
+      // Parent field setting must be a field 
+      $display_id = ($this->view->display_handler->is_defaulted('fields')) ? 'default' : $this->view->current_display;
+
+     	$fields = $this->view->display[$display_id]->display_options['fields'];
+     	//dsm($fields);
+     	if (!isset($fields[$this->options['parent_data']])) {
+        $errors[] = t('The hierarchy style requires a parent field to be set from the current display.');
+     	}
+    }
+
+    return $errors;
   }
 }
 
