diff --git a/help/api-forms.html b/help/api-forms.html
index 3849719..0fc4497 100644
--- a/help/api-forms.html
+++ b/help/api-forms.html
@@ -55,6 +55,11 @@ A "form_example" module could add a confirmation step by setting:
in form_example_views_form_submit().
Then, views_form would call form_example_confirmation($form, $form_state, $view, $output) to get that step.
+Important: You can fetch the Views object in form_alter and validate / submit hooks from the form state:
+
+ $view = $form_state['build_info']['args'][0];
+
+
Relevant Views functions
- template_preprocess_views_view()
diff --git a/theme/theme.inc b/theme/theme.inc
index bf772b2..a56169d 100644
--- a/theme/theme.inc
+++ b/theme/theme.inc
@@ -873,20 +873,18 @@ function template_preprocess_views_exposed_form(&$vars) {
*/
function theme_views_form_views_form($variables) {
$form = $variables['form'];
- $view = $form['view']['#value'];
// Placeholders and their substitutions (usually rendered form elements).
$search = array();
$replace = array();
- // Prepare substitutions for views form elements.
- foreach ($view->field as $field_name => $field) {
- if (property_exists($field, 'views_form_callback') || method_exists($field, 'views_form')) {
- foreach ($view->result as $row_id => $row) {
- $search[] = '';
- $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
- }
- }
+ // Add in substitutions provided by the form.
+ foreach ($form['#substitutions']['#value'] as $substitution) {
+ $field_name = $substitution['field_name'];
+ $row_id = $substitution['row_id'];
+
+ $search[] = $substitution['placeholder'];
+ $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
}
// Add in substitutions from hook_views_form_substitutions().
$substitutions = module_invoke_all('views_form_substitutions');
diff --git a/views.module b/views.module
index b77a67d..9c5a864 100644
--- a/views.module
+++ b/views.module
@@ -1486,12 +1486,6 @@ function views_form($form, &$form_state, $view, $output) {
$form = array();
$form['#validate'] = array('views_form_validate');
$form['#submit'] = array('views_form_submit');
- // The view is stored in $form and not $form_state because
- // the theme functions only get $form.
- $form['view'] = array(
- '#type' => 'value',
- '#value' => $view,
- );
// Tell the preprocessor whether it should hide the header, footer, pager...
$form['show_view_elements'] = array(
'#type' => 'value',
@@ -1549,17 +1543,36 @@ function views_form_views_form($form, &$form_state, $view, $output) {
'#weight' => 50,
);
+ $substitutions = array();
foreach ($view->field as $field_name => $field) {
// If the field provides a views form, allow it to modify the $form array.
+ $has_form = FALSE;
if (property_exists($field, 'views_form_callback')) {
$callback = $field->views_form_callback;
$callback($view, $field, $form, $form_state);
+ $has_form = TRUE;
}
elseif (method_exists($field, 'views_form')) {
$field->views_form($form, $form_state);
+ $has_form = TRUE;
+ }
+
+ // Build the substitutions array for use in the theme function.
+ if ($has_form) {
+ foreach ($view->result as $row_id => $row) {
+ $substitutions[] = array(
+ 'placeholder' => '',
+ 'field_name' => $field_name,
+ 'row_id' => $row_id,
+ );
+ }
}
}
+ $form['#substitutions'] = array(
+ '#type' => 'value',
+ '#value' => $substitutions,
+ );
$form['actions'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('form-actions')),
@@ -1580,7 +1593,7 @@ function views_form_views_form($form, &$form_state, $view, $output) {
* on the views fields.
*/
function views_form_views_form_validate($form, &$form_state) {
- $view = $form['view']['#value'];
+ $view = $form_state['build_info']['args'][0];
// Call the validation method on every field handler that has it.
foreach ($view->field as $field_name => $field) {
@@ -1596,7 +1609,7 @@ function views_form_views_form_validate($form, &$form_state) {
* on the views fields.
*/
function views_form_views_form_submit($form, &$form_state) {
- $view = $form['view']['#value'];
+ $view = $form_state['build_info']['args'][0];
// Call the submit method on every field handler that has it.
foreach ($view->field as $field_name => $field) {