View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Utility\Token as TokenService;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce\Rest\RestClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\salesforce_mapping\MappingConstants;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Token extends SalesforceMappingFieldPluginBase {
protected $token;
protected $renderer;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, RestClientInterface $rest_client, EntityTypeManagerInterface $etm, DateFormatterInterface $dateFormatter, EventDispatcherInterface $event_dispatcher, TokenService $token, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_bundle_info, $entity_field_manager, $rest_client, $etm, $dateFormatter, $event_dispatcher);
$this->token = $token;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.bundle.info'), $container
->get('entity_field.manager'), $container
->get('salesforce.client'), $container
->get('entity_type.manager'), $container
->get('date.formatter'), $container
->get('event_dispatcher'), $container
->get('token'), $container
->get('renderer'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$pluginForm = parent::buildConfigurationForm($form, $form_state);
$token_browser = [
'token_browser' => [
'#theme' => 'token_tree_link',
'#token_types' => [
$form['#entity']
->getDrupalEntityType(),
],
'#global_types' => TRUE,
'#click_insert' => TRUE,
],
];
$pluginForm['drupal_field_value'] += [
'#type' => 'textfield',
'#default_value' => $this
->config('drupal_field_value'),
'#description' => $this
->t('Enter a token to map a Salesforce field. @token_browser', [
'@token_browser' => $this->renderer
->render($token_browser),
]),
];
$pluginForm['direction']['#options'] = [
MappingConstants::SALESFORCE_MAPPING_DIRECTION_DRUPAL_SF => $pluginForm['direction']['#options'][MappingConstants::SALESFORCE_MAPPING_DIRECTION_DRUPAL_SF],
];
$pluginForm['direction']['#default_value'] = MappingConstants::SALESFORCE_MAPPING_DIRECTION_DRUPAL_SF;
return $pluginForm;
}
public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
$text = $this
->config('drupal_field_value');
$data = [
$entity
->getEntityTypeId() => $entity,
];
$options = [
'clear' => TRUE,
];
$result = $this->token
->replace($text, $data, $options);
return trim($result) != '' ? $result : NULL;
}
public function pull() {
return FALSE;
}
}