public function WebformVariantFormBase::buildForm in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Form/WebformVariantFormBase.php \Drupal\webform\Form\WebformVariantFormBase::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
\Drupal\webform\WebformInterface $webform: The webform.
string $webform_variant: The webform variant ID.
Return value
array The form structure.
Throws
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Throws not found exception if the number of variant instances for this webform exceeds the variant's cardinality.
Overrides FormInterface::buildForm
2 calls to WebformVariantFormBase::buildForm()
- WebformVariantAddForm::buildForm in src/
Form/ WebformVariantAddForm.php - Form constructor.
- WebformVariantEditForm::buildForm in src/
Form/ WebformVariantEditForm.php - Form constructor.
2 methods override WebformVariantFormBase::buildForm()
- WebformVariantAddForm::buildForm in src/
Form/ WebformVariantAddForm.php - Form constructor.
- WebformVariantEditForm::buildForm in src/
Form/ WebformVariantEditForm.php - Form constructor.
File
- src/
Form/ WebformVariantFormBase.php, line 94
Class
- WebformVariantFormBase
- Provides a base webform for webform variants.
Namespace
Drupal\webform\FormCode
public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL, $webform_variant = NULL) {
$this->webform = $webform;
try {
$this->webformVariant = $this
->prepareWebformVariant($webform_variant);
} catch (PluginNotFoundException $e) {
throw new NotFoundHttpException("Invalid variant id: '{$webform_variant}'.");
}
// Add meta data to webform variant form.
// This information makes it a little easier to alter a variant's form.
$form['#webform_id'] = $this->webform
->id();
$form['#webform_variant_id'] = $this->webformVariant
->getVariantId();
$form['#webform_variant_plugin_id'] = $this->webformVariant
->getPluginId();
$request = $this
->getRequest();
$form['description'] = [
'#type' => 'container',
'text' => [
'#markup' => $this->webformVariant
->description(),
'#prefix' => '<p>',
'#suffix' => '</p>',
],
'#weight' => -20,
];
$form['id'] = [
'#type' => 'value',
'#value' => $this->webformVariant
->getPluginId(),
];
$form['general'] = [
'#type' => 'fieldset',
'#title' => $this
->t('General settings'),
'#weight' => -10,
];
$form['general']['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#maxlength' => 255,
'#default_value' => $this->webformVariant
->getLabel(),
'#required' => TRUE,
'#attributes' => [
'autofocus' => 'autofocus',
],
];
$t_args = [
'@requirements' => $this
->t('letters, numbers, underscores, and dashes'),
];
$form['general']['variant_id'] = [
'#type' => 'machine_name',
'#maxlength' => static::MACHINE_NAME_MAXLENGHTH,
'#description' => $this
->t('A unique name for this variant instance. Can only contain @requirements.', $t_args),
'#default_value' => $this->webformVariant
->getVariantId(),
'#required' => TRUE,
'#disabled' => $this->webformVariant
->getVariantId() ? TRUE : FALSE,
'#machine_name' => [
'source' => [
'general',
'label',
],
'exists' => [
$this,
'exists',
],
'replace_pattern' => $this->webformVariant
->getMachineNameReplacePattern(),
'replace' => $this->webformVariant
->getMachineNameReplace(),
'error' => $this
->t('The element key name must contain only @requirements.', $t_args),
],
];
// Only show variants select menu when there is more than
// one variant available.
$variant_options = $this
->getVariantElementsAsOptions();
if (count($variant_options) === 1) {
$form['general']['element_key'] = [
'#type' => 'value',
'#value' => key($variant_options),
];
$form['general']['element_key_item'] = [
'#title' => $this
->t('Element'),
'#type' => 'item',
'#markup' => reset($variant_options),
'#access' => TRUE,
];
}
else {
$form['general']['element_key'] = [
'#type' => 'select',
'#title' => $this
->t('Element'),
'#options' => $variant_options,
'#default_value' => $this->webformVariant
->getElementKey(),
'#required' => TRUE,
];
}
$form['general']['notes'] = [
'#type' => 'textarea',
'#title' => $this
->t('Administrative notes'),
'#description' => $this
->t("Entered text will be displayed on the variants administrative page."),
'#rows' => 2,
'#default_value' => $this->webformVariant
->getNotes(),
];
$form['advanced'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Advanced settings'),
'#weight' => -10,
];
$form['advanced']['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable the %name variant', [
'%name' => $this->webformVariant
->label(),
]),
'#return_value' => TRUE,
'#default_value' => $this->webformVariant
->isEnabled(),
// Disable broken plugins.
'#disabled' => $this->webformVariant
->getPluginId() === 'broken',
];
$form['#parents'] = [];
$form['settings'] = [
'#tree' => TRUE,
'#parents' => [
'settings',
],
];
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$form['settings'] = $this->webformVariant
->buildConfigurationForm($form['settings'], $subform_state);
// Get $form['settings']['#attributes']['novalidate'] and apply it to the
// $form.
// This allows variants with hide/show logic to skip HTML5 validation.
// @see http://stackoverflow.com/questions/22148080/an-invalid-form-control-with-name-is-not-focusable
if (isset($form['settings']['#attributes']['novalidate'])) {
$form['#attributes']['novalidate'] = 'novalidate';
}
$form['settings']['#tree'] = TRUE;
// Check the URL for a weight, then the webform variant,
// otherwise use default.
$form['weight'] = [
'#type' => 'hidden',
'#value' => $request->query
->has('weight') ? (int) $request->query
->get('weight') : $this->webformVariant
->getWeight(),
];
// Build tabs.
$tabs = [
'advanced' => [
'title' => $this
->t('Advanced'),
'elements' => [
'advanced',
'additional',
'development',
],
'weight' => 20,
],
];
$form = WebformFormHelper::buildTabs($form, $tabs);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
// Add token links below the form and on every tab.
$form['token_tree_link'] = $this->tokenManager
->buildTreeElement();
if ($form['token_tree_link']) {
$form['token_tree_link'] += [
'#weight' => 101,
];
}
return $this
->buildDialogForm($form, $form_state);
}