You are here

protected function WebformCompositeBase::buildCompositeElementsTable in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformCompositeBase.php \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::buildCompositeElementsTable()

Build the composite elements settings table.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array A renderable array container the composite elements settings table.

1 call to WebformCompositeBase::buildCompositeElementsTable()
WebformCompositeBase::form in src/Plugin/WebformElement/WebformCompositeBase.php
Gets the actual configuration webform array to be built.
2 methods override WebformCompositeBase::buildCompositeElementsTable()
WebformCustomComposite::buildCompositeElementsTable in src/Plugin/WebformElement/WebformCustomComposite.php
Build the composite elements settings table.
WebformLocationBase::buildCompositeElementsTable in src/Plugin/WebformElement/WebformLocationBase.php
Build the composite elements settings table.

File

src/Plugin/WebformElement/WebformCompositeBase.php, line 978

Class

WebformCompositeBase
Provides a base for composite elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function buildCompositeElementsTable(array $form, FormStateInterface $form_state) {
  $labels_help = [
    'help' => [
      '#type' => 'webform_help',
      '#help' => '<b>' . $this
        ->t('Key') . ':</b> ' . $this
        ->t('The machine-readable name.') . '<hr/><b>' . $this
        ->t('Title') . ':</b> ' . $this
        ->t('This is used as a descriptive label when displaying this webform element.') . '<hr/><b>' . $this
        ->t('Placeholder') . ':</b> ' . $this
        ->t('The placeholder will be shown in the element until the user starts entering a value.') . '<hr/><b>' . $this
        ->t('Description') . ':</b> ' . $this
        ->t('A short description of the element used as help for the user when they use the webform.') . '<hr/><b>' . $this
        ->t('Help text') . ':</b> ' . $this
        ->t('A tooltip displayed after the title.') . '<hr/><b>' . $this
        ->t('Title display') . ':</b> ' . $this
        ->t('A tooltip displayed after the title.'),
      '#help_title' => $this
        ->t('Labels'),
    ],
  ];
  $settings_help = [
    'help' => [
      '#type' => 'webform_help',
      '#help' => '<b>' . $this
        ->t('Required') . ':</b> ' . $this
        ->t('Check this option if the user must enter a value.') . '<hr/><b>' . $this
        ->t('Type') . ':</b> ' . $this
        ->t('The type of element to be displayed.') . '<hr/><b>' . $this
        ->t('Options') . ':</b> ' . $this
        ->t('Please select predefined options.'),
      '#help_title' => $this
        ->t('Settings'),
    ],
  ];
  $header = [
    'visible' => $this
      ->t('Visible'),
    'labels' => [
      'data' => [
        [
          'title' => [
            '#markup' => $this
              ->t('Labels'),
          ],
        ],
        $labels_help,
      ],
    ],
    'settings' => [
      'data' => [
        [
          'title' => [
            '#markup' => $this
              ->t('Settings'),
          ],
        ],
        $settings_help,
      ],
    ],
  ];
  $rows = [];
  $composite_elements = $this
    ->getCompositeElements();
  foreach ($composite_elements as $composite_key => $composite_element) {
    $title = isset($composite_element['#title']) ? $composite_element['#title'] : $composite_key;
    $type = isset($composite_element['#type']) ? $composite_element['#type'] : NULL;
    $t_args = [
      '@title' => $title,
    ];
    $state_disabled = [
      'disabled' => [
        ':input[name="properties[' . $composite_key . '__access]"]' => [
          'checked' => FALSE,
        ],
      ],
    ];
    $row = [];

    // Access.
    $row[$composite_key . '__access'] = [
      '#title' => $this
        ->t('@title visible', $t_args),
      '#title_display' => 'invisible',
      '#type' => 'checkbox',
      '#return_value' => TRUE,
    ];

    // Key, title, placeholder, help, description, and title display.
    if ($type) {
      $row['labels'] = [
        'data' => [
          $composite_key . '__key' => [
            '#markup' => $composite_key,
            '#suffix' => '<hr/>',
            '#access' => TRUE,
          ],
          $composite_key . '__title' => [
            '#type' => 'textfield',
            '#title' => $this
              ->t('@title title', $t_args),
            '#title_display' => 'invisible',
            '#description' => $this
              ->t('This is used as a descriptive label when displaying this webform element.'),
            '#description_display' => 'invisible',
            '#placeholder' => $this
              ->t('Enter title…'),
            '#required' => TRUE,
            '#states' => $state_disabled,
          ],
          $composite_key . '__placeholder' => [
            '#type' => 'textfield',
            '#title' => $this
              ->t('@title placeholder', $t_args),
            '#title_display' => 'invisible',
            '#description' => $this
              ->t('The placeholder will be shown in the element until the user starts entering a value.'),
            '#description_display' => 'invisible',
            '#placeholder' => $this
              ->t('Enter placeholder…'),
            '#states' => $state_disabled,
          ],
          $composite_key . '__help' => [
            '#type' => 'textarea',
            '#title' => $this
              ->t('@title help text', $t_args),
            '#title_display' => 'invisible',
            '#description' => $this
              ->t('A short description of the element used as help for the user when they use the webform.'),
            '#description_display' => 'invisible',
            '#rows' => 2,
            '#placeholder' => $this
              ->t('Enter help text…'),
            '#states' => $state_disabled,
          ],
          $composite_key . '__description' => [
            '#type' => 'textarea',
            '#title' => $this
              ->t('@title description', $t_args),
            '#title_display' => 'invisible',
            '#description' => $this
              ->t('A tooltip displayed after the title.'),
            '#description_display' => 'invisible',
            '#rows' => 2,
            '#placeholder' => $this
              ->t('Enter description…'),
            '#states' => $state_disabled,
          ],
          $composite_key . '__title_display' => [
            '#type' => 'select',
            '#title' => $this
              ->t('@title title display', $t_args),
            '#title_display' => 'invisible',
            '#description' => $this
              ->t('A tooltip displayed after the title.'),
            '#description_display' => 'invisible',
            '#options' => [
              'before' => $this
                ->t('Before'),
              'after' => $this
                ->t('After'),
              'inline' => $this
                ->t('Inline'),
              'invisible' => $this
                ->t('Invisible'),
            ],
            '#empty_option' => $this
              ->t('Select title display… '),
            '#states' => $state_disabled,
          ],
        ],
      ];
    }
    else {
      $row['title_and_description'] = [
        'data' => [
          '',
        ],
      ];
    }

    // Type and options.
    // Using if/else instead of switch/case because of complex conditions.
    $row['settings'] = [];

    // Required.
    if ($type) {
      $row['settings']['data'][$composite_key . '__required'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Required'),
        '#description' => $this
          ->t('Check this option if the user must enter a value.'),
        '#description_display' => 'invisible',
        '#return_value' => TRUE,
        '#states' => $state_disabled,
        '#wrapper_attributes' => [
          'style' => 'white-space: nowrap',
        ],
        '#suffix' => '<hr/>',
      ];
    }
    if ($type === 'tel') {
      $row['settings']['data'][$composite_key . '__type'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('@title type', $t_args),
        '#title_display' => 'invisible',
        '#description' => $this
          ->t('The type of element to be displayed.'),
        '#description_display' => 'invisible',
        '#required' => TRUE,
        '#options' => [
          'tel' => $this
            ->t('Telephone'),
          'textfield' => $this
            ->t('Text field'),
        ],
        '#states' => $state_disabled,
      ];
    }
    elseif (in_array($type, [
      'select',
      'webform_select_other',
      'radios',
      'webform_radios_other',
    ])) {

      // Get base type (select or radios).
      $base_type = preg_replace('/webform_(select|radios)_other/', '\\1', $type);

      // Get type options.
      switch ($base_type) {
        case 'radios':
          $settings = [
            'radios' => $this
              ->t('Radios'),
            'webform_radios_other' => $this
              ->t('Radios other'),
            'textfield' => $this
              ->t('Text field'),
          ];
          break;
        case 'select':
        default:
          $settings = [
            'select' => $this
              ->t('Select'),
            'webform_select_other' => $this
              ->t('Select other'),
            'textfield' => $this
              ->t('Text field'),
          ];
          break;
      }
      $row['settings']['data'][$composite_key . '__type'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('@title type', $t_args),
        '#title_display' => 'invisible',
        '#description' => $this
          ->t('The type of element to be displayed.'),
        '#description_display' => 'invisible',
        '#required' => TRUE,
        '#options' => $settings,
        '#states' => $state_disabled,
      ];
      $composite_options = $this
        ->getCompositeElementOptions($composite_key);

      // Make sure custom options defined via the YAML source are not
      // deleted when a composite element is edited via the UI.

      /** @var \Drupal\webform_ui\Form\WebformUiElementEditForm $form_object */
      $form_object = $form_state
        ->getFormObject();
      $element = $form_object
        ->getElement();
      $composite_options_default_value = isset($element['#' . $composite_key . '__options']) ? $element['#' . $composite_key . '__options'] : NULL;
      if ($composite_options_default_value && (is_array($composite_options_default_value) || !isset($composite_options[$composite_options_default_value]))) {
        $webform = $form_object
          ->getWebform();
        if ($this->currentUser
          ->hasPermission('edit webform source') && $webform
          ->hasLinkTemplate('source-form')) {
          $t_args = [
            ':href' => $webform
              ->toUrl('source-form')
              ->toString(),
          ];
          $message = $this
            ->t('Custom options can only be updated via the <a href=":href">YAML source</a>.', $t_args);
        }
        else {
          $message = $this
            ->t('Custom options can only be updated via the YAML source.');
        }
        $row['settings']['data'][$composite_key . '__options'] = [
          '#type' => 'value',
          '#suffix' => '<em>' . $message . '</em>',
        ];
      }
      elseif ($composite_options) {
        $row['settings']['data'][$composite_key . '__options'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('@title options', $t_args),
          '#title_display' => 'invisible',
          '#description' => $this
            ->t('Please select predefined options.'),
          '#description_display' => 'invisible',
          '#options' => $composite_options,
          '#required' => TRUE,
          '#attributes' => [
            'style' => 'width: 100%;',
          ],
          '#states' => $state_disabled + [
            'invisible' => [
              ':input[name="properties[' . $composite_key . '__type]"]' => [
                'value' => 'textfield',
              ],
            ],
          ],
        ];
      }
      else {
        $row['settings']['data'][$composite_key . '__options'] = [
          '#type' => 'value',
        ];
      }
    }
    else {
      $row['settings']['data'][$composite_key . '__type'] = [
        '#type' => 'textfield',
        '#access' => FALSE,
      ];
      $row['settings']['data']['markup'] = [
        '#type' => 'container',
        '#markup' => $this->elementManager
          ->getElementInstance($composite_element)
          ->getPluginLabel(),
        '#access' => TRUE,
      ];
    }
    $rows[$composite_key] = $row;
  }
  return [
    '#type' => 'table',
    '#header' => $header,
  ] + $rows;
}