View source
<?php
namespace Drupal\webform_options_custom\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Element\WebformHtmlEditor;
use Drupal\webform\Element\WebformMessage;
use Drupal\webform\Entity\WebformOptions;
use Drupal\webform\Plugin\WebformElement\Select;
use Drupal\webform\Plugin\WebformElementAssetInterface;
use Drupal\webform\Utility\WebformOptionsHelper;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_options_custom\Plugin\WebformOptionsCustomInterface;
class WebformOptionsCustom extends Select implements WebformOptionsCustomInterface, WebformElementAssetInterface {
protected function defineDefaultProperties() {
$properties = [
'multiple' => FALSE,
'multiple_error' => '',
'empty_option' => '',
'empty_value' => '',
'select2' => FALSE,
'choices' => FALSE,
'chosen' => FALSE,
'placeholder' => '',
'help_display' => '',
'size' => '',
'options_custom' => '',
'options_description_display' => TRUE,
] + parent::defineDefaultProperties();
unset($properties['options_randomize']);
return $properties;
}
public function supportsMultipleValues() {
return TRUE;
}
public function initialize(array &$element) {
unset($element['#template']);
}
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
$this
->setOptions($element, [
'webform_submission' => $webform_submission,
]);
parent::prepare($element, $webform_submission);
}
protected function getElementSelectorInputsOptions(array $element) {
$this
->setOptions($element);
$title = $this
->getAdminLabel($element);
return [
'select' => $title . ' [' . $this
->t('Select') . ']',
];
}
public function getElementSelectorSourceValues(array $element) {
$this
->setOptions($element);
return parent::getElementSelectorSourceValues($element);
}
protected function build($format, array &$element, WebformSubmissionInterface $webform_submission, array $options = []) {
$this
->setOptions($element, [
'webform_submission' => $webform_submission,
]);
return parent::build($format, $element, $webform_submission, $options);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
list(, $webform_options_custom_id) = explode(':', $this
->getPluginId());
$webform_options_custom = $this->entityTypeManager
->getStorage('webform_options_custom')
->load($webform_options_custom_id);
$element = $webform_options_custom
->getElement();
$this
->setOptions($element);
$rows = [];
foreach ($element['#options'] as $value => $text) {
$rows[] = [
$value,
[
'data' => [
'#markup' => str_replace(WebformOptionsHelper::DESCRIPTION_DELIMITER, '<br/>', $text),
],
],
];
}
if ($this
->hasProperty('options')) {
$form['options']['options']['#options_description'] = TRUE;
if ($this
->getEntity()
->getOptions() || $this
->getEntity()
->getTemplateOptions()) {
$form['options']['options']['#type'] = 'webform_options';
$form['options']['options']['#required'] = FALSE;
$form['options']['options_message'] = [
'#type' => 'webform_message',
'#message_type' => 'info',
'#message_message' => $this
->t('Below options are used to enhance and also translate the custom options.'),
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
'#access' => TRUE,
'#weight' => -20,
];
}
}
if ($rows) {
$form['options']['custom_options'] = [
'#type' => 'details',
'#title' => $this
->t('Custom options'),
'table' => [
'#type' => 'table',
'#header' => [
$this
->t('Option value'),
$this
->t('Option text / description'),
],
'#rows' => $rows,
'#access' => TRUE,
],
'#access' => TRUE,
'#weight' => -20,
];
}
$form['options']['#description'] = WebformHtmlEditor::checkMarkup($webform_options_custom
->get('help'));
unset($form['options']['options_display_container']);
return $form;
}
public function preview() {
list(, $webform_options_custom_id) = explode(':', $this
->getPluginId());
$webform_options_custom = $this->entityTypeManager
->getStorage('webform_options_custom')
->load($webform_options_custom_id);
return $webform_options_custom
->getPreview() + [
'#title' => $webform_options_custom
->label(),
];
}
public function getTestValues(array $element, WebformInterface $webform, array $options = []) {
$this
->setOptions($element);
return array_keys($element['#options']);
}
public function hasAssets() {
return $this
->getCss() || $this
->getJavaScript() ? TRUE : FALSE;
}
public function getAssetId() {
return $this
->getEntity()
->id();
}
public function getCss() {
return $this
->getEntity()
->get('css');
}
public function getJavaScript() {
return $this
->getEntity()
->get('javascript');
}
protected function setOptions(array &$element, array $settings = []) {
if (!empty($element['#options'])) {
$element['#options'] = WebformOptions::getElementOptions($element);
}
list($type, $options_custom) = explode(':', $this
->getPluginId());
$element['#type'] = $type;
$element['#options_custom'] = $options_custom;
$class = $this
->getFormElementClassDefinition();
$class::setTemplateOptions($element);
}
protected function getEntity() {
list(, $webform_options_custom_id) = explode(':', $this
->getPluginId());
return $this->entityTypeManager
->getStorage('webform_options_custom')
->load($webform_options_custom_id);
}
}