You are here

PardotFormMappingForm.php in Pardot Integration 2.x

Namespace

Drupal\pardot\Form

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.
 */
class PardotFormMappingForm extends FormBase {

  /**
   * Drupal\pardot\Plugin\FormMapHandlerPluginManager definition.
   *
   * @var \Drupal\pardot\Plugin\FormMapHandlerPluginManager
   */
  protected $pluginManagerFormMapHandlerPlugin;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->pluginManagerFormMapHandlerPlugin = $container
      ->get('plugin.manager.form_map_handler_plugin');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'pardot_form_mapping_form';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Display result.
    $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();
    }
  }

}

Classes

Namesort descending Description
PardotFormMappingForm Class PardotFormMappingForm.