Token.php in Pardot Integration 2.x
File
src/Plugin/PardotFormMapFormatterPlugin/Token.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 Token extends PardotFormMapFormatterPluginBase implements ContainerFactoryPluginInterface {
protected $token;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->token = $container
->get('token');
return $instance;
}
public function getPluginId() {
return 'token';
}
public function getPluginDefinition() {
return $this->pluginDefinition;
}
public function defaultConfiguration() {
return [
'token' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$token_types = $this->configuration['entity_types'];
$form['token'] = [
'#type' => 'textfield',
'#title' => 'Token',
'#default_value' => $this->configuration['token'],
'#size' => 65,
'#maxlength' => 1280,
'#element_validate' => [
'token_element_validate',
],
'#after_build' => [
'token_element_validate',
],
'#token_types' => $token_types,
'#min_tokens' => 1,
'#required' => TRUE,
];
$form['token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['token'] = $form_state
->getValue('token');
}
public function getFormattedValue(FormStateInterface $form_state) {
$token = $this
->getConfiguration()['token'] ?? '';
$entity_types = $this
->getConfiguration()['entity_types'] ?? [];
$form_entity = $form_state
->getFormObject()
->getEntity();
$replaced = $this->token
->replace($token, [
$form_entity
->getEntityTypeId() => $form_entity,
]);
if (empty($this->token
->scan($replaced))) {
return $replaced;
}
return null;
}
}
Classes
Name |
Description |
Token |
Plugin to generate a text field and consume tokens for mappings. |