You are here

FormMap.php in Pardot Integration 2.x

Namespace

Drupal\pardot

File

src/FormMap.php
View source
<?php

namespace Drupal\pardot;


/**
 * Provides an interface for defining Pardot Form Map entities.
 */
class FormMap implements FormMapInterface {

  /**
   * The post url provided by the pardot form handler.
   *
   * @var string
   */
  protected $post_url;

  /**
   * The array of mapped contact form fields to pardot form handler fields.
   *
   * @var array
   */
  public $settings = [];

  /**
   * The array of mapped contact form fields to pardot form handler fields.
   *
   * @var array
   */
  public $mapping = [];

  /**
   * {@inheritdoc}
   */
  public function __construct($settings) {
    $mapping = [];
    $this->settings = $settings;
    $form_map_plugin_manager = \Drupal::service('plugin.manager.pardot_form_map_formatter_plugin');
    if (isset($this->settings['form_map'])) {
      foreach ($this->settings['form_map'] as $key => $setting) {
        if ($setting['plugin_type']) {
          $plugin = $form_map_plugin_manager
            ->createInstance($setting['plugin']['id']);
          $plugin
            ->setConfiguration($setting['plugin']);
        }
        else {
          $plugin = [];
        }
        $class = $setting['class'];
        $mapping[$key] = new $class($setting['pardot_key'], $setting['plugin_type'], $plugin);
      }
    }
    $post_url = $this->settings['post_url'] ?? '';
    $this
      ->setPostUrl($post_url);
    $this
      ->setMappedFieldCollection($mapping);
  }

  /**
   * {@inheritdoc}
   */
  public function getPostUrl() {
    return $this->post_url;
  }

  /**
   * {@inheritdoc}
   */
  public function setPostUrl(string $post_url) {
    $this->post_url = $post_url;
  }

  /**
   * {@inheritdoc}
   */
  public function getMappedFieldCollection() {
    return $this->mapping;
  }

  /**
   * {@inheritdoc}
   */
  public function setMappedFieldCollection(array $mapping) {
    $this->mapping = $mapping;
  }

  /**
   * Add a new MappedField instance to the collection array.
   *
   * @param string $class
   *   The specific class name.
   * @param string $pardot_key
   *   The name of the pardot field.
   * @param string $plugin_type
   *   The machine name of the selected plugin.
   * @param mixed $plugin
   *   The plugin instance or empty array.
   * @param array $config
   *   Pass some optional init config to the plugin.
   */
  public function appendMappedField(string $class = '\\Drupal\\pardot\\MappedField', string $pardot_key = '', string $plugin_type = '', $plugin = [], array $config = []) {
    $this->mapping[] = new $class($pardot_key, $plugin_type, $plugin, $config);
  }

  /**
   * Retrieve a particular row from the MappedField collection.
   *
   * @param int $key
   *   The index where the MappedField lives.
   *
   * @return mixed
   *   Return the instance of MappedFieldInterface or null.
   */
  public function getMappedField(int $key) {
    if (isset($this->mapping[$key])) {
      return $this->mapping[$key];
    }
    return NULL;
  }

  /**
   * Remove a particular row from the MappedField collection.
   *
   * @param int $key
   *   The index where the instance of MappedFieldInterface lives.
   */
  public function removeMappedField(int $key) {
    if (isset($this->mapping[$key])) {
      unset($this->mapping[$key]);
    }
  }
  public function toArray() {
    $mapping = $this->mapping;
    $mapping = array_map(function (MappedFieldInterface $mapping) {
      return $mapping
        ->toArray();
    }, $mapping);
    $this->settings['post_url'] = $this
      ->getPostUrl();
    $this->settings['form_map'] = $mapping;
    return $this->settings;
  }

}

Classes

Namesort descending Description
FormMap Provides an interface for defining Pardot Form Map entities.