class WebformOptionsCustomForm in Webform 8.5
Same name and namespace in other branches
- 6.x modules/webform_options_custom/src/WebformOptionsCustomForm.php \Drupal\webform_options_custom\WebformOptionsCustomForm
Provides a form to set webform options custom.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\webform_options_custom\WebformOptionsCustomForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of WebformOptionsCustomForm
File
- modules/
webform_options_custom/ src/ WebformOptionsCustomForm.php, line 17
Namespace
Drupal\webform_options_customView source
class WebformOptionsCustomForm extends EntityForm {
/**
* The HTTP client to fetch the feed data with.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Constructs a WebformOptionsCustomForm object.
*
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP client.
*/
public function __construct(ClientInterface $http_client) {
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('http_client'));
}
/**
* {@inheritdoc}
*/
protected function prepareEntity() {
if ($this->operation === 'duplicate') {
$this
->setEntity($this
->getEntity()
->createDuplicate());
}
parent::prepareEntity();
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
$webform_options_custom = $this
->getEntity();
// Customize title for duplicate and edit operation.
switch ($this->operation) {
case 'duplicate':
$form['#title'] = $this
->t("Duplicate '@label' custom options", [
'@label' => $webform_options_custom
->label(),
]);
break;
case 'edit':
case 'source':
case 'preview':
$form['#title'] = $webform_options_custom
->label();
break;
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
$webform_options_custom = $this->entity;
switch ($this->operation) {
case 'preview':
// Build and return a preview of the custom options element.
$form['preview'] = $webform_options_custom
->getPreview();
return $form;
case 'source':
// Build and return a options YAML source element.
$form['options'] = [
'#type' => 'details',
'#title' => $this
->t('Options'),
'#open' => TRUE,
];
$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 parsed from the HTML/SVG markup.'),
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
];
$form['options']['options'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Options (YAML)'),
'#title_displace' => 'invisible',
'#attributes' => [
'style' => 'min-height: 200px',
],
'#default_value' => $this
->getOptions(),
];
return parent::form($form, $form_state);
}
/** @var \Drupal\webform_options_custom\WebformOptionsCustomStorageInterface $webform_options_custom_storage */
$webform_options_custom_storage = $this->entityTypeManager
->getStorage('webform_options_custom');
// General.
$form['general'] = [
'#type' => 'details',
'#title' => $this
->t('General'),
'#open' => TRUE,
];
$form['general']['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#required' => TRUE,
'#attributes' => $webform_options_custom
->isNew() ? [
'autofocus' => 'autofocus',
] : [],
'#default_value' => $webform_options_custom
->label(),
];
$form['general']['id'] = [
'#type' => 'machine_name',
'#machine_name' => [
'exists' => '\\Drupal\\webform_options_custom\\Entity\\WebformOptionsCustom::load',
'label' => '<br/>' . $this
->t('Machine name'),
'source' => [
'general',
'label',
],
],
'#maxlength' => 32,
'#field_suffix' => $webform_options_custom
->isNew() ? ' (' . $this
->t('Maximum @max characters', [
'@max' => 32,
]) . ')' : '',
'#required' => TRUE,
'#disabled' => !$webform_options_custom
->isNew(),
'#default_value' => $webform_options_custom
->id(),
];
$form['general']['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#description' => $this
->t('A brief description present to the user when adding this element to a webform.'),
'#required' => TRUE,
'#rows' => 2,
'#default_value' => $webform_options_custom
->get('description'),
];
$form['general']['help'] = [
'#type' => 'webform_html_editor',
'#title' => $this
->t('Help text'),
'#description' => $this
->t('Instructions to present to the user below this element on the editing form.'),
'#default_value' => $webform_options_custom
->get('help'),
];
$form['general']['category'] = [
'#type' => 'webform_select_other',
'#title' => $this
->t('Category'),
'#options' => $webform_options_custom_storage
->getCategories(),
'#empty_option' => $this
->t('- None -'),
'#default_value' => $webform_options_custom
->get('category'),
];
// Template.
$form['template'] = [
'#type' => 'details',
'#title' => $this
->t('Template'),
'#open' => TRUE,
];
$form['template']['type'] = [
'#type' => 'radios',
'#title' => $this
->t('Type'),
'#options' => [
WebformOptionsCustomInterface::TYPE_URL => $this
->t('URL'),
WebformOptionsCustomInterface::TYPE_TEMPLATE => $this
->t('Template'),
],
'#options_display' => 'side_by_side',
'#default_value' => $webform_options_custom
->get('type'),
'#required' => TRUE,
];
$form['template']['url'] = [
'#type' => 'textfield',
'#title' => $this
->t('HTML/SVG file URL or path'),
'#description' => $this
->t('Enter the absolute URL or root-relative path to the HTML/SVG file. The HTML/SVG file must be publicly accessible using http:// or https://.'),
'#default_value' => $webform_options_custom
->get('url'),
'#states' => [
'visible' => [
':input[name="type"]' => [
'value' => WebformOptionsCustomInterface::TYPE_URL,
],
],
'required' => [
':input[name="type"]' => [
'value' => WebformOptionsCustomInterface::TYPE_URL,
],
],
],
];
if (function_exists('imce_process_url_element')) {
imce_process_url_element($form['template']['url'], 'link');
$form['#attached']['library'][] = 'webform/imce.input';
}
$form['template']['template'] = [
'#type' => 'webform_codemirror',
'#mode' => 'twig',
'#title' => $this
->t('HTML/SVG markup template (Twig)'),
'#description' => $this
->t('The entire element with descriptions without the hash (#) prefixes is passed as variables to the Twig template.'),
'#default_value' => $webform_options_custom
->get('template'),
'#states' => [
'visible' => [
':input[name="type"]' => [
'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
],
],
'required' => [
':input[name="type"]' => [
'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
],
],
],
];
$form['template']['twig_help'] = [
'#type' => 'details',
'#title' => $this
->t('Help using Twig'),
'description' => $this
->buildTwigHelp(),
'#states' => [
'visible' => [
':input[name="type"]' => [
'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
],
],
'required' => [
':input[name="type"]' => [
'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
],
],
],
];
$form['template']['value_attributes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Option value attribute names'),
'#description' => $this
->t('Enter a comma-delimited list of attribute names. The first matched value attribute will be used for all custom option values .Leave blank if options values is populated using Twig.'),
'#pattern' => '^([-_,]|[a-z])+$',
'#default_value' => $webform_options_custom
->get('value_attributes'),
];
$form['template']['text_attributes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Option text attribute names'),
'#description' => $this
->t('Enter a comma-delimited list of attribute names. The first matched text attribute will be used for all custom option text. Leave blank if options text is populated using Twig.'),
'#pattern' => '^([-_,]|[a-z])+$',
'#default_value' => $webform_options_custom
->get('text_attributes'),
];
$form['template']['fill'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow SVG option elements to be filled using CSS'),
'#description' => $this
->t('If checked, inline fill styles will be removed and replaced using CSS.'),
'#return_value' => TRUE,
'#default_value' => $webform_options_custom
->get('fill'),
];
$form['template']['zoom'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable SVG panning and zooming'),
'#description' => $this
->t('If checked, SVG graphic can be panned and zooming using the <a href=":href">svg-pan-zoom</a> library', [
':href' => 'https://github.com/ariutta/svg-pan-zoom',
]),
'#return_value' => TRUE,
'#default_value' => $webform_options_custom
->get('zoom'),
];
$form['assets'] = [
'#type' => 'details',
'#title' => $this
->t('CSS/JS'),
'#open' => TRUE,
];
$form['assets']['css'] = [
'#type' => 'webform_codemirror',
'#mode' => 'css',
'#title' => $this
->t('Custom CSS'),
'#default_value' => $webform_options_custom
->get('css'),
];
$form['assets']['javascript'] = [
'#type' => 'webform_codemirror',
'#mode' => 'javascript',
'#title' => $this
->t('Custom JavaScript'),
'#default_value' => $webform_options_custom
->get('javascript'),
];
// Options.
$form['options'] = [
'#type' => 'details',
'#title' => $this
->t('Options'),
'#open' => TRUE,
];
$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 parsed from the HTML/SVG markup.'),
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
];
$form['options']['options'] = [
'#type' => 'webform_options',
'#title' => $this
->t('Options'),
'#descriptions' => $this
->t('Option descriptions are displayed by tooltips.'),
'#title_displace' => 'invisible',
'#options_description' => TRUE,
'#empty_options' => 10,
'#add_more_items' => 10,
'#default_value' => $this
->getOptions(),
];
$form['options']['show_select'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show select menu associated with the custom options element'),
'#description' => $this
->t('If checked, the select menu associated with the custom options element will be visible. Displaying the standard HTML select menu assists mobile users and users with disabilities when thy are selecting custom options.'),
'#return_value' => TRUE,
'#default_value' => $webform_options_custom
->get('show_select'),
];
$form['options']['tooltip'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display option text and description in a tooltip'),
'#description' => $this
->t('If checked, option text and description will be displayed using a tooltip.'),
'#return_value' => TRUE,
'#default_value' => $webform_options_custom
->get('tooltip'),
];
// Integration.
$form['integration'] = [
'#type' => 'details',
'#title' => $this
->t('Integration'),
'#open' => TRUE,
];
$form['integration']['element'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use as a basic select element'),
'#description' => $this
->t('If checked, this custom options element will be available when a user is adding elements to a webform.'),
'#return_value' => TRUE,
'#default_value' => $webform_options_custom
->get('element'),
];
$form['integration']['entity_reference'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use as an entity reference element'),
'#description' => $this
->t('If checked, this custom options element will be available for entity references when a user is adding elements to a webform.'),
'#return_value' => TRUE,
'#default_value' => $webform_options_custom
->get('entity_reference'),
];
return parent::form($form, $form_state);
}
/**
* Build Twig help.
*
* @return array
* A renderable array container Twig help.
*/
protected function buildTwigHelp() {
// Build custom options specific Twig help.
// @see \Drupal\webform\Twig\WebformTwigExtension::buildTwigHelp
$t_args = [
':twig_href' => 'https://twig.sensiolabs.org/',
':drupal_href' => 'https://www.drupal.org/docs/8/theming/twig',
];
$build = [];
$build[] = [
'#markup' => '<p>' . $this
->t('Learn about <a href=":twig_href">Twig</a> and how it is used in <a href=":drupal_href">Drupal</a>.', $t_args) . '</p>',
];
$build[] = [
'#markup' => '<p>' . $this
->t("The following variables are available:") . '</p>',
];
$build[] = [
'#theme' => 'item_list',
'#items' => [
'{{ type }}',
'{{ title }}',
'{{ description }}',
'{{ help }}',
'{{ options }}',
'{{ options_custom }}',
'{{ descriptions }}',
'{{ default_value }}',
'{{ multiple }}',
'{{ attributes }}',
'{{ empty_option }}',
'{{ empty_value }}',
],
];
$build[] = [
'#markup' => '<p>' . $this
->t("You can debug data using the <code>webform_debug()</code> function.") . '</p>',
];
$build[] = [
'#markup' => "<pre>{{ webform_debug(data) }}</pre>",
];
return $build;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
if ($this->operation === 'preview') {
return [];
}
$actions = parent::actions($form, $form_state);
// Remove delete button from source edit form.
if ($this->operation === 'source') {
unset($actions['delete']);
}
// Open delete button in a modal dialog.
if (isset($actions['delete'])) {
$actions['delete']['#attributes'] = WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, $actions['delete']['#attributes']['class']);
WebformDialogHelper::attachLibraries($actions['delete']);
}
return $actions;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
$webform_options_custom = $this
->getEntity();
$this
->copyFormValuesToEntity($webform_options_custom, $form, $form_state);
// Make sure the URL exists.
$url = $webform_options_custom
->getUrl();
if ($url) {
$file_exists = FALSE;
try {
$response = $this->httpClient
->get($url);
$file_exists = $response
->getStatusCode() === 200;
} catch (\RangeException $exception) {
// Do nothing.
}
if (!$file_exists) {
$t_args = [
'%url' => $form_state
->getValue('url'),
];
$form_state
->setErrorByName('url', $this
->t('HTML/SVG file URL or path %url not found.', $t_args));
}
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
$webform_options_custom = $this
->getEntity();
$webform_options_custom
->save();
$context = [
'@label' => $webform_options_custom
->label(),
'link' => $webform_options_custom
->toLink($this
->t('Edit'), 'edit-form')
->toString(),
];
$this
->logger('webform_options_custom')
->notice('Custom options @label saved.', $context);
$this
->messenger()
->addStatus($this
->t('Custom options %label saved.', [
'%label' => $webform_options_custom
->label(),
]));
$form_state
->setRedirect('entity.webform_options_custom.collection');
}
/**
* {@inheritdoc}
*/
public function afterBuild(array $element, FormStateInterface $form_state) {
// Overriding after \Drupal\Core\Entity\EntityForm::afterBuild because
// it calls ::buildEntity(), which calls ::copyFormValuesToEntity, which
// attempts to populate the entity even though the 'options' have not been
// validated and set.
// @see \Drupal\Core\Entity\EntityForm::afterBuild
// @eee \Drupal\webform_options_custom\WebformOptionsCustomForm::copyFormValuesToEntity
// @see \Drupal\webform\Element\WebformOptions
return $element;
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $entity */
$values = $form_state
->getValues();
if (is_array($values['options'])) {
$entity
->setOptions($values['options']);
unset($values['options']);
}
foreach ($values as $key => $value) {
$entity
->set($key, $value);
}
}
/**
* Get options.
*
* @return array
* An associative array of options.
*/
protected function getOptions() {
/** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
$webform_options_custom = $this
->getEntity();
$options = $webform_options_custom
->getOptions();
return WebformOptionsHelper::convertOptionsToString($options);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
WebformOptionsCustomForm:: |
protected | property | The HTTP client to fetch the feed data with. | |
WebformOptionsCustomForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
WebformOptionsCustomForm:: |
public | function |
Form element #after_build callback: Updates the entity with submitted data. Overrides EntityForm:: |
|
WebformOptionsCustomForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
WebformOptionsCustomForm:: |
protected | function | Build Twig help. | |
WebformOptionsCustomForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
WebformOptionsCustomForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
WebformOptionsCustomForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
WebformOptionsCustomForm:: |
protected | function | Get options. | |
WebformOptionsCustomForm:: |
protected | function |
Prepares the entity object before the form is built first. Overrides EntityForm:: |
|
WebformOptionsCustomForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
WebformOptionsCustomForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
WebformOptionsCustomForm:: |
public | function | Constructs a WebformOptionsCustomForm object. |