You are here

private static function WebformContentCreatorUtilities::buildTree in Webform Content Creator 8

Same name and namespace in other branches
  1. 3.x 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) {
  $elementsDefinitions = \Drupal::service('plugin.manager.webform.element')
    ->getDefinitions();
  $layoutElements = [
    'webform_wizard_page',
    'container',
    'details',
    'fieldset',
    'webform_flexbox',
  ];
  $result = [];
  $webformFieldIds = 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 ($webformFieldIds 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"], $layoutElements, TRUE)) {
      if ($elements[$v]["#webform_parent_key"] !== '') {
        continue;
      }

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

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