class WebformContentCreatorManageFieldsForm in Webform Content Creator 8
Same name and namespace in other branches
- 3.x src/Form/WebformContentCreatorManageFieldsForm.php \Drupal\webform_content_creator\Form\WebformContentCreatorManageFieldsForm
- 2.x src/Form/WebformContentCreatorManageFieldsForm.php \Drupal\webform_content_creator\Form\WebformContentCreatorManageFieldsForm
Form handler for the Webform content creator manage fields form.
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_content_creator\Form\WebformContentCreatorManageFieldsForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of WebformContentCreatorManageFieldsForm
File
- src/
Form/ WebformContentCreatorManageFieldsForm.php, line 14
Namespace
Drupal\webform_content_creator\FormView source
class WebformContentCreatorManageFieldsForm extends EntityForm {
const CONTENT_TYPE_FIELD = 'content_type_field';
const FIELD_TYPE = 'field_type';
const WEBFORM_FIELD = 'webform_field';
const CUSTOM_CHECK = 'custom_check';
const CUSTOM_VALUE = 'custom_value';
const FORM_TABLE = 'table';
/**
* Plugin field type.
*
* @var object
*/
protected $pluginFieldType;
/**
* Entity type manager.
*
* @var object
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = new static();
$instance->pluginFieldType = $container
->get('plugin.manager.field.field_type');
$instance->entityTypeManager = $container
->get('entity_type.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#maxlength' => 255,
'#default_value' => $this->entity
->get('field_title'),
'#help' => $this
->t('Title of content created after webform submission. You may use tokens.'),
'#description' => $this
->t("Default value: webform title."),
'#weight' => 0,
];
$form['intro_text'] = [
'#markup' => '<p>' . $this
->t('You can create nodes based on webform submission values. In this page, you can do mappings between content type fields and webform submission values. You may also use tokens in custom text.') . '</p>',
];
$form['tokens'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
'webform_submission',
],
'#global_types' => TRUE,
'#click_insert' => TRUE,
'#show_restricted' => FALSE,
'#recursion_limit' => 3,
'#text' => $this
->t('Browse available tokens'),
];
// Construct table with mapping between content type and webform.
$this
->constructTable($form);
return $form;
}
/**
* Constructs table with mapping between webform and content type.
*
* @param array $form
* Form entity array.
*/
public function constructTable(array &$form) {
$fieldTypesDefinitions = $this->pluginFieldType
->getDefinitions();
$attributes = $this->entity
->getAttributes();
$ct = $this->entity
->getContentType();
$contentType = $this->entityTypeManager
->getStorage('node_type')
->load($ct);
$nodeFilteredFieldIds = WebformContentCreatorUtilities::getContentFieldsIds($contentType);
asort($nodeFilteredFieldIds);
$nodeFields = WebformContentCreatorUtilities::contentTypeFields($contentType);
$webform_id = $this->entity
->getWebform();
$webformOptions = WebformContentCreatorUtilities::getWebformElements($webform_id);
// Table header.
$header = [
self::CONTENT_TYPE_FIELD => $this
->t('Content type field'),
self::FIELD_TYPE => $this
->t('Field type'),
self::CUSTOM_CHECK => $this
->t('Custom'),
self::WEBFORM_FIELD => $this
->t('Webform field'),
self::CUSTOM_VALUE => $this
->t('Custom text'),
];
$form[self::FORM_TABLE] = [
'#type' => 'table',
'#header' => $header,
];
foreach ($nodeFilteredFieldIds as $fieldId) {
$route_parameters = [
'node_type' => $ct,
'field_config' => 'node.' . $ct . '.' . $fieldId,
];
// Checkboxes with content type fields.
$form[self::FORM_TABLE][$fieldId][self::CONTENT_TYPE_FIELD] = [
'#type' => 'checkbox',
'#default_value' => array_key_exists($fieldId, $attributes),
'#title' => $nodeFields[$fieldId]
->getLabel() . ' (' . $fieldId . ')',
];
// Link to edit field settings.
$form[self::FORM_TABLE][$fieldId][self::FIELD_TYPE] = [
'#type' => 'link',
'#title' => $fieldTypesDefinitions[$nodeFields[$fieldId]
->getType()]['label'],
'#url' => Url::fromRoute("entity.field_config.node_storage_edit_form", $route_parameters),
'#options' => [
'attributes' => [
'title' => $this
->t('Edit field settings.'),
],
],
];
// Checkbox to select between webform element/property or custom text.
$form[self::FORM_TABLE][$fieldId][self::CUSTOM_CHECK] = [
'#type' => 'checkbox',
'#default_value' => array_key_exists($fieldId, $attributes) ? $attributes[$fieldId][self::CUSTOM_CHECK] : '',
'#states' => [
'disabled' => [
':input[name="' . self::FORM_TABLE . '[' . $fieldId . '][' . self::CONTENT_TYPE_FIELD . ']"]' => [
'checked' => FALSE,
],
],
],
];
$type = !empty($attributes[$fieldId]) && $attributes[$fieldId]['type'] ? '1' : '0';
// Select with webform elements and basic properties.
$form[self::FORM_TABLE][$fieldId][self::WEBFORM_FIELD] = [
'#type' => 'select',
'#options' => $webformOptions,
'#states' => [
'required' => [
':input[name="' . self::FORM_TABLE . '[' . $fieldId . '][' . self::CONTENT_TYPE_FIELD . ']"]' => [
'checked' => TRUE,
],
':input[name="' . self::FORM_TABLE . '[' . $fieldId . '][' . self::CUSTOM_CHECK . ']"]' => [
'checked' => FALSE,
],
],
],
];
if (array_key_exists($fieldId, $attributes) && !$attributes[$fieldId][self::CUSTOM_CHECK]) {
$form[self::FORM_TABLE][$fieldId][self::WEBFORM_FIELD]['#default_value'] = $type . ',' . $attributes[$fieldId][self::WEBFORM_FIELD];
}
// Textarea with custom text (including tokens)
$form[self::FORM_TABLE][$fieldId][self::CUSTOM_VALUE] = [
'#type' => 'textarea',
'#default_value' => array_key_exists($fieldId, $attributes) ? $attributes[$fieldId][self::CUSTOM_VALUE] : '',
];
}
// Change table position in page.
$form[self::FORM_TABLE]['#weight'] = 1;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$ct = $this->entity
->getContentType();
$contentType = $this->entityTypeManager
->getStorage('node_type')
->load($ct);
$nodeFields = WebformContentCreatorUtilities::contentTypeFields($contentType);
$webform_id = $this->entity
->getWebform();
$webformElementTypes = WebformContentCreatorUtilities::getWebformElementsTypes($webform_id);
// For each table row.
foreach ($form_state
->getValue(self::FORM_TABLE) as $k => $v) {
// Check if a content type field is selected.
if (!$v[self::CONTENT_TYPE_FIELD]) {
continue;
}
$args = explode(',', $v[self::WEBFORM_FIELD]);
if (empty($args) || count($args) < 2) {
continue;
}
$nodeFieldType = $nodeFields[$k]
->getType();
$webformOptionType = array_key_exists($args[1], $webformElementTypes) ? $webformElementTypes[$args[1]] : '';
if ($nodeFieldType === $webformOptionType) {
continue;
}
if ($nodeFieldType === 'email') {
$form_state
->setErrorByName(self::FORM_TABLE . '][' . $k . '][' . self::WEBFORM_FIELD, $this
->t('Incompatible type'));
}
if ($webformOptionType === 'email' && strpos($nodeFieldType, 'text') === FALSE && strpos($nodeFieldType, 'string') === FALSE) {
$form_state
->setErrorByName(self::FORM_TABLE . '][' . $k . '][' . self::WEBFORM_FIELD, $this
->t('Incompatible type'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$attributes = [];
// For each table row.
foreach ($form_state
->getValue(self::FORM_TABLE) as $k => $v) {
// Check if a content type field is selected.
if (!$v[self::CONTENT_TYPE_FIELD]) {
continue;
}
$args = explode(',', $v[self::WEBFORM_FIELD]);
if (empty($args) || count($args) < 1) {
continue;
}
$attributes[$k] = [
'type' => explode(',', $v[self::WEBFORM_FIELD])[0] === '1',
self::WEBFORM_FIELD => !$v[self::CUSTOM_CHECK] ? explode(',', $v[self::WEBFORM_FIELD])[1] : '',
self::CUSTOM_CHECK => $v[self::CUSTOM_CHECK],
self::CUSTOM_VALUE => $v[self::CUSTOM_CHECK] ? $v[self::CUSTOM_VALUE] : '',
];
}
$this->entity
->set('field_title', $form_state
->getValue('title'));
$this->entity
->set('elements', $attributes);
$status = $this->entity
->save();
$this->entity
->statusMessage($status);
$form_state
->setRedirect('entity.webform_content_creator.collection');
}
/**
* Helper function to check whether a Webform content creator entity exists.
*
* @param mixed $id
* Entity id.
*
* @return bool
* True if entity already exists.
*/
public function exist($id) {
return WebformContentCreatorUtilities::existsWebformContentCreatorEntity($id);
}
}
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 module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
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 | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
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 | ||
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. | |
WebformContentCreatorManageFieldsForm:: |
protected | property |
Entity type manager. Overrides EntityForm:: |
|
WebformContentCreatorManageFieldsForm:: |
protected | property | Plugin field type. | |
WebformContentCreatorManageFieldsForm:: |
public | function | Constructs table with mapping between webform and content type. | |
WebformContentCreatorManageFieldsForm:: |
constant | |||
WebformContentCreatorManageFieldsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
WebformContentCreatorManageFieldsForm:: |
constant | |||
WebformContentCreatorManageFieldsForm:: |
constant | |||
WebformContentCreatorManageFieldsForm:: |
public | function | Helper function to check whether a Webform content creator entity exists. | |
WebformContentCreatorManageFieldsForm:: |
constant | |||
WebformContentCreatorManageFieldsForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
WebformContentCreatorManageFieldsForm:: |
constant | |||
WebformContentCreatorManageFieldsForm:: |
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 EntityForm:: |
|
WebformContentCreatorManageFieldsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
WebformContentCreatorManageFieldsForm:: |
constant |