You are here

protected function WebformDevelSchema::getElement in Webform 6.x

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

Get webform element schema.

Parameters

string $element_key: The webform element key.

array $element: The webform element.

Return value

array An array containing the schema for the webform element.

1 call to WebformDevelSchema::getElement()
WebformDevelSchema::getElements in modules/webform_devel/src/WebformDevelSchema.php
Get a webform's scheme elements.

File

modules/webform_devel/src/WebformDevelSchema.php, line 205

Class

WebformDevelSchema
Provides a webform schema generator.

Namespace

Drupal\webform_devel

Code

protected function getElement($element_key, array $element) {
  $element_info = $this->elementInfo
    ->getInfo($element['#type']);
  $element_plugin = $this->elementManager
    ->getElementInstance($element);
  $data = [];

  // Name.
  $data['name'] = $element_key;

  // Title.
  if (isset($element['#admin_title'])) {
    $title = $element['#admin_title'];
  }
  elseif (isset($element['#title'])) {
    $title = $element['#title'];
  }
  else {
    $title = $element_key;
  }
  $data['title'] = $title;

  // Element type.
  $data['type'] = $element['#type'];

  // Datatype.
  if ($element_plugin instanceof WebformCompositeBase) {
    $datatype = 'Composite';
  }
  elseif ($element_plugin instanceof BooleanBase) {
    $datatype = 'Boolean';
  }
  elseif ($element_plugin instanceof DateBase) {
    $datatype = 'Date';
  }
  elseif ($element_plugin instanceof NumericBase) {
    $datatype = 'Number';
  }
  elseif ($element_plugin instanceof Textarea) {
    $datatype = 'Blob';
  }
  elseif ($element_plugin instanceof WebformManagedFileBase) {
    $datatype = 'Number';
  }
  elseif ($element_plugin instanceof WebformElementEntityReferenceInterface) {
    $datatype = 'Number';
  }
  else {
    $datatype = 'Text';
  }
  $data['datatype'] = $datatype;

  // Maxlength.
  if (isset($element['#maxlength'])) {
    $maxlength = $element['#maxlength'];
  }
  elseif (isset($element['#options'])) {
    $maxlength = $this
      ->getOptionsMaxlength($element);
  }
  elseif ($element_plugin instanceof TextField) {

    // @see \Drupal\webform\Plugin\WebformElement\TextField::prepare
    $maxlength = '255';
  }
  elseif (isset($element_info['#maxlength'])) {
    $maxlength = $element_info['#maxlength'];
  }
  else {
    switch ($element['#type']) {
      case 'color':
        $maxlength = 7;
        break;
      case 'email':
      case 'webform_email_confirm':
        $maxlength = EmailElement::EMAIL_MAX_LENGTH;
        break;
      case 'password_confirm':
        $maxlength = $this->elementInfo
          ->getInfo('password')['#maxlength'];
        break;
      case 'textarea':
      case 'text_format':
      case 'webform_signature':
      case 'webform_codemirror':
      case 'webform_email_multiple':
        $maxlength = $this
          ->t('Unlimited');
        break;
      default:
        $maxlength = '';
        break;
    }
  }
  $data['maxlength'] = $maxlength;

  // Required.
  $data['required'] = !empty($element['#required']) ? $this
    ->t('Yes') : $this
    ->t('No');

  // Multiple.
  if (isset($element['#multiple'])) {
    $multiple = $element['#multiple'] > 1 ? $element['#multiple'] : $this
      ->t('Unlimited');
  }
  else {
    $multiple = '1';
  }
  $data['multiple'] = $multiple;
  if (isset($element['#options'])) {
    $data['options_text'] = OptGroup::flattenOptions($element['#options']);
    $data['options_value'] = array_keys(OptGroup::flattenOptions($element['#options']));
  }
  else {
    $data['options_text'] = [];
    $data['options_value'] = [];
  }
  return $data;
}