You are here

function _webform_test_get_element_preview in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/modules/webform_test/webform_test.module \_webform_test_get_element_preview()

Get a preview/example element for a specified element type.

Parameters

string $type: An element type.

array $options: Options for generating the example element.

Return value

array|bool An example element or FALSE if the element type can't have an example.

9 calls to _webform_test_get_element_preview()
webform_test_test_element_description_tooltip in tests/modules/webform_test/includes/webform_test.test_element_description_tooltip.inc
Generate test element description tooltip.
webform_test_test_element_disabled in tests/modules/webform_test/includes/webform_test.test_element_disabled.inc
Generate test elements with #disabled set to TRUE.
webform_test_test_element_flexbox in tests/modules/webform_test/includes/webform_test.test_element_flexbox.inc
Generate test elements with Flexbox wrappers.
webform_test_test_element_format in tests/modules/webform_test/includes/webform_test.test_element_format.inc
Generate test element formats.
webform_test_test_element_help_display in tests/modules/webform_test/includes/webform_test.test_element_help_display.inc
Generate test elements help displays.

... See full list

File

tests/modules/webform_test/webform_test.module, line 145
Support module for webform related testing.

Code

function _webform_test_get_element_preview($type, array $options = []) {
  static $skipped = [
    'webform_example_element',
    'webform_example_composite',
    'webform_test_offcanvas_width_element',
  ];

  // Skip certain elements that should never be included in test forms.
  if (in_array($type, $skipped)) {
    return FALSE;
  }
  $options += [
    'issues' => TRUE,
  ];

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  $webform_element = $element_manager
    ->createInstance($type);
  $element = $webform_element
    ->preview();
  if (empty($element)) {
    return FALSE;
  }

  // Skip custom options.
  // @see \Drupal\webform_options_custom\Entity\WebformOptionsCustom::getPreview
  if (isset($element['#type']) && $element['#type'] === 'webform_options_custom') {
    return FALSE;
  }

  // Add known issues to #description.
  if ($options['issues']) {
    $issues = _webform_test_issues();
    if (isset($issues[$type])) {
      $items = [];
      foreach ($issues[$type] as $issue_number => $issue_title) {
        $items[$issue_number] = "<a href=\"https://www.drupal.org/node/{$issue_number}\">Issue #{$issue_number}: {$issue_title}</a>";
      }
      $element['#description'] = '<b>' . t('Known Issues:') . '</b><br />' . implode('<br />', $items);
    }
  }

  // Remove markup so that the element can be serialized to YAML.
  WebformElementHelper::convertRenderMarkupToStrings($element);
  return $element;
}