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;
class AutoPath extends PardotFormMapFormatterPluginBase implements ContainerFactoryPluginInterface {
protected $dataFetcher;
protected $entityTypeManager;
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;
}
public function getPluginId() {
return 'autopath';
}
public function getPluginDefinition() {
return $this->pluginDefinition;
}
public function defaultConfiguration() {
return [
'path' => '',
];
}
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;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['path'] = $form_state
->getValue('path');
}
public function getFormattedValue(FormStateInterface $form_state) {
$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
Name |
Description |
AutoPath |
Plugin to have an autocomplete textfield for typed data. |