You are here

public function WizardPluginBase::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 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 228

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', [
    $this->base_table,
  ]);
  $feed_row_options = Views::fetchPluginNames('row', 'feed', [
    $this->base_table,
  ]);
  $path_prefix = Url::fromRoute('<none>', [], [
    'absolute' => TRUE,
  ])
    ->toString();

  // 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'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Page settings'),
    '#attributes' => [
      'class' => [
        'views-attachment',
        'fieldset-no-legend',
      ],
    ],
    '#tree' => TRUE,
  ];
  $form['displays']['page']['create'] = [
    '#title' => $this
      ->t('Create a page'),
    '#type' => 'checkbox',
    '#attributes' => [
      'class' => [
        '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'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'options-set',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="page[create]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#prefix' => '<div><div id="edit-page-wrapper">',
    '#suffix' => '</div></div>',
    '#parents' => [
      'page',
    ],
  ];
  $form['displays']['page']['options']['title'] = [
    '#title' => $this
      ->t('Page title'),
    '#type' => 'textfield',
    '#maxlength' => 255,
  ];
  $form['displays']['page']['options']['path'] = [
    '#title' => $this
      ->t('Path'),
    '#type' => 'textfield',
    '#field_prefix' => $path_prefix,
    // Account for the leading backslash.
    '#maxlength' => 254,
  ];
  $form['displays']['page']['options']['style'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Page display settings'),
    '#attributes' => [
      'class' => [
        'container-inline',
        'fieldset-no-legend',
      ],
    ],
  ];

  // Create the dropdown for choosing the display format.
  $form['displays']['page']['options']['style']['style_plugin'] = [
    '#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, [
    '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', [
    'displays',
    'page',
    'options',
  ]);
  $this
    ->buildFormStyle($form, $form_state, 'page');
  $form['displays']['page']['options']['items_per_page'] = [
    '#title' => $this
      ->t('Items to display'),
    '#type' => 'number',
    '#default_value' => 10,
    '#min' => 0,
  ];
  $form['displays']['page']['options']['pager'] = [
    '#title' => $this
      ->t('Use a pager'),
    '#type' => 'checkbox',
    '#default_value' => TRUE,
  ];
  $form['displays']['page']['options']['link'] = [
    '#title' => $this
      ->t('Create a menu link'),
    '#type' => 'checkbox',
    '#id' => 'edit-page-link',
  ];
  $form['displays']['page']['options']['link_properties'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        ':input[name="page[link]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#prefix' => '<div id="edit-page-link-properties-wrapper">',
    '#suffix' => '</div>',
  ];
  $form['displays']['page']['options']['link_properties']['parent'] = $this->parentFormSelector
    ->parentSelectElement('admin:');
  $form['displays']['page']['options']['link_properties']['parent'] += [
    '#title' => $this
      ->t('Menu'),
  ];
  $form['displays']['page']['options']['link_properties']['title'] = [
    '#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'] = [
      '#title' => $this
        ->t('Include an RSS feed'),
      '#type' => 'checkbox',
      '#id' => 'edit-page-feed',
    ];
    $form['displays']['page']['options']['feed_properties'] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          ':input[name="page[feed]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#prefix' => '<div id="edit-page-feed-properties-wrapper">',
      '#suffix' => '</div>',
    ];
    $form['displays']['page']['options']['feed_properties']['path'] = [
      '#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'] = [
      '#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' => [
        'visible' => [
          ':input[name="page[feed]"]' => [
            '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'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Block settings'),
      '#attributes' => [
        'class' => [
          'views-attachment',
          'fieldset-no-legend',
        ],
      ],
      '#tree' => TRUE,
    ];
    $form['displays']['block']['create'] = [
      '#title' => $this
        ->t('Create a block'),
      '#type' => 'checkbox',
      '#attributes' => [
        'class' => [
          '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'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'options-set',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input[name="block[create]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#prefix' => '<div id="edit-block-wrapper">',
      '#suffix' => '</div>',
      '#parents' => [
        'block',
      ],
    ];
    $form['displays']['block']['options']['title'] = [
      '#title' => $this
        ->t('Block title'),
      '#type' => 'textfield',
      '#maxlength' => 255,
    ];
    $form['displays']['block']['options']['style'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Block display settings'),
      '#attributes' => [
        'class' => [
          'container-inline',
          'fieldset-no-legend',
        ],
      ],
    ];

    // Create the dropdown for choosing the display format.
    $form['displays']['block']['options']['style']['style_plugin'] = [
      '#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, [
      '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', [
      'displays',
      'block',
      'options',
    ]);
    $this
      ->buildFormStyle($form, $form_state, 'block');
    $form['displays']['block']['options']['items_per_page'] = [
      '#title' => $this
        ->t('Items per block'),
      '#type' => 'number',
      '#default_value' => 5,
      '#min' => 0,
    ];
    $form['displays']['block']['options']['pager'] = [
      '#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'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('REST export settings'),
      '#attributes' => [
        'class' => [
          'views-attachment',
          'fieldset-no-legend',
        ],
      ],
      '#tree' => TRUE,
    ];
    $form['displays']['rest_export']['create'] = [
      '#title' => $this
        ->t('Provide a REST export'),
      '#type' => 'checkbox',
      '#attributes' => [
        'class' => [
          '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'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'options-set',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input[name="rest_export[create]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#prefix' => '<div id="edit-rest-export-wrapper">',
      '#suffix' => '</div>',
      '#parents' => [
        'rest_export',
      ],
    ];
    $form['displays']['rest_export']['options']['path'] = [
      '#title' => $this
        ->t('REST export path'),
      '#type' => 'textfield',
      '#field_prefix' => $path_prefix,
      // Account for the leading backslash.
      '#maxlength' => 254,
    ];
  }
  return $form;
}