You are here

class DefaultValue in Pardot Integration 2.x

Plugin to select from the list of fields.

Plugin annotation


@PardotFormMapFormatterPlugin(
 id = "default_value",
 label = @Translation("Default Value"),
 types = {
    "contact_form",
  }
)

Hierarchy

Expanded class hierarchy of DefaultValue

File

src/Plugin/PardotFormMapFormatterPlugin/DefaultValue.php, line 22

Namespace

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

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultValue::$entityFieldManager protected property Entity field manager to get the contact form field definitions.
DefaultValue::buildConfigurationForm public function
DefaultValue::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
DefaultValue::defaultConfiguration public function
DefaultValue::getContactFormFieldCollection public function Get the list of fields to map with.
DefaultValue::getEntityReferenceValues private function Get entity reference entity labels.
DefaultValue::getFormattedValue public function
DefaultValue::getPluginDefinition public function
DefaultValue::getPluginId public function
DefaultValue::submitConfigurationForm public function