You are here

public function YamlFormHandlerFormBase::buildForm in YAML Form 8

Parameters

\Drupal\yamlform\YamlFormInterface $yamlform: The form.

string $yamlform_handler: The form 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 form exceeds the handler's cardinality.

Overrides FormInterface::buildForm

2 calls to YamlFormHandlerFormBase::buildForm()
YamlFormHandlerAddForm::buildForm in src/Form/YamlFormHandlerAddForm.php
Form constructor.
YamlFormHandlerEditForm::buildForm in src/Form/YamlFormHandlerEditForm.php
Form constructor.
2 methods override YamlFormHandlerFormBase::buildForm()
YamlFormHandlerAddForm::buildForm in src/Form/YamlFormHandlerAddForm.php
Form constructor.
YamlFormHandlerEditForm::buildForm in src/Form/YamlFormHandlerEditForm.php
Form constructor.

File

src/Form/YamlFormHandlerFormBase.php, line 57

Class

YamlFormHandlerFormBase
Provides a base form for form handlers.

Namespace

Drupal\yamlform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, YamlFormInterface $yamlform = NULL, $yamlform_handler = NULL) {
  $this->yamlform = $yamlform;
  try {
    $this->yamlformHandler = $this
      ->prepareYamlFormHandler($yamlform_handler);
  } catch (PluginNotFoundException $e) {
    throw new NotFoundHttpException("Invalid handler id: '{$yamlform_handler}'.");
  }

  // Limit the number of plugin instanced allowed.
  if (!$this->yamlformHandler
    ->getHandlerId()) {
    $plugin_id = $this->yamlformHandler
      ->getPluginId();
    $cardinality = $this->yamlformHandler
      ->cardinality();
    $number_of_instances = $yamlform
      ->getHandlers($plugin_id)
      ->count();
    if ($cardinality !== YamlFormHandlerInterface::CARDINALITY_UNLIMITED && $cardinality <= $number_of_instances) {
      $t_args = [
        '@number' => $cardinality,
        '@instances' => $this
          ->formatPlural($cardinality, $this
          ->t('instance is'), $this
          ->t('instances are')),
      ];
      throw new NotFoundHttpException($this
        ->t('Only @number @instance permitted', $t_args));
    }
  }
  $request = $this
    ->getRequest();
  $form['description'] = [
    '#markup' => $this->yamlformHandler
      ->description(),
    '#prefix' => '<p>',
    '#suffix' => '</p>',
  ];
  $form['id'] = [
    '#type' => 'value',
    '#value' => $this->yamlformHandler
      ->getPluginId(),
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable the %name handler.', [
      '%name' => $this->yamlformHandler
        ->label(),
    ]),
    '#default_value' => $this->yamlformHandler
      ->isEnabled(),
    // Disable broken plugins.
    '#disabled' => $this->yamlformHandler
      ->getPluginId() == 'broken',
  ];
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#maxlength' => 255,
    '#default_value' => $this->yamlformHandler
      ->label(),
    '#required' => TRUE,
    '#attributes' => [
      'autofocus' => 'autofocus',
    ],
  ];
  $form['handler_id'] = [
    '#type' => 'machine_name',
    '#maxlength' => 64,
    '#description' => $this
      ->t('A unique name for this handler instance. Must be alpha-numeric and underscore separated.'),
    '#default_value' => $this->yamlformHandler
      ->getHandlerId() ?: $this
      ->getUniqueMachineName($this->yamlformHandler),
    '#required' => TRUE,
    '#disabled' => $this->yamlformHandler
      ->getHandlerId() ? TRUE : FALSE,
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
    ],
  ];
  $form['settings'] = $this->yamlformHandler
    ->buildConfigurationForm([], $form_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;

  // Check the URL for a weight, then the form handler,
  // otherwise use default.
  $form['weight'] = [
    '#type' => 'hidden',
    '#value' => $request->query
      ->has('weight') ? (int) $request->query
      ->get('weight') : $this->yamlformHandler
      ->getWeight(),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
  ];
  $form = $this
    ->buildDialog($form, $form_state);
  return $form;
}