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;
class DefaultValue extends PardotFormMapFormatterPluginBase implements ContainerFactoryPluginInterface {
protected $entityFieldManager;
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;
}
public function getPluginId() {
return 'default_value';
}
public function getPluginDefinition() {
return $this->pluginDefinition;
}
public function defaultConfiguration() {
return [
'fields' => '',
];
}
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;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['fields'] = $form_state
->getValue('fields');
}
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;
}
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)) {
$target_id = array_column($value, 'target_id');
if ($target_id) {
$value = $this
->getEntityReferenceValues($field_name, $form_state);
}
else {
$value = implode(',', array_values(array_column($value, 'value')));
}
}
return $value;
}
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);
}
}