You are here

public function WebformNodeReferencesAddForm::buildForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_node/src/Form/WebformNodeReferencesAddForm.php \Drupal\webform_node\Form\WebformNodeReferencesAddForm::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.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/webform_node/src/Form/WebformNodeReferencesAddForm.php, line 63

Class

WebformNodeReferencesAddForm
Form for adding webform node variants.

Namespace

Drupal\webform_node\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL) {
  $bundles = [];

  /** @var \Drupal\field\FieldConfigInterface[] $field_configs */
  $field_configs = $this->entityTypeManager
    ->getStorage('field_config')
    ->loadByProperties([
    'entity_type' => 'node',
  ]);
  foreach ($field_configs as $field_config) {
    if ($field_config
      ->get('field_type') === 'webform') {
      $bundle = $field_config
        ->get('bundle');
      $node_type = $this->entityTypeManager
        ->getStorage('node_type')
        ->load($bundle);
      $bundles[$bundle] = $node_type
        ->label();
    }
  }
  $form['description'] = [
    '#type' => 'container',
    'text' => [
      '#markup' => $this
        ->t('Enter webform information and then click submit, which will redirect you to the appropriate create content form.'),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ],
  ];
  $form['webform_id'] = [
    '#type' => 'value',
    '#value' => $webform
      ->id(),
  ];
  $form['webform_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $webform
      ->label(),
    '#required' => TRUE,
  ];
  $form['bundle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Content type'),
    '#options' => $bundles,
    '#required' => TRUE,
  ];
  $element_keys = $webform
    ->getElementsVariant();
  if (isset($element_keys)) {
    $form['webform_default_data'] = [
      '#tree' => TRUE,
    ];
    foreach ($element_keys as $element_key) {
      $element = $webform
        ->getElement($element_key);
      $variants = $webform
        ->getVariants(NULL, TRUE, $element_key);
      if (!$variants
        ->count()) {
        continue;
      }
      $variant_options = [];
      foreach ($variants as $variant) {
        if ($variant
          ->isEnabled()) {
          $variant_options[$variant
            ->getVariantId()] = $variant
            ->label();
        }
      }
      if ($variant_options) {
        $form['webform_default_data'][$element_key] = [
          '#type' => 'select',
          '#title' => WebformElementHelper::getAdminTitle($element),
          '#options' => $variant_options,
          '#empty_option' => $this
            ->t('- None -'),
        ];
      }
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Create content'),
    '#button_type' => 'primary',
  ];
  return $form;
}