You are here

DefaultValue.php in Pardot Integration 2.x

File

src/Plugin/PardotFormMapFormatterPlugin/DefaultValue.php
View source
<?php

namespace Drupal\pardot\Plugin\PardotFormMapFormatterPlugin;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\pardot\Plugin\PardotFormMapFormatterPluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin to select from the list of fields.
 *
 * @PardotFormMapFormatterPlugin(
 *  id = "default_value",
 *  label = @Translation("Default Value"),
 *  types = {
 *     "contact_form",
 *   }
 * )
 */
class DefaultValue extends PardotFormMapFormatterPluginBase implements ContainerFactoryPluginInterface {

  /**
   * Entity field manager to get the contact form field definitions.
   *
   * @var \Drupal\Core\Entity\EntityFieldManager
   */
  protected $entityFieldManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance->entityFieldManager = $container
      ->get('entity_field.manager');
    return $instance;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['fields'] = [
      '#type' => 'select',
      '#title' => 'Fields',
      '#default_value' => $this->configuration['fields'],
      '#options' => $this
        ->getContactFormFieldCollection(),
      '#required' => TRUE,
    ];
    return $form;
  }

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

  /**
   * Get the list of fields to map with.
   *
   * @return array|mixed|null
   *   Just return the mapped field container.
   */
  public function getContactFormFieldCollection() {
    $fields = [];
    $entity_id = explode(':', $this->configuration['entity_id'])[1] ?? '';
    $allowed_types = $this->configuration['allowed_field_types'] ?? [];
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions('contact_message', $entity_id);
    foreach ($field_definitions as $field_name => $field_definition) {
      $i = in_array($field_definition
        ->getType(), $allowed_types);
      $k = $field_definition
        ->getType();
      if ($allowed_types && in_array($field_definition
        ->getType(), $allowed_types)) {
        $fields[$field_name] = (string) $field_definition
          ->getLabel();
      }
      elseif (!$allowed_types) {
        $fields[$field_name] = (string) $field_definition
          ->getLabel();
      }
    }
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormattedValue(FormStateInterface $form_state) {
    $field_name = $this
      ->getConfiguration()['fields'] ?? FALSE;
    if ($field_name) {
      $value = $form_state
        ->getValue($field_name);
    }
    else {
      $value = NULL;
    }
    if (is_array($value)) {

      // If this is an entity selection we get a target id so lets just get the title.
      $target_id = array_column($value, 'target_id');
      if ($target_id) {
        $value = $this
          ->getEntityReferenceValues($field_name, $form_state);
      }
      else {

        // I think if its not target_id then its value and we will just have all the values in an array.
        $value = implode(',', array_values(array_column($value, 'value')));
      }
    }
    return $value;
  }

  /**
   * Get entity reference entity labels.
   *
   * @param string $field_name
   *   The name of the field we are looking at.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state that holds the input values.
   *
   * @return string
   *   String of comma seperated entity labels.
   */
  private function getEntityReferenceValues(string $field_name, FormStateInterface $form_state) {
    $value = [];
    $form_entity = $form_state
      ->getFormObject()
      ->getEntity();
    $selected_entities = $form_entity
      ->get($field_name)
      ->referencedEntities();
    foreach ($selected_entities as $selected_entity) {
      $value[] = $selected_entity
        ->label();
    }
    return implode(',', $value);
  }

}

Classes

Namesort descending Description
DefaultValue Plugin to select from the list of fields.