You are here

function WizardPluginBase::build_form in Views (for Drupal 7) 8.3

Implements Drupal\views\Plugin\views\wizard\WizardInterface::build_form().

Overrides WizardInterface::build_form

File

lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php, line 202
Definition of Drupal\views\Plugin\views\wizard\WizardPluginBase.

Class

WizardPluginBase
Provides the interface and base class for Views Wizard plugins.

Namespace

Drupal\views\Plugin\views\wizard

Code

function build_form(array $form, array &$form_state) {
  $style_options = views_fetch_plugin_names('style', 'normal', array(
    $this->base_table,
  ));
  $feed_row_options = views_fetch_plugin_names('row', 'feed', array(
    $this->base_table,
  ));
  $path_prefix = url(NULL, array(
    'absolute' => TRUE,
  ));

  // Add filters and sorts which apply to the view as a whole.
  $this
    ->build_filters($form, $form_state);
  $this
    ->build_sorts($form, $form_state);
  $form['displays']['page'] = array(
    '#type' => 'fieldset',
    '#attributes' => array(
      'class' => array(
        'views-attachment',
        'fieldset-no-legend',
      ),
    ),
    '#tree' => TRUE,
  );
  $form['displays']['page']['create'] = array(
    '#title' => t('Create a page'),
    '#type' => 'checkbox',
    '#attributes' => array(
      'class' => array(
        'strong',
      ),
    ),
    '#default_value' => TRUE,
    '#id' => 'edit-page-create',
  );

  // All options for the page display are included in this container so they
  // can be hidden as a group when the "Create a page" checkbox is unchecked.
  $form['displays']['page']['options'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'options-set',
      ),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="page[create]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#prefix' => '<div><div id="edit-page-wrapper">',
    '#suffix' => '</div></div>',
    '#parents' => array(
      'page',
    ),
  );
  $form['displays']['page']['options']['title'] = array(
    '#title' => t('Page title'),
    '#type' => 'textfield',
  );
  $form['displays']['page']['options']['path'] = array(
    '#title' => t('Path'),
    '#type' => 'textfield',
    '#field_prefix' => $path_prefix,
  );
  $form['displays']['page']['options']['style'] = array(
    '#type' => 'fieldset',
    '#attributes' => array(
      'class' => array(
        'container-inline',
        'fieldset-no-legend',
      ),
    ),
  );

  // Create the dropdown for choosing the display format.
  $form['displays']['page']['options']['style']['style_plugin'] = array(
    '#title' => t('Display format'),
    '#type' => 'select',
    '#options' => $style_options,
  );
  $style_form =& $form['displays']['page']['options']['style'];
  $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array(
    'page',
    'style',
    'style_plugin',
  ), 'default', $style_form['style_plugin']);

  // Changing this dropdown updates $form['displays']['page']['options'] via
  // AJAX.
  views_ui_add_ajax_trigger($style_form, 'style_plugin', array(
    'displays',
    'page',
    'options',
  ));
  $this
    ->build_form_style($form, $form_state, 'page');
  $form['displays']['page']['options']['items_per_page'] = array(
    '#title' => t('Items to display'),
    '#type' => 'number',
    '#default_value' => 10,
    '#min' => 0,
  );
  $form['displays']['page']['options']['pager'] = array(
    '#title' => t('Use a pager'),
    '#type' => 'checkbox',
    '#default_value' => TRUE,
  );
  $form['displays']['page']['options']['link'] = array(
    '#title' => t('Create a menu link'),
    '#type' => 'checkbox',
    '#id' => 'edit-page-link',
  );
  $form['displays']['page']['options']['link_properties'] = array(
    '#type' => 'container',
    '#states' => array(
      'visible' => array(
        ':input[name="page[link]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#prefix' => '<div id="edit-page-link-properties-wrapper">',
    '#suffix' => '</div>',
  );
  if (module_exists('menu')) {
    $menu_options = menu_get_menus();
  }
  else {

    // These are not yet translated.
    $menu_options = menu_list_system_menus();
    foreach ($menu_options as $name => $title) {
      $menu_options[$name] = t($title);
    }
  }
  $form['displays']['page']['options']['link_properties']['menu_name'] = array(
    '#title' => t('Menu'),
    '#type' => 'select',
    '#options' => $menu_options,
  );
  $form['displays']['page']['options']['link_properties']['title'] = array(
    '#title' => t('Link text'),
    '#type' => 'textfield',
  );

  // Only offer a feed if we have at least one available feed row style.
  if ($feed_row_options) {
    $form['displays']['page']['options']['feed'] = array(
      '#title' => t('Include an RSS feed'),
      '#type' => 'checkbox',
      '#id' => 'edit-page-feed',
    );
    $form['displays']['page']['options']['feed_properties'] = array(
      '#type' => 'container',
      '#states' => array(
        'visible' => array(
          ':input[name="page[feed]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#prefix' => '<div id="edit-page-feed-properties-wrapper">',
      '#suffix' => '</div>',
    );
    $form['displays']['page']['options']['feed_properties']['path'] = array(
      '#title' => t('Feed path'),
      '#type' => 'textfield',
      '#field_prefix' => $path_prefix,
    );

    // This will almost never be visible.
    $form['displays']['page']['options']['feed_properties']['row_plugin'] = array(
      '#title' => t('Feed row style'),
      '#type' => 'select',
      '#options' => $feed_row_options,
      '#default_value' => key($feed_row_options),
      '#access' => count($feed_row_options) > 1,
      '#states' => array(
        'visible' => array(
          ':input[name="page[feed]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#prefix' => '<div id="edit-page-feed-properties-row-plugin-wrapper">',
      '#suffix' => '</div>',
    );
  }
  if (!module_exists('block')) {
    return $form;
  }
  $form['displays']['block'] = array(
    '#type' => 'fieldset',
    '#attributes' => array(
      'class' => array(
        'views-attachment',
        'fieldset-no-legend',
      ),
    ),
    '#tree' => TRUE,
  );
  $form['displays']['block']['create'] = array(
    '#title' => t('Create a block'),
    '#type' => 'checkbox',
    '#attributes' => array(
      'class' => array(
        'strong',
      ),
    ),
    '#id' => 'edit-block-create',
  );

  // All options for the block display are included in this container so they
  // can be hidden as a group when the "Create a page" checkbox is unchecked.
  $form['displays']['block']['options'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'options-set',
      ),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="block[create]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#prefix' => '<div id="edit-block-wrapper">',
    '#suffix' => '</div>',
    '#parents' => array(
      'block',
    ),
  );
  $form['displays']['block']['options']['title'] = array(
    '#title' => t('Block title'),
    '#type' => 'textfield',
  );
  $form['displays']['block']['options']['style'] = array(
    '#type' => 'fieldset',
    '#attributes' => array(
      'class' => array(
        'container-inline',
        'fieldset-no-legend',
      ),
    ),
  );

  // Create the dropdown for choosing the display format.
  $form['displays']['block']['options']['style']['style_plugin'] = array(
    '#title' => t('Display format'),
    '#type' => 'select',
    '#options' => $style_options,
  );
  $style_form =& $form['displays']['block']['options']['style'];
  $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array(
    'block',
    'style',
    'style_plugin',
  ), 'default', $style_form['style_plugin']);

  // Changing this dropdown updates $form['displays']['block']['options'] via
  // AJAX.
  views_ui_add_ajax_trigger($style_form, 'style_plugin', array(
    'displays',
    'block',
    'options',
  ));
  $this
    ->build_form_style($form, $form_state, 'block');
  $form['displays']['block']['options']['items_per_page'] = array(
    '#title' => t('Items per page'),
    '#type' => 'number',
    '#default_value' => 5,
    '#min' => 0,
  );
  $form['displays']['block']['options']['pager'] = array(
    '#title' => t('Use a pager'),
    '#type' => 'checkbox',
    '#default_value' => FALSE,
  );
  return $form;
}