You are here

class AutoPath in Pardot Integration 2.x

Plugin to have an autocomplete textfield for typed data.

Plugin annotation


@PardotFormMapFormatterPlugin(
 id = "autopath",
 label = @Translation("Auto Path"),
 types = {
    "contact_form",
  }
)

Hierarchy

Expanded class hierarchy of AutoPath

File

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

Namespace

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

}

Members

Namesort descending Modifiers Type Description Overrides
AutoPath::$dataFetcher protected property The data fetcher object.
AutoPath::$entityTypeManager protected property Entity field manager to get the contact form field definitions.
AutoPath::buildConfigurationForm public function
AutoPath::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AutoPath::defaultConfiguration public function
AutoPath::getFormattedValue public function Get the form field from the form state and apply formatting.
AutoPath::getPluginDefinition public function
AutoPath::getPluginId public function
AutoPath::submitConfigurationForm public function