You are here

class Token in Pardot Integration 2.x

Plugin to generate a text field and consume tokens for mappings.

Plugin annotation


@PardotFormMapFormatterPlugin(
 id = "token",
 label = @Translation("Token"),
 types = {
    "contact_form",
  }
)

Hierarchy

Expanded class hierarchy of Token

1 string reference to 'Token'
Token::buildConfigurationForm in src/Plugin/PardotFormMapFormatterPlugin/Token.php

File

src/Plugin/PardotFormMapFormatterPlugin/Token.php, line 21

Namespace

Drupal\pardot\Plugin\PardotFormMapFormatterPlugin
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Token::$token protected property Drupal\Core\Utility\Token definition.
Token::buildConfigurationForm public function
Token::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Token::defaultConfiguration public function
Token::getFormattedValue public function Get the form field from the form state and apply formatting.
Token::getPluginDefinition public function
Token::getPluginId public function
Token::submitConfigurationForm public function