class WebformDevelSchema in Webform 8.5
Same name and namespace in other branches
- 6.x modules/webform_devel/src/WebformDevelSchema.php \Drupal\webform_devel\WebformDevelSchema
Provides a webform schema generator.
Hierarchy
- class \Drupal\webform_devel\WebformDevelSchema implements WebformDevelSchemaInterface uses StringTranslationTrait
Expanded class hierarchy of WebformDevelSchema
1 string reference to 'WebformDevelSchema'
- webform_devel.services.yml in modules/
webform_devel/ webform_devel.services.yml - modules/webform_devel/webform_devel.services.yml
1 service uses WebformDevelSchema
- webform_devel.schema in modules/
webform_devel/ webform_devel.services.yml - Drupal\webform_devel\WebformDevelSchema
File
- modules/
webform_devel/ src/ WebformDevelSchema.php, line 23
Namespace
Drupal\webform_develView source
class WebformDevelSchema implements WebformDevelSchemaInterface {
use StringTranslationTrait;
/**
* The element info manager.
*
* @var \Drupal\Core\Render\ElementInfoManagerInterface
*/
protected $elementInfo;
/**
* The webform element manager.
*
* @var \Drupal\webform\Plugin\WebformElementManagerInterface
*/
protected $elementManager;
/**
* Constructs a WebformDevelSchema object.
*
* @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
* The element info manager.
* @param \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager
* The webform element manager.
*/
public function __construct(ElementInfoManagerInterface $element_info, WebformElementManagerInterface $element_manager) {
$this->elementInfo = $element_info;
$this->elementManager = $element_manager;
}
/**
* Get webform scheme columns.
*
* @return array
* Associative array container webform scheme columns
*/
public function getColumns() {
return [
'name' => $this
->t('Name'),
'title' => $this
->t('Title'),
'type' => $this
->t('Type'),
'datatype' => $this
->t('Datatype'),
'maxlength' => $this
->t('Maxlength'),
'required' => $this
->t('Required'),
'multiple' => $this
->t('Multiple'),
'options' => $this
->t('Options'),
];
}
/**
* Get a webform's scheme elements.
*
* @param \Drupal\webform\WebformInterface $webform
* A webform.
*
* @return array
* An associative containing a webform's scheme elements.
*/
public function getElements(WebformInterface $webform) {
$records = [];
$elements = $webform
->getElementsInitializedFlattenedAndHasValue();
foreach ($elements as $element_key => $element) {
$element_plugin = $this->elementManager
->getElementInstance($element);
$records[$element_key] = $this
->getElement($element_key, $element);
if ($element_plugin instanceof WebformCompositeBase) {
$composite_elements = $element_plugin
->getInitializedCompositeElement($element);
foreach ($composite_elements as $composite_element_key => $composite_element) {
$records["{$element_key}.{$composite_element_key}"] = $this
->getElement("{$element_key}.{$composite_element_key}", $composite_element);
}
}
}
return $records;
}
/**
* Get webform element schema.
*
* @param string $element_key
* The webform element key.
* @param array $element
* The webform element.
*
* @return array
* An array containing the schema for the webform element.
*/
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'] = OptGroup::flattenOptions($element['#options']);
}
else {
$data['options'] = [];
}
return $data;
}
/**
* Get element options maxlength from option values.
*
* @param array $element
* An element.
*
* @return int
* An element options maxlength from option values.
*/
protected function getOptionsMaxlength(array $element) {
$options = OptGroup::flattenOptions($element['#options']);
$maxlength = 0;
foreach ($options as $option_value => $option_text) {
$maxlength = max(mb_strlen($option_value), $maxlength);
}
// Check element w/ other value maxlength.
if (preg_match('/_other$/', $element['#type'])) {
if (isset($element['#other__maxlength'])) {
$maxlength = max($element['#other__maxlength'], $maxlength);
}
else {
// @see \Drupal\webform\Plugin\WebformElement\TextField::prepare
$maxlength = max(255, $maxlength);
}
}
return $maxlength;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformDevelSchema:: |
protected | property | The element info manager. | |
WebformDevelSchema:: |
protected | property | The webform element manager. | |
WebformDevelSchema:: |
public | function |
Get webform scheme columns. Overrides WebformDevelSchemaInterface:: |
|
WebformDevelSchema:: |
protected | function | Get webform element schema. | |
WebformDevelSchema:: |
public | function |
Get a webform's scheme elements. Overrides WebformDevelSchemaInterface:: |
|
WebformDevelSchema:: |
protected | function | Get element options maxlength from option values. | |
WebformDevelSchema:: |
public | function | Constructs a WebformDevelSchema object. |