summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Wehner2012-02-08 21:32:01 (GMT)
committer Daniel Wehner2012-02-08 21:32:01 (GMT)
commitcaf5b8c785bf5658e1051e08b97479f472b2e944 (patch)
treeb8d42c0b1b278664e6eaf9ce9336cf87ea960c29
parent0e2132ac12862aa6a2e00051ec635024a6e28912 (diff)
#1425378 by dagmar: Allow the field handler to override the form-row-id nd form-element-name in the handler, but still be backward compatible
-rw-r--r--help/api-forms.html18
-rw-r--r--views.module22
2 files changed, 34 insertions, 6 deletions
diff --git a/help/api-forms.html b/help/api-forms.html
index 749c565..f7f4ff6 100644
--- a/help/api-forms.html
+++ b/help/api-forms.html
@@ -11,19 +11,31 @@ The views handler can also implement views_form_validate() and views_form_submit
return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
}
+ function form_element_name() {
+ // Make sure this value is unique for all the view fields
+ return $this->options['id'];
+ }
+
+ function form_element_row_id($row_id) {
+ // You could use values from $this->view->result[$row_id]
+ // to provide complex row ids.
+ return $row_id;
+ }
+
function views_form(&$form, &$form_state) {
// The view is empty, abort.
if (empty($this->view->result)) {
return;
}
- $field_name = $this->options['id'];
+ $field_name = $this->form_element_name();
$form[$field_name] = array(
'#tree' => TRUE,
);
// At this point, the query has already been run, so we can access the results
foreach ($this->view->result as $row_id => $row) {
- $form[$field_name][$row_id] = array(
+ $form_element_row_id = $this->form_element_row_id($row_id);
+ $form[$field_name][$form_element_row_id] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#default_value' => '',
@@ -33,7 +45,7 @@ The views handler can also implement views_form_validate() and views_form_submit
// Optional validate function.
function views_form_validate($form, &$form_state) {
- $field_name = $this->options['id'];
+ $field_name = $this->form_element_name();
foreach ($form_state['values'][$field_name] as $row_id => $value) {
if ($value == 'Drupal') {
form_set_error($field_name . '][' . $row_id, "You can't be named Drupal. That's my name.");
diff --git a/views.module b/views.module
index 8085953..1bc8088 100644
--- a/views.module
+++ b/views.module
@@ -1741,6 +1741,15 @@ function views_form_views_form($form, &$form_state, $view, $output) {
$substitutions = array();
foreach ($view->field as $field_name => $field) {
+ $form_element_name = $field_name;
+ if (method_exists($field, 'form_element_name')) {
+ $form_element_name = $field->form_element_name();
+ }
+ $method_form_element_row_id_exists = FALSE;
+ if (method_exists($field, 'form_element_row_id')) {
+ $method_form_element_row_id_exists = TRUE;
+ }
+
// If the field provides a views form, allow it to modify the $form array.
$has_form = FALSE;
if (property_exists($field, 'views_form_callback')) {
@@ -1756,10 +1765,17 @@ function views_form_views_form($form, &$form_state, $view, $output) {
// Build the substitutions array for use in the theme function.
if ($has_form) {
foreach ($view->result as $row_id => $row) {
+ if ($method_form_element_row_id_exists) {
+ $form_element_row_id = $field->form_element_row_id($row_id);
+ }
+ else {
+ $form_element_row_id = $row_id;
+ }
+
$substitutions[] = array(
- 'placeholder' => '<!--form-item-' . $field_name . '--' . $row_id . '-->',
- 'field_name' => $field_name,
- 'row_id' => $row_id,
+ 'placeholder' => '<!--form-item-' . $form_element_name . '--' . $form_element_row_id . '-->',
+ 'field_name' => $form_element_name,
+ 'row_id' => $form_element_row_id,
);
}
}