Currently the views_handler_area not allow substitutions.

This is necessary to correctly position the defined form item on custom handlers ("views_form" method) like on views_handler_fields.

Views calls render handler method before views_form, so a placeholder is used to replace on the theme function (theme_views_form_views_form.

views_handler_field example:

class view_form_field_handler_title_edit  extends views_handler_field {
  ...
  function render($values) {
    // Render a Views form item placeholder.
    // This causes Views to wrap the View in a form.
    // Render a Views form item placeholder.
    return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  }
  ...
  function views_form(&$form, &$form_state) {
    $form[$this->options['id']] = array(
      '#type' => 'container',
      '#tree' => TRUE,
    );
    ...
  }
  ...
}

Views views_form_views_form function:

http://api.drupal.org/api/views/views.module/function/views_form_views_f...

Substitutios:

$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')) {
      $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) {
        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-' . $form_element_name . '--' . $form_element_row_id . '-->',
          'field_name' => $form_element_name,
          'row_id' => $form_element_row_id,
        );
      }
    }
  }

Area handlers: without substitutions.

// Give the area handlers a chance to extend the form.
  $area_handlers = array_merge(array_values($view->header), array_values($view->footer));
  $empty = empty($view->result);
  foreach ($area_handlers as $area) {
    if (method_exists($area, 'views_form') && !$area->views_form_empty($empty)) {
      $area->views_form($form, $form_state);
    }
  }

In the theme function the substitutions are used to move/set the fields order:

function theme_views_form_views_form($variables) {
  $form = $variables['form'];

  // Placeholders and their substitutions (usually rendered form elements).
  $search = array();
  $replace = array();

  // 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');
  foreach ($substitutions as $placeholder => $substitution) {
    $search[] = $placeholder;
    $replace[] = $substitution;
  }

  // Apply substitutions to the rendered output.
  $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);

  // Render and add remaining form fields.
  return drupal_render_children($form);
}

Comments

GeduR’s picture

Component: Miscellaneous » Code
Category: bug » feature

Changing the category and component thas was misassigned.

GeduR’s picture

Issue summary: View changes

Improve documentation