class WebformContentCreatorManageFieldsForm in Webform Content Creator 2.x
Same name and namespace in other branches
- 8 src/Form/WebformContentCreatorManageFieldsForm.php \Drupal\webform_content_creator\Form\WebformContentCreatorManageFieldsForm
- 3.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, RedirectDestinationTrait, 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 BUNDLE_FIELD = 'bundle_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['intro_text'] = [
'#markup' => '<p>' . $this
->t('You can create content based on webform submission values. In this page, you can do mappings between bundle fields and webform submission values. You may also use tokens in custom text. You must map the required fields, otherwise content will not be created.') . '</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 bundle and webform.
$this
->constructTable($form);
return $form;
}
/**
* Constructs table with mapping between webform and bundle.
*
* @param array $form
* Form entity array.
*/
public function constructTable(array &$form) {
$fieldTypesDefinitions = $this->pluginFieldType
->getDefinitions();
$attributes = $this->entity
->getAttributes();
$entity_type_initial_id = $this->entity
->getEntityTypeValue();
$entity_type_id = $this->entity
->getEntityTypeValue();
$bundle_id = $this->entity
->getBundleValue();
if ($entity_type_id === 'node') {
$entity_type_id = 'node_type';
}
$bundleFilteredFieldIds = WebformContentCreatorUtilities::getBundleIds($entity_type_initial_id, $bundle_id);
asort($bundleFilteredFieldIds);
$bundleFields = WebformContentCreatorUtilities::bundleFields($entity_type_initial_id, $bundle_id);
$webform_id = $this->entity
->getWebform();
$webformOptions = WebformContentCreatorUtilities::getWebformElements($webform_id);
// Table header.
$header = [
self::BUNDLE_FIELD => $this
->t('Bundle 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 ($bundleFilteredFieldIds as $fieldId) {
// Checkboxes with bundle fields.
$form[self::FORM_TABLE][$fieldId][self::BUNDLE_FIELD] = [
'#type' => 'checkbox',
'#default_value' => array_key_exists($fieldId, $attributes),
'#title' => $bundleFields[$fieldId]
->getLabel() . ' (' . $fieldId . ')',
];
// Link to edit field settings.
$form[self::FORM_TABLE][$fieldId][self::FIELD_TYPE] = [
'#type' => 'markup',
'#markup' => $fieldTypesDefinitions[$bundleFields[$fieldId]
->getType()]['label'],
];
// 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::BUNDLE_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::BUNDLE_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);
$entity_type_initial_id = $this->entity
->getEntityTypeValue();
$entity_type_id = $this->entity
->getEntityTypeValue();
$bundle_id = $this->entity
->getBundleValue();
$bundleFields = WebformContentCreatorUtilities::bundleFields($entity_type_initial_id, $bundle_id);
$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 bundle field is selected.
if (!$v[self::BUNDLE_FIELD]) {
continue;
}
$args = explode(',', $v[self::WEBFORM_FIELD]);
if (empty($args) || count($args) < 2) {
continue;
}
$bundleFieldType = $bundleFields[$k]
->getType();
$webformOptionType = array_key_exists($args[1], $webformElementTypes) ? $webformElementTypes[$args[1]] : '';
if ($bundleFieldType === $webformOptionType) {
continue;
}
if ($bundleFieldType === 'email') {
$form_state
->setErrorByName(self::FORM_TABLE . '][' . $k . '][' . self::WEBFORM_FIELD, $this
->t('Incompatible type'));
}
if ($webformOptionType === 'email' && strpos($bundleFieldType, 'text') === FALSE && strpos($bundleFieldType, '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 bundle field is selected.
if (!$v[self::BUNDLE_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('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 | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 11 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 35 |
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:: |
3 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
13 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties. | 9 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
6 |
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:: |
3 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
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:: |
47 |
EntityForm:: |
public | function |
Sets the form entity. 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:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
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. | 3 |
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. | |
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. | |
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. | |
WebformContentCreatorManageFieldsForm:: |
protected | property |
Entity type manager. Overrides EntityForm:: |
|
WebformContentCreatorManageFieldsForm:: |
protected | property | Plugin field type. | |
WebformContentCreatorManageFieldsForm:: |
constant | |||
WebformContentCreatorManageFieldsForm:: |
public | function | Constructs table with mapping between webform and bundle. | |
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 |