You are here

private static function WebformContentCreatorUtilities::buildTree in Webform Content Creator 3.x

Same name and namespace in other branches
  1. 8 src/WebformContentCreatorUtilities.php \Drupal\webform_content_creator\WebformContentCreatorUtilities::buildTree()
  2. 2.x src/WebformContentCreatorUtilities.php \Drupal\webform_content_creator\WebformContentCreatorUtilities::buildTree()

Contructs a tree with webform elements which can be used in Selects.

Parameters

array $elements: Webform elements.

Return value

array Tree with webform elements

1 call to WebformContentCreatorUtilities::buildTree()
WebformContentCreatorUtilities::getWebformElements in src/WebformContentCreatorUtilities.php
Get webform elements and properties structured as a tree.

File

src/WebformContentCreatorUtilities.php, line 48

Class

WebformContentCreatorUtilities
Provides useful functions required in Webform content creator module.

Namespace

Drupal\webform_content_creator

Code

private static function buildTree(array $elements) {
  $definitions = \Drupal::service('plugin.manager.webform.element')
    ->getDefinitions();
  $layout_elements = [
    'webform_wizard_page',
    'container',
    'details',
    'fieldset',
    'webform_flexbox',
  ];
  $result = [];
  $webform_field_ids = array_keys($elements);

  // Default value, only used if there are no wizard pages in webform.
  $wizardPage = t('Webform elements');

  // Check which element is the first wizard page (in case it exists)
  $flag = 0;
  $aux = [];
  foreach ($webform_field_ids as $v) {
    if ($v === 'actions') {
      continue;
    }
    $title = 'Section';
    if (isset($elements[$v]['#title'])) {
      $title = $elements[$v]['#title'];
    }
    else {
      if (isset($elements[$v]['#markup'])) {
        $title = $elements[$v]['#markup'];
      }
    }
    if (in_array($elements[$v]["#type"], $layout_elements, TRUE)) {
      if ($elements[$v]["#webform_parent_key"] !== '') {
        continue;
      }

      // Executes only for the first wizard page (first optgroup in select)
      if ($flag === 0) {
        $wizard_page = html_entity_decode($title);
        unset($aux);
        $flag++;
        continue;
      }
      if (!empty($aux)) {
        foreach ($aux as $k2 => $v2) {
          $result[$wizard_page][$k2] = $v2;
        }
      }
      $wizard_page = html_entity_decode($title);
      unset($aux);
    }
    elseif ($elements[$v]["#webform_parent_key"] === '') {
      $result['0,' . $v] = html_entity_decode($title) . ' (' . $v . ') - ' . $definitions[$elements[$v]["#type"]]['label'];
    }
    elseif ($elements[$v]["#type"] !== "webform_section") {
      $aux['0,' . $v] = html_entity_decode($title) . ' (' . $v . ') - ' . $definitions[$elements[$v]["#type"]]['label'];
    }
  }

  // Organize webform elements as a tree (wizard pages as optgroups)
  foreach ($aux as $k2 => $v2) {
    $result[$wizard_page][$k2] = $v2;
  }
  return $result;
}