You are here

protected function WebformDevelEntityFormApiBaseForm::renderExport in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_devel/src/Form/WebformDevelEntityFormApiBaseForm.php \Drupal\webform_devel\Form\WebformDevelEntityFormApiBaseForm::renderExport()

Export a PHP render array.

Parameters

array $form: A form.

string $prefix: The render arrays prefix.

Return value

string Returns the variable representation of the render array.

1 call to WebformDevelEntityFormApiBaseForm::renderExport()
WebformDevelEntityFormApiExportForm::buildForm in modules/webform_devel/src/Form/WebformDevelEntityFormApiExportForm.php
Form constructor.

File

modules/webform_devel/src/Form/WebformDevelEntityFormApiBaseForm.php, line 124

Class

WebformDevelEntityFormApiBaseForm
Export a webform's element to Form API (FAPI).

Namespace

Drupal\webform_devel\Form

Code

protected function renderExport(array $form, $prefix = '$form') {
  $output = '';
  foreach ($form as $element_key => $element) {
    $element_prefix = $prefix . "['" . $element_key . "']";
    if (!is_array($element)) {
      $output .= $element_prefix . '[' . $element_key . '] = ' . var_export($element, TRUE) . ';' . PHP_EOL;
    }
    elseif ($prefix === '$form' && !Element::child($element_key)) {
      $output .= $element_prefix . ' = ' . $this
        ->varExport($element, TRUE) . ';' . PHP_EOL;
    }
    else {
      $element_plugin = is_array($element) ? $this->elementManager
        ->getElementInstance($element) : NULL;
      $element_children = [];
      $element_export = [];
      foreach ($element as $property => $value) {
        if (Element::child($property)) {
          $element_children[$property] = $value;
        }
        elseif ($this
          ->isPropertyTranslatable($property)) {
          $element_export[$property] = $this
            ->wrapTranslatableValue($value);
        }
        else {
          $element_export[$property] = $value;
        }
      }

      // Add comment for main container element.
      if ($prefix === '$form' && $element_plugin && $element_plugin
        ->isContainer($element)) {
        $output .= PHP_EOL . '// ' . $element_plugin
          ->getAdminLabel($element) . '.' . PHP_EOL;
      }
      $output .= $element_prefix . ' = ' . $this
        ->varExport($element_export, TRUE) . ';' . PHP_EOL;
      $output .= $this
        ->renderExport($element_children, $element_prefix);
    }
  }
  $output = str_replace("'<T>", "\$this->t('", $output);
  $output = str_replace("</T>'", "')", $output);
  return $output;
}