You are here

AutoPath.php in Pardot Integration 2.x

File

src/Plugin/PardotFormMapFormatterPlugin/AutoPath.php
View source
<?php

namespace Drupal\pardot\Plugin\PardotFormMapFormatterPlugin;

use Drupal\Core\Form\FormStateInterface;
use Drupal\pardot\Plugin\PardotFormMapFormatterPluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin to have an autocomplete textfield for typed data.
 *
 * @PardotFormMapFormatterPlugin(
 *  id = "autopath",
 *  label = @Translation("Auto Path"),
 *  types = {
 *     "contact_form",
 *   }
 * )
 */
class AutoPath extends PardotFormMapFormatterPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The data fetcher object.
   *
   * @var \Drupal\typed_data\DataFetcherInterface
   */
  protected $dataFetcher;

  /**
   * Entity field manager to get the contact form field definitions.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance->dataFetcher = $container
      ->get('typed_data.data_fetcher');
    $instance->entityTypeManager = $container
      ->get('entity_type.manager');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginId() {
    return 'autopath';
  }

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

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'path' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $entity_id = explode(':', $this->configuration['entity_id'])[1] ?? '';
    $form['path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('autocomplete'),
      '#autocomplete_route_name' => 'pardot.autocomplete',
      '#description' => $this
        ->t('Enter the form handler post url from Pardot.'),
      '#default_value' => $this->configuration['path'],
      '#autocomplete_route_parameters' => [
        'entity_id' => $entity_id,
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['path'] = $form_state
      ->getValue('path');
  }

  /**
   * Get the form field from the form state and apply formatting.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state that holds the input values.
   *
   * @return mixed
   *   The formatted value or null i guess.
   */
  public function getFormattedValue(FormStateInterface $form_state) {

    /** @var \Drupal\contact\Entity\Message $form_entity */
    $form_entity = $form_state
      ->getFormObject()
      ->getEntity();
    $path = $this
      ->getConfiguration()['path'] ?? '';
    if ($path) {
      $path = explode('.', $path);
      array_shift($path);
      $path = implode('.', $path);
      $value = $this->dataFetcher
        ->fetchDataByPropertyPath($form_entity
        ->getTypedData(), $path)
        ->getValue();
      return $value;
    }
    return NULL;
  }

}

Classes

Namesort descending Description
AutoPath Plugin to have an autocomplete textfield for typed data.