You are here

protected function WebformUiElementTypeFormBase::buildElementPreview in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_ui/src/Form/WebformUiElementTypeFormBase.php \Drupal\webform_ui\Form\WebformUiElementTypeFormBase::buildElementPreview()

Build and fully initialize and prepare a preview of a webform element.

Parameters

\Drupal\webform\Plugin\WebformElementInterface $webform_element: A webform element plugin.

Return value

array A fully initialized and prepared preview of a webform element.

1 call to WebformUiElementTypeFormBase::buildElementPreview()
WebformUiElementTypeFormBase::buildRow in modules/webform_ui/src/Form/WebformUiElementTypeFormBase.php
Build element type row.

File

modules/webform_ui/src/Form/WebformUiElementTypeFormBase.php, line 295

Class

WebformUiElementTypeFormBase
Provides a abstract element type webform for a webform element.

Namespace

Drupal\webform_ui\Form

Code

protected function buildElementPreview(WebformElementInterface $webform_element) {
  $element = $webform_element
    ->preview();
  if ($element) {
    $webform_element
      ->initialize($element);
    $webform_element
      ->prepare($element, $this->webformSubmission);
    if ($webform_element
      ->hasProperty('title_display') && $webform_element
      ->getDefaultProperty('title_display') !== 'after') {
      $element['#title_display'] = 'invisible';
    }
  }

  // Placeholders.
  switch ($webform_element
    ->getTypeName()) {
    case 'container':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Displays an HTML container. (i.e. @div)', [
        '@div' => '<div>',
      ]));
      break;
    case 'hidden':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Hidden element (less secure, changeable via JavaScript)'));
      break;
    case 'label':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Displays a form label without any associated element. (i.e. @label)', [
        '@label' => '<label>',
      ]));
      break;
    case 'processed_text':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Advanced HTML markup rendered using a text format.'));
      break;
    case 'table':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Displays a custom table. (i.e. @table).', [
        '@table' => '<table>',
      ]) . '<br/><em>' . $this
        ->t('Requires understanding <a href=":href">how to build tables using render arrays</a>.', [
        ':href' => $webform_element
          ->getPluginApiUrl()
          ->toString(),
      ]) . '</em>');
      break;
    case 'value':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Secure value (changeable via server-side code and tokens).'));
      break;
    case 'webform_computed_token':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Allows value to be computed using [tokens].'));
      break;
    case 'webform_computed_twig':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Allows value to be computed using a {{ Twig }} template.'));
      break;
    case 'webform_markup':
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Basic HTML markup.'));
      break;
    case 'webform_section':
      $default_section_title_tag = $this
        ->config('webform.settings')
        ->get('element.default_section_title_tag');
      $element = $this
        ->buildElementPreviewPlaceholder($this
        ->t('Displays a section container (i.e. @section) with a header (i.e. @header).', [
        '@section' => '<section>',
        '@header' => '<' . $default_section_title_tag . '>',
      ]));
      break;
  }

  // Disable all file uploads.
  if ($webform_element instanceof WebformManagedFileBase) {
    $element['#disabled'] = TRUE;
  }

  // Custom element type specific attributes.
  switch ($webform_element
    ->getTypeName()) {
    case 'details':
    case 'fieldset':
    case 'webform_email_confirm':

      // Title needs to be displayed.
      unset($element['#title_display']);
      break;
    case 'textarea':
    case 'webform_codemirror':
    case 'webform_rating':

      // Notice: Undefined index: #value in template_preprocess_textarea()
      // (line 382 of core/includes/form.inc).
      $element['#value'] = '';
      break;
    case 'password':

      // https://stackoverflow.com/questions/15738259/disabling-chrome-autofill
      $element['#attributes']['autocomplete'] = 'new-password';
      break;
    case 'webform_actions':
      $element = [
        '#type' => 'button',
        '#value' => $this
          ->t('Submit'),
        '#attributes' => [
          'onclick' => 'return false;',
        ],
      ];
      break;
    case 'webform_email_multiple':

      // Notice: Undefined index: #description_display in
      // template_preprocess_form_element()
      // (line 476 of core/includes/form.inc).
      $element['#description_display'] = 'after';
      break;
    case 'webform_flexbox':
      $element['#type'] = 'webform_flexbox';
      $element += [
        'element_flex_1' => [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Flex: 1'),
          '#flex' => 1,
          '#prefix' => '<div class="webform-flex webform-flex--1"><div class="webform-flex--container">',
          '#suffix' => '</div></div>',
        ],
        'element_flex_2' => [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Flex: 2'),
          '#flex' => 2,
          '#prefix' => '<div class="webform-flex webform-flex--2"><div class="webform-flex--container">',
          '#suffix' => '</div></div>',
        ],
      ];
      break;
    case 'webform_toggles':
      $element['#options_display'] = 'side_by_side';
      break;
    case 'webform_terms_of_service':
      unset($element['#title_display']);
      break;
  }

  // Add placeholder for empty element.
  if (empty($element)) {
    $element = $this
      ->buildElementPreviewPlaceholder($this
      ->t('No preview available.'));
  }

  // Required attributes.
  $element['#id'] = $webform_element
    ->getTypeName();
  $element['#webform_key'] = $webform_element
    ->getTypeName();
  return $element;
}