You are here

public function WebformHandlerFormBase::buildForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Form/WebformHandlerFormBase.php \Drupal\webform\Form\WebformHandlerFormBase::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_handler: The webform handler ID.

Return value

array The form structure.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Throws not found exception if the number of handler instances for this webform exceeds the handler's cardinality.

Overrides FormInterface::buildForm

2 calls to WebformHandlerFormBase::buildForm()
WebformHandlerAddForm::buildForm in src/Form/WebformHandlerAddForm.php
Form constructor.
WebformHandlerEditForm::buildForm in src/Form/WebformHandlerEditForm.php
Form constructor.
2 methods override WebformHandlerFormBase::buildForm()
WebformHandlerAddForm::buildForm in src/Form/WebformHandlerAddForm.php
Form constructor.
WebformHandlerEditForm::buildForm in src/Form/WebformHandlerEditForm.php
Form constructor.

File

src/Form/WebformHandlerFormBase.php, line 100

Class

WebformHandlerFormBase
Provides a base webform for webform handlers.

Namespace

Drupal\webform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL, $webform_handler = NULL) {
  $this->webform = $webform;
  try {
    $this->webformHandler = $this
      ->prepareWebformHandler($webform_handler);
  } catch (PluginNotFoundException $e) {
    throw new NotFoundHttpException("Invalid handler id: '{$webform_handler}'.");
  }

  // Limit the number of plugin instanced allowed.
  if (!$this->webformHandler
    ->getHandlerId()) {
    $plugin_id = $this->webformHandler
      ->getPluginId();
    $cardinality = $this->webformHandler
      ->cardinality();
    $number_of_instances = $webform
      ->getHandlers($plugin_id)
      ->count();
    if ($cardinality !== WebformHandlerInterface::CARDINALITY_UNLIMITED && $cardinality <= $number_of_instances) {
      throw new NotFoundHttpException($this
        ->formatPlural($cardinality, 'Only @count instance is permitted', 'Only @count instances are permitted'));
    }
  }

  // Add meta data to webform handler form.
  // This information makes it a little easier to alter a handler's form.
  $form['#webform_id'] = $this->webform
    ->id();
  $form['#webform_handler_id'] = $this->webformHandler
    ->getHandlerId();
  $form['#webform_handler_plugin_id'] = $this->webformHandler
    ->getPluginId();
  $request = $this
    ->getRequest();
  $form['description'] = [
    '#type' => 'container',
    'text' => [
      '#markup' => $this->webformHandler
        ->description(),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ],
    '#weight' => -20,
  ];
  $form['id'] = [
    '#type' => 'value',
    '#value' => $this->webformHandler
      ->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->webformHandler
      ->label(),
    '#required' => TRUE,
    '#attributes' => [
      'autofocus' => 'autofocus',
    ],
  ];
  $form['general']['handler_id'] = [
    '#type' => 'machine_name',
    '#maxlength' => static::MACHINE_NAME_MAXLENGTH,
    '#description' => $this
      ->t('A unique name for this handler instance. Must be alpha-numeric and underscore separated.'),
    '#default_value' => $this->webformHandler
      ->getHandlerId() ?: NULL,
    '#required' => TRUE,
    '#disabled' => $this->webformHandler
      ->getHandlerId() ? TRUE : FALSE,
    '#machine_name' => [
      'source' => [
        'general',
        'label',
      ],
      'exists' => [
        $this,
        'exists',
      ],
    ],
    '#element_validate' => [
      [
        $this,
        'validateMachineName',
      ],
      [
        MachineName::class,
        'validateMachineName',
      ],
    ],
  ];
  $form['general']['notes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Administrative notes'),
    '#description' => $this
      ->t("Entered text will be displayed on the handlers administrative page and replace this handler's default description."),
    '#rows' => 2,
    '#default_value' => $this->webformHandler
      ->getNotes(),
  ];
  $form['advanced'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Advanced settings'),
    '#weight' => -10,
  ];
  $form['advanced']['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable the %name handler.', [
      '%name' => $this->webformHandler
        ->label(),
    ]),
    '#return_value' => TRUE,
    '#default_value' => $this->webformHandler
      ->isEnabled(),
    // Disable broken plugins.
    '#disabled' => $this->webformHandler
      ->getPluginId() === 'broken',
  ];
  $form['#parents'] = [];
  $form['settings'] = [
    '#tree' => TRUE,
    '#parents' => [
      'settings',
    ],
  ];
  $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
  $form['settings'] = $this->webformHandler
    ->buildConfigurationForm($form['settings'], $subform_state);

  // Get $form['settings']['#attributes']['novalidate'] and apply it to the
  // $form.
  // This allows handlers 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;

  // Conditional logic.
  if ($this->webformHandler
    ->supportsConditions()) {
    $form['conditional_logic'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Conditional logic'),
    ];
    $form['conditional_logic']['conditions'] = [
      '#type' => 'webform_element_states',
      '#state_options' => [
        'enabled' => $this
          ->t('Enabled'),
        'disabled' => $this
          ->t('Disabled'),
      ],
      '#selector_options' => $webform
        ->getElementsSelectorOptions([
        'excluded_elements' => [],
      ]),
      '#selector_sources' => $webform
        ->getElementsSelectorSourceValues(),
      '#multiple' => FALSE,
      '#default_value' => $this->webformHandler
        ->getConditions(),
    ];
  }

  // Check the URL for a weight, then the webform handler,
  // otherwise use default.
  $form['weight'] = [
    '#type' => 'hidden',
    '#value' => $request->query
      ->has('weight') ? (int) $request->query
      ->get('weight') : $this->webformHandler
      ->getWeight(),
  ];

  // Build tabs.
  $tabs = [
    'conditions' => [
      'title' => $this
        ->t('Conditions'),
      'elements' => [
        'conditional_logic',
      ],
      'weight' => 10,
    ],
    '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);
}