View source
<?php
namespace Drupal\field_nif\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElement\TextBase;
use Drupal\webform\WebformSubmissionInterface;
class Nif extends TextBase {
public function getDefaultProperties() {
$properties = [
'title' => '',
'default_value' => '',
'supported_types' => [
'nif',
'nie',
'cif',
],
'title_display' => '',
'description_display' => '',
'field_prefix' => '',
'field_suffix' => '',
'disabled' => FALSE,
'required' => FALSE,
'required_error' => '',
'unique' => FALSE,
'unique_error' => '',
'wrapper_attributes' => [],
'attributes' => [],
'states' => [],
'access_create_roles' => [
'anonymous',
'authenticated',
],
'access_create_users' => [],
'access_update_roles' => [
'anonymous',
'authenticated',
],
'access_update_users' => [],
'access_view_roles' => [
'anonymous',
'authenticated',
],
'access_view_users' => [],
];
return $properties;
}
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
$element['#type'] = 'nif';
parent::prepare($element, $webform_submission);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['supported_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Supported document types'),
'#parents' => [
'properties',
'supported_types',
],
'#default_value' => $form_state
->get('custom_properties')['supported_types'],
'#options' => [
'nif' => $this
->t('NIF'),
'cif' => $this
->t('CIF'),
'nie' => $this
->t('NIE'),
],
'#required' => TRUE,
'#weight' => -10,
];
$form['form']['maxlength'] = [
'#type' => 'value',
'#value' => 10,
];
return $form;
}
public function getFormElementClassDefinition() {
$definition = $this->elementInfo
->getDefinition('nif');
return $definition['class'];
}
}