You are here

public function WizardPluginBase::buildForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php \Drupal\views\Plugin\views\wizard\WizardPluginBase::buildForm()

Form callback to build other elements in the "show" form.

This method builds all form elements beside of the selection of the base table.

Parameters

array $form: The full wizard form array.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the wizard form.

Return value

array Returns the changed wizard form.

Overrides WizardInterface::buildForm

File

core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php, line 182
Contains \Drupal\views\Plugin\views\wizard\WizardPluginBase.

Class

WizardPluginBase
Base class for Views wizard plugins.

Namespace

Drupal\views\Plugin\views\wizard

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $style_options = Views::fetchPluginNames('style', 'normal', array(
    $this->base_table,
  ));
  $feed_row_options = Views::fetchPluginNames('row', 'feed', array(
    $this->base_table,
  ));
  $path_prefix = $this
    ->url('<none>', [], [
    'absolute' => TRUE,
  ]);

  // Add filters and sorts which apply to the view as a whole.
  $this
    ->buildFilters($form, $form_state);
  $this
    ->buildSorts($form, $form_state);
  $form['displays']['page'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Page settings'),
    '#attributes' => array(
      'class' => array(
        'views-attachment',
        'fieldset-no-legend',
      ),
    ),
    '#tree' => TRUE,
  );
  $form['displays']['page']['create'] = array(
    '#title' => $this
      ->t('Create a page'),
    '#type' => 'checkbox',
    '#attributes' => array(
      'class' => array(
        'strong',
      ),
    ),
    '#default_value' => FALSE,
    '#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' => $this
      ->t('Page title'),
    '#type' => 'textfield',
    '#maxlength' => 255,
  );
  $form['displays']['page']['options']['path'] = array(
    '#title' => $this
      ->t('Path'),
    '#type' => 'textfield',
    '#field_prefix' => $path_prefix,
    // Account for the leading backslash.
    '#maxlength' => 254,
  );
  $form['displays']['page']['options']['style'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Page display settings'),
    '#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' => $this
      ->t('Display format'),
    '#type' => 'select',
    '#options' => $style_options,
  );
  $style_form =& $form['displays']['page']['options']['style'];
  $style_form['style_plugin']['#default_value'] = static::getSelected($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
    ->buildFormStyle($form, $form_state, 'page');
  $form['displays']['page']['options']['items_per_page'] = array(
    '#title' => $this
      ->t('Items to display'),
    '#type' => 'number',
    '#default_value' => 10,
    '#min' => 0,
  );
  $form['displays']['page']['options']['pager'] = array(
    '#title' => $this
      ->t('Use a pager'),
    '#type' => 'checkbox',
    '#default_value' => TRUE,
  );
  $form['displays']['page']['options']['link'] = array(
    '#title' => $this
      ->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 (\Drupal::moduleHandler()
    ->moduleExists('menu_ui')) {
    $menu_options = menu_ui_get_menus();
  }
  else {

    // These are not yet translated.
    $menu_options = menu_list_system_menus();
    foreach ($menu_options as $name => $title) {
      $menu_options[$name] = $this
        ->t($title);
    }
  }
  $form['displays']['page']['options']['link_properties']['menu_name'] = array(
    '#title' => $this
      ->t('Menu'),
    '#type' => 'select',
    '#options' => $menu_options,
  );
  $form['displays']['page']['options']['link_properties']['title'] = array(
    '#title' => $this
      ->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' => $this
        ->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' => $this
        ->t('Feed path'),
      '#type' => 'textfield',
      '#field_prefix' => $path_prefix,
      // Account for the leading backslash.
      '#maxlength' => 254,
    );

    // This will almost never be visible.
    $form['displays']['page']['options']['feed_properties']['row_plugin'] = array(
      '#title' => $this
        ->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>',
    );
  }

  // Only offer the block settings if the module is enabled.
  if (\Drupal::moduleHandler()
    ->moduleExists('block')) {
    $form['displays']['block'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Block settings'),
      '#attributes' => array(
        'class' => array(
          'views-attachment',
          'fieldset-no-legend',
        ),
      ),
      '#tree' => TRUE,
    );
    $form['displays']['block']['create'] = array(
      '#title' => $this
        ->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 block" 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' => $this
        ->t('Block title'),
      '#type' => 'textfield',
      '#maxlength' => 255,
    );
    $form['displays']['block']['options']['style'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Block display settings'),
      '#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' => $this
        ->t('Display format'),
      '#type' => 'select',
      '#options' => $style_options,
    );
    $style_form =& $form['displays']['block']['options']['style'];
    $style_form['style_plugin']['#default_value'] = static::getSelected($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
      ->buildFormStyle($form, $form_state, 'block');
    $form['displays']['block']['options']['items_per_page'] = array(
      '#title' => $this
        ->t('Items per block'),
      '#type' => 'number',
      '#default_value' => 5,
      '#min' => 0,
    );
    $form['displays']['block']['options']['pager'] = array(
      '#title' => $this
        ->t('Use a pager'),
      '#type' => 'checkbox',
      '#default_value' => FALSE,
    );
  }

  // Only offer the REST export settings if the module is enabled.
  if (\Drupal::moduleHandler()
    ->moduleExists('rest')) {
    $form['displays']['rest_export'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('REST export settings'),
      '#attributes' => array(
        'class' => array(
          'views-attachment',
          'fieldset-no-legend',
        ),
      ),
      '#tree' => TRUE,
    );
    $form['displays']['rest_export']['create'] = array(
      '#title' => $this
        ->t('Provide a REST export'),
      '#type' => 'checkbox',
      '#attributes' => array(
        'class' => array(
          'strong',
        ),
      ),
      '#id' => 'edit-rest-export-create',
    );

    // All options for the REST export display are included in this container
    // so they can be hidden as a group when the "Provide a REST export"
    // checkbox is unchecked.
    $form['displays']['rest_export']['options'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'options-set',
        ),
      ),
      '#states' => array(
        'visible' => array(
          ':input[name="rest_export[create]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#prefix' => '<div id="edit-rest-export-wrapper">',
      '#suffix' => '</div>',
      '#parents' => array(
        'rest_export',
      ),
    );
    $form['displays']['rest_export']['options']['path'] = array(
      '#title' => $this
        ->t('REST export path'),
      '#type' => 'textfield',
      '#field_prefix' => $path_prefix,
      // Account for the leading backslash.
      '#maxlength' => 254,
    );
  }
  return $form;
}