Form builder callback for editing a View.

@todo Remove as many #prefix/#suffix lines as possible. Use #theme_wrappers instead.

@todo Rename to views_ui_edit_view_form(). See that function for the "old" version.

See also

views_ui_ajax_get_form()

2 string references to 'views_ui_edit_form'
views_ui_edit_page in includes/admin.inc
Page callback for the Edit View page.
views_ui_menu in ./views_ui.module
Implements hook_menu().

File

includes/admin.inc, line 983
Provides the Views' administrative interface.

Code

function views_ui_edit_form($form, &$form_state, $view, $display_id = NULL) {

  // Do not allow the form to be cached, because $form_state['view'] can become
  // stale between page requests.
  // See views_ui_ajax_get_form() for how this affects #ajax.
  // @todo To remove this and allow the form to be cacheable:
  //   - Change $form_state['view'] to $form_state['temporary']['view'].
  //   - Add a #process function to initialize $form_state['temporary']['view']
  //     on cached form submissions.
  //   - Update ctools_include() to support cached forms, or else use
  //     form_load_include().
  $form_state['no_cache'] = TRUE;
  if ($display_id) {
    if (!$view
      ->set_display($display_id)) {
      $form['#markup'] = t('Invalid display id @display', array(
        '@display' => $display_id,
      ));
      return $form;
    }
    $view
      ->fix_missing_relationships();
  }
  ctools_include('dependent');
  $form['#attached']['js'][] = ctools_attach_js('dependent');
  $form['#attached']['js'][] = ctools_attach_js('collapsible-div');
  $form['#tree'] = TRUE;

  // @todo When more functionality is added to this form, cloning here may be
  //   too soon. But some of what we do with $view later in this function
  //   results in making it unserializable due to PDO limitations.
  $form_state['view'] = clone $view;
  $form['#attached']['library'][] = array(
    'system',
    'ui.tabs',
  );
  $form['#attached']['library'][] = array(
    'system',
    'ui.dialog',
  );
  $form['#attached']['library'][] = array(
    'system',
    'drupal.ajax',
  );
  $form['#attached']['library'][] = array(
    'system',
    'jquery.form',
  );

  // @todo This should be getting added to the page when an ajax popup calls
  // for it, instead of having to add it manually here.
  $form['#attached']['js'][] = 'misc/tabledrag.js';
  $form['#attached']['css'] = views_ui_get_admin_css();
  $module_path = drupal_get_path('module', 'views_ui');
  $form['#attached']['js'][] = $module_path . '/js/views-admin.js';
  $form['#attached']['js'][] = array(
    'data' => array(
      'views' => array(
        'ajax' => array(
          'id' => '#views-ajax-body',
          'title' => '#views-ajax-title',
          'popup' => '#views-ajax-popup',
          'defaultForm' => views_ui_get_default_ajax_message(),
        ),
      ),
    ),
    'type' => 'setting',
  );
  $form += array(
    '#prefix' => '',
    '#suffix' => '',
  );
  $form['#prefix'] = $form['#prefix'] . '<div class="views-edit-view views-admin clearfix">';
  $form['#suffix'] = '</div>' . $form['#suffix'];
  $form['#attributes']['class'] = array(
    'form-edit',
  );
  if (isset($view->locked) && is_object($view->locked)) {
    $form['locked'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#attributes' => array(
        'class' => array(
          'view-locked',
          'messages',
          'warning',
        ),
      ),
      '#markup' => t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="!break">break this lock</a>.', array(
        '!user' => theme('username', array(
          'account' => user_load($view->locked->uid),
        )),
        '!age' => format_interval(REQUEST_TIME - $view->locked->updated),
        '!break' => url('admin/structure/views/view/' . $view->name . '/break-lock'),
      )),
    );
  }
  if (isset($view->vid) && $view->vid == 'new') {
    $message = t('* All changes are stored temporarily. Click Save to make your changes permanent. Click Cancel to discard the view.');
  }
  else {
    $message = t('* All changes are stored temporarily. Click Save to make your changes permanent. Click Cancel to discard your changes.');
  }
  $form['changed'] = array(
    '#theme_wrappers' => array(
      'container',
    ),
    '#attributes' => array(
      'class' => array(
        'view-changed',
        'messages',
        'warning',
      ),
    ),
    '#markup' => $message,
  );
  if (empty($view->changed)) {
    $form['changed']['#attributes']['class'][] = 'js-hide';
  }
  $form['help_text'] = array(
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#markup' => t('Modify the display(s) of your view below or add new displays.'),
  );
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => 0,
  );
  if (empty($view->changed)) {
    $form['actions']['#attributes'] = array(
      'class' => array(
        'js-hide',
      ),
    );
  }
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    // Taken from the "old" UI. @todo: Review and rename.
    '#validate' => array(
      'views_ui_edit_view_form_validate',
    ),
    '#submit' => array(
      'views_ui_edit_view_form_submit',
    ),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#submit' => array(
      'views_ui_edit_view_form_cancel',
    ),
  );
  $form['displays'] = array(
    '#prefix' => '<h1 class="unit-title clearfix">' . t('Displays') . "</h1>\n" . '<div class="views-displays">',
    '#suffix' => '</div>',
  );
  $form['displays']['top'] = views_ui_render_display_top($view, $display_id);

  // The rest requires a display to be selected.
  if ($display_id) {
    $form_state['display_id'] = $display_id;

    // The part of the page where editing will take place. This element is the
    // CTools collapsible-div container for the display edit elements.
    $form['displays']['settings'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#attributes' => array(
        'class' => array(
          'views-display-settings',
          'box-margin',
          'ctools-collapsible-container',
        ),
      ),
      '#id' => 'edit-display-settings',
    );
    $display_title = views_ui_get_display_label($view, $display_id, FALSE);

    // Add a handle for the ctools collapsible-div. The handle is the title of
    // the display.
    $form['displays']['settings']['tab_title']['#markup'] = '<h2 id="edit-display-settings-title" class="ctools-collapsible-handle">' . t('@display_title details', array(
      '@display_title' => ucwords($display_title),
    )) . '</h2>';

    // Add a text that the display is disabled.
    if (!empty($view->display[$display_id]->handler)) {
      $enabled = $view->display[$display_id]->handler
        ->get_option('enabled');
      if (empty($enabled)) {
        $form['displays']['settings']['disabled']['#markup'] = t('This display is disabled.');
      }
    }

    // The ctools collapsible-div content.
    $form['displays']['settings']['settings_content'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#id' => 'edit-display-settings-content',
      '#attributes' => array(
        'class' => array(
          'ctools-collapsible-content',
        ),
      ),
    );

    // Add the edit display content.
    $form['displays']['settings']['settings_content']['tab_content'] = views_ui_get_display_tab($view, $display_id);
    $form['displays']['settings']['settings_content']['tab_content']['#theme_wrappers'] = array(
      'container',
    );
    $form['displays']['settings']['settings_content']['tab_content']['#attributes'] = array(
      'class' => array(
        'views-display-tab',
      ),
    );
    $form['displays']['settings']['settings_content']['tab_content']['#id'] = 'views-tab-' . $display_id;

    // Mark deleted displays as such.
    if (!empty($view->display[$display_id]->deleted)) {
      $form['displays']['settings']['settings_content']['tab_content']['#attributes']['class'][] = 'views-display-deleted';
    }

    // Mark disabled displays as such.
    if (empty($enabled)) {
      $form['displays']['settings']['settings_content']['tab_content']['#attributes']['class'][] = 'views-display-disabled';
    }

    // The content of the popup dialog.
    $form['ajax-area'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#id' => 'views-ajax-popup',
    );
    $form['ajax-area']['ajax-title'] = array(
      '#markup' => '<h2 id="views-ajax-title"></h2>',
    );
    $form['ajax-area']['ajax-body'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#id' => 'views-ajax-body',
      '#markup' => views_ui_get_default_ajax_message(),
    );
  }

  // If relationships had to be fixed, we want to get that into the cache
  // so that edits work properly, and to try to get the user to save it
  // so that it's not using weird fixed up relationships.
  if (!empty($view->relationships_changed) && empty($_POST)) {
    drupal_set_message(t('This view has been automatically updated to fix missing relationships. While this View should continue to work, you should verify that the automatic updates are correct and save this view.'));
    views_ui_cache_set($view);
  }
  return $form;
}