You are here

public function WebformTableRow::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformTableRow.php \Drupal\webform\Plugin\WebformElement\WebformTableRow::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformElementBase::form

File

src/Plugin/WebformElement/WebformTableRow.php, line 163

Class

WebformTableRow
Provides a 'webform_table_row' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\webform_ui\Form\WebformUiElementFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();

  // Handle new row being added to a table.
  if ($form_object
    ->isNew()) {
    $parent_key = $form_object
      ->getParentKey();

    // Make sure the new row is being inserted into a table.
    $table_element = $form_object
      ->getWebform()
      ->getElement($parent_key);
    if (!$table_element || $table_element['#type'] !== 'webform_table') {
      throw new NotFoundHttpException();
    }

    // Make sure the table support prefixing.
    $prefix_children = !isset($table_element['#prefix_children']) || $table_element['#prefix_children'] === TRUE;
    if (!$prefix_children) {
      return $form;
    }
    $form['element']['table_message'] = [
      '#type' => 'webform_message',
      '#message_message' => $this
        ->t("Row keys are the tables's key with an incremented value."),
      '#message_type' => 'warning',
      '#message_close' => TRUE,
      '#message_storage' => WebformMessageElement::STORAGE_SESSION,
      '#weight' => -98,
      '#access' => TRUE,
    ];
    $element_properties = $form_state
      ->get('element_properties');

    // Set duplicate and incremented title element properties.
    if ($table_element['#webform_children']) {
      $first_row_key = reset($table_element['#webform_children']);
      $first_row_element = $this
        ->getWebform()
        ->getElement($first_row_key);
      if ($this
        ->hasIncrementalChildrenElements($first_row_key)) {
        $form['table_settings'] = [
          '#type' => 'fieldset',
          '#title' => $this
            ->t('Table row settings'),
        ];
        $form['table_settings']['duplicate'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t("Duplicate the table's first row"),
          '#return_value' => TRUE,
        ];
        $element_properties['duplicate'] = TRUE;
      }
      $element_properties['title'] = preg_replace('/\\d+/', $this
        ->getNextIncrement(), $first_row_element['#title']);
    }
    else {
      $element_properties['title'] = $table_element['#title'] . ' (1)';
    }
    $form_state
      ->set('element_properties', $element_properties);
  }
  return $form;
}