You are here

protected function WebformTableRow::getChildrenElements in Webform 6.x

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

Get child elements incremented with a new index.

Parameters

string $element_key: The element key.

int $index: The index for all child elements.

Return value

array|bool Child elements or FALSE is child element keys are not incremental.

1 call to WebformTableRow::getChildrenElements()
WebformTableRow::getConfigurationFormProperties in src/Plugin/WebformElement/WebformTableRow.php
Get an associative array of element properties from configuration webform.

File

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

Class

WebformTableRow
Provides a 'webform_table_row' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function getChildrenElements($element_key, $index) {
  $webform = $this
    ->getWebform();
  $element = $webform
    ->getElement($element_key);
  $elements = [];
  foreach ($element['#webform_children'] as $child_key) {

    // Return FALSE if the child key is not incremental.
    if (!preg_match('/\\d+/', $child_key, $match)) {
      return FALSE;
    }

    // Set incremented key.
    $increment = str_pad($index, 2, '0', STR_PAD_LEFT);
    $increment_key = preg_replace('/\\d+/', $increment, $child_key);

    // Return FALSE if the sub element already exists.
    if ($webform
      ->getElement($increment_key)) {
      return FALSE;
    }

    // Get the decoded element.
    $element = $webform
      ->getElementDecoded($child_key);

    // Increment the element's #title and #admin_title.
    $increment_properties = [
      '#title',
      '#admin_title',
    ];
    foreach ($increment_properties as $increment_property) {
      if (isset($element[$increment_property])) {
        $element[$increment_property] = preg_replace('/\\d+/', $index, $element[$increment_property]);
      }
    }

    // Get child elements.
    $child_elements = $this
      ->getChildrenElements($child_key, $index);

    // Return FALSE if any child element is not incremented.
    if ($child_elements === FALSE) {
      return FALSE;
    }

    // Set new incremented element with child elements.
    $elements[$increment_key] = $element + $child_elements;
  }
  return $elements;
}