You are here

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;

/**
 * Plugin to generate a text field and consume tokens for mappings.
 *
 * @PardotFormMapFormatterPlugin(
 *  id = "token",
 *  label = @Translation("Token"),
 *  types = {
 *     "contact_form",
 *   }
 * )
 */
class Token extends PardotFormMapFormatterPluginBase implements ContainerFactoryPluginInterface {

  /**
   * Drupal\Core\Utility\Token definition.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

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

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

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

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

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

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

  /**
   * 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) {
    $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

Namesort descending Description
Token Plugin to generate a text field and consume tokens for mappings.