You are here

public function ViewUI::buildEditForm in Views (for Drupal 7) 8.3

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()

File

views_ui/lib/Drupal/views_ui/ViewUI.php, line 1248
Definition of Drupal\views_ui\ViewUI.

Class

ViewUI
Stores UI related temporary settings.

Namespace

Drupal\views_ui

Code

public function buildEditForm($form, &$form_state, $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.
  //   - Use form_load_include().
  $form_state['no_cache'] = TRUE;
  if ($display_id) {
    if (!$this
      ->setDisplay($display_id)) {
      $form['#markup'] = t('Invalid display id @display', array(
        '@display' => $display_id,
      ));
      return $form;
    }
  }
  $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 $this;
  $form['#attached']['library'][] = array(
    'system',
    'jquery.ui.tabs',
  );
  $form['#attached']['library'][] = array(
    'system',
    'jquery.ui.dialog',
  );
  $form['#attached']['library'][] = array(
    'system',
    'drupal.ajax',
  );
  $form['#attached']['library'][] = array(
    'system',
    'jquery.form',
  );
  $form['#attached']['library'][] = array(
    'system',
    'drupal.states',
  );
  $form['#attached']['library'][] = array(
    'system',
    'drupal.tabledrag',
  );
  $form['#attached']['css'] = static::getAdminCSS();
  $form['#attached']['js'][] = drupal_get_path('module', 'views_ui') . '/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' => static::getDefaultAJAXMessage(),
        ),
      ),
    ),
    'type' => 'setting',
  );
  $form += array(
    '#prefix' => '',
    '#suffix' => '',
  );
  $form['#prefix'] .= '<div class="views-edit-view views-admin clearfix">';
  $form['#suffix'] = '</div>' . $form['#suffix'];
  $form['#attributes']['class'] = array(
    'form-edit',
  );
  if (isset($this->locked) && is_object($this->locked) && $this->locked->owner != $GLOBALS['user']->uid) {
    $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($this->locked->owner),
        )),
        '!age' => format_interval(REQUEST_TIME - $this->locked->updated),
        '!break' => url('admin/structure/views/view/' . $this->storage->name . '/break-lock'),
      )),
    );
  }
  else {
    if (isset($this->vid) && $this->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($this->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($this->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'] = $this
    ->renderDisplayTop($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.
    $form['displays']['settings'] = array(
      '#type' => 'container',
      '#id' => 'edit-display-settings',
    );
    $display_title = $this
      ->getDisplayLabel($display_id, FALSE);
    $form['displays']['settings']['#title'] = '<h2>' . t('@display_title details', array(
      '@display_title' => ucwords($display_title),
    )) . '</h2>';

    // Add a text that the display is disabled.
    if (!empty($this->displayHandlers[$display_id])) {
      if (!$this->displayHandlers[$display_id]
        ->isEnabled()) {
        $form['displays']['settings']['disabled']['#markup'] = t('This display is disabled.');
      }
    }
    $form['displays']['settings']['settings_content'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
    );

    // Add the edit display content
    $form['displays']['settings']['settings_content']['tab_content'] = $this
      ->getDisplayTab($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($this->storage->display[$display_id]['deleted'])) {
      $form['displays']['settings']['settings_content']['tab_content']['#attributes']['class'][] = 'views-display-deleted';
    }

    // Mark disabled displays as such.
    if (!$this->displayHandlers[$display_id]
      ->isEnabled()) {
      $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' => static::getDefaultAJAXMessage(),
    );
  }

  // 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($this->relationships_changed) && drupal_container()
    ->get('request')->request
    ->count()) {
    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($this);
  }
  return $form;
}