PardotFormMappingForm.php in Pardot Integration 2.x
File
src/Form/PardotFormMappingForm.php
View source
<?php
namespace Drupal\pardot\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\pardot\PluginSubFormHelpers;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PardotFormMappingForm extends FormBase {
protected $pluginManagerFormMapHandlerPlugin;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->pluginManagerFormMapHandlerPlugin = $container
->get('plugin.manager.form_map_handler_plugin');
return $instance;
}
public function getFormId() {
return 'pardot_form_mapping_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$formMapPluginManager = $this->pluginManagerFormMapHandlerPlugin;
$entity = func_get_args()[2];
$form_map_plugin_instance_id = func_get_args()[3] ?? '';
if ($entity && $form_map_plugin_instance_id) {
$form_state
->set('entity', $entity);
$form_state
->set('form_map_plugin_instance_id', $form_map_plugin_instance_id);
$plugin = $formMapPluginManager
->createInstance($form_map_plugin_instance_id);
$settings = [
$entity
->getEntityTypeId() => $entity
->id(),
'entity_id' => $entity
->id(),
];
if ($plugin_settings = $entity
->getThirdPartySetting('pardot', 'pardot_form_mapping_plugin', FALSE)) {
$settings['settings'] = $plugin_settings['settings'];
}
$plugin
->setConfiguration($settings);
$form['pardot_form_mapping'] = PluginSubFormHelpers::buildPluginForm($plugin, $form, $form_state, [
'pardot_form_mapping',
]);
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$pardot_form_mapping = $values['pardot_form_mapping'] ?? FALSE;
$entity = $form_state
->get('entity');
$form_map_plugin_instance_id = $form_state
->get('form_map_plugin_instance_id');
if ($pardot_form_mapping && $form_map_plugin_instance_id && $entity) {
$plugin = $this->pluginManagerFormMapHandlerPlugin;
$plugin = $plugin
->createInstance($form_map_plugin_instance_id);
$plugin = PluginSubFormHelpers::pluginSubmit($plugin, $form, $form_state, [
'pardot_form_mapping',
]);
$entity
->setThirdPartySetting('pardot', 'pardot_form_mapping_plugin', $plugin
->getConfiguration());
$entity
->save();
}
}
}