You are here

protected function WebformDevelEntityFormApiBaseForm::varExport 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::varExport()

Outputs string representation of a variable using array short syntax.

Parameters

mixed $expression: The variable you want to export.

bool $return: If used and set to TRUE, var_export() will return the variable representation instead of outputting it.

Return value

string Returns the variable representation when the return parameter is used and evaluates to TRUE. Otherwise, this function will return NULL.

1 call to WebformDevelEntityFormApiBaseForm::varExport()
WebformDevelEntityFormApiBaseForm::renderExport in modules/webform_devel/src/Form/WebformDevelEntityFormApiBaseForm.php
Export a PHP render array.

File

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

Class

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

Namespace

Drupal\webform_devel\Form

Code

protected function varExport($expression, $return = FALSE) {

  // Export variable using array short syntax.
  // @see https://gist.github.com/Bogdaan/ffa287f77568fcbb4cffa0082e954022
  $export = var_export($expression, TRUE);
  $export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
  $array = preg_split("/\r\n|\n|\r/", $export);
  $array = preg_replace([
    "/\\s*array\\s\\(\$/",
    "/\\)(,)?\$/",
    "/\\s=>\\s\$/",
  ], [
    NULL,
    ']$1',
    ' => [',
  ], $array);
  $export = implode(PHP_EOL, array_filter([
    "[",
  ] + $array));

  // Clean up output to match Drupal coding guidelines.
  $export = str_replace('    ', '  ', $export);
  $export = str_replace('=> true,', '=> TRUE,', $export);
  $export = str_replace('=> false,', '=> FALSE,', $export);
  $export = preg_replace('/\\d+ => /', '', $export);
  if ($return) {
    return $export;
  }
  else {
    echo $export;
  }
}