You are here

public function WebformTableRow::getDefaultKey in Webform 6.x

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

Gets the element's default key.

Return value

string The element's default key.

Overrides WebformElementBase::getDefaultKey

File

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

Class

WebformTableRow
Provides a 'webform_table_row' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function getDefaultKey() {
  $webform = $this
    ->getWebform();
  $parent_key = \Drupal::request()->query
    ->get('parent');
  $table_element = $webform
    ->getElement($parent_key);

  // Make sure prefixing elements is enabled for the table.
  $prefix_children = !isset($table_element['#prefix_children']) || $table_element['#prefix_children'] === TRUE;
  if (!$prefix_children) {
    return NULL;
  }

  // Return the first rows keys based on the parent title.
  if (empty($table_element['#webform_children'])) {
    return $parent_key . '_01';
  }

  // Replace increment in first row.
  $first_row_key = reset($table_element['#webform_children']);
  $increment = $this
    ->getNextIncrement();
  $increment = str_pad($increment, 2, '0', STR_PAD_LEFT);
  return preg_replace('/\\d+/', $increment, $first_row_key);
}