Loading src/Form/HubspotMappingForm.php +71 −22 Original line number Diff line number Diff line Loading @@ -3,12 +3,12 @@ namespace Drupal\hubspot_integration\Form; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\hubspot_integration\Services\HubspotAPI; use Drupal\taxonomy\Entity\Vocabulary; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\taxonomy\Entity\Term; /** * Configures Hubspot mapping for this site. Loading @@ -16,27 +16,51 @@ use Drupal\taxonomy\Entity\Term; class HubspotMappingForm extends ConfigFormBase { /** * The config factory service. * * @var \Drupal\Core\Config\ConfigFactoryInterface|null */ protected $configFactory = NULL; /** * The Hubspot API service. * * @var \Drupal\hubspot_integration\Services\HubspotAPI|null */ protected $hubspotApi = NULL; /** * The messenger service. * * @var \Drupal\Core\Messenger\MessengerInterface */ protected $messenger; /** * The entity type manager service. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The properties. * * @var array */ protected $properties = []; /** * The mapping array. * * @var array|array[]|\Drupal\Component\Render\MarkupInterface|\Drupal\hubspot_integration\Services\unknown[]|mixed|string|null */ protected $mapping = []; /** * @var string[] * The managed types. * * @var array */ protected $managedTypes = ['enumeration']; Loading @@ -46,7 +70,9 @@ class HubspotMappingForm extends ConfigFormBase { public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), $container->get('hubspot_integration.api') $container->get('hubspot_integration.api'), $container->get('messenger'), $container->get('entity_type.manager') ); } Loading @@ -57,14 +83,22 @@ class HubspotMappingForm extends ConfigFormBase { * The config factory service. * @param \Drupal\hubspot_integration\Services\HubspotAPI $hubspotApi * The Hubspot API service. * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager service. */ public function __construct(ConfigFactoryInterface $configFactory, HubspotAPI $hubspotApi) { public function __construct(ConfigFactoryInterface $configFactory, HubspotAPI $hubspotApi, MessengerInterface $messenger, EntityTypeManagerInterface $entityTypeManager) { $this->configFactory = $configFactory; $this->hubspotApi = $hubspotApi; $this->messenger = $messenger; $this->entityTypeManager = $entityTypeManager; $properties = $this->hubspotApi->getContactProperties(); if ($properties) { foreach ($properties as $property) { $this->properties[$property->name] = $property; } } $this->mapping = $this->hubspotApi->getMapping(); parent::__construct($configFactory); } Loading @@ -89,7 +123,7 @@ class HubspotMappingForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state) { $settings = $this->config('hubspot_integration.settings'); if (empty($settings->get('token_private_app'))) { \Drupal::messenger()->addError($this->t('No token yet.')); $this->messenger->addError($this->t('No token yet.')); return $form; } if ($form_state->isSubmitted()) { Loading Loading @@ -132,9 +166,8 @@ class HubspotMappingForm extends ConfigFormBase { '#description' => $property->description, '#open' => FALSE, ]; $form['mapping']['details__' . $property_name] += $this->mappingItemForm($property, $form_state, $mapping); $form['mapping']['details__' . $property_name] += $this->mappingItemForm($property, $form_state); } if ($triggering_element = $form_state->getUserInput()['_triggering_element_name']) { if ($triggering_element == 'add_item') { $property_name = $form_state->getValue('property_select'); Loading @@ -160,18 +193,22 @@ class HubspotMappingForm extends ConfigFormBase { * Build a form item depending on hubspot item type. * * @param $property * The properties. * @param \Drupal\Core\Form\FormStateInterface $form_state * @param $mapping * The form state object. * * @return array * The item options. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ protected function mappingItemForm($property, FormStateInterface $form_state, $mapping = NULL) { protected function mappingItemForm($property, FormStateInterface $form_state) { $item = []; switch ($property->type) { case 'enumeration': $item = $this->mappingItemEnumerationForm($property, $form_state, $mapping); $item = $this->mappingItemEnumerationForm($property, $form_state); break; // @todo handler ? case 'bool': // @todo handler ? Loading @@ -187,17 +224,22 @@ class HubspotMappingForm extends ConfigFormBase { } /** * Build an taxonomy mapping form item (Hubspot Enumeration) * Build a form item depending on hubspot item type. * * @param $property * The properties. * @param \Drupal\Core\Form\FormStateInterface $form_state * @param $mapping * The form state object. * * @return array * The item options. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ protected function mappingItemEnumerationForm($property, FormStateInterface $form_state, $mapping = NULL) { protected function mappingItemEnumerationForm($property, FormStateInterface $form_state) { $item_opts = []; $vocabularies = Vocabulary::loadMultiple(); $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->loadMultiple(); $vocabulariesList = []; foreach ($vocabularies as $vid => $vocablary) { $vocabulariesList[$vid] = $vocablary->get('name'); Loading @@ -220,14 +262,15 @@ class HubspotMappingForm extends ConfigFormBase { '#attributes' => ['id' => 'mapping__' . $property->name], ]; foreach ($property->options as $option) { // Hubspot label & values are human readable, we have to create a machine name to avoid the forms items key issues. $option->value = HubspotAPI::machineName($option->value); // Hubspot label & values are human readable, we have to create a machine // name to avoid the forms items key issues. $option->value = $this->hubspotApi->machineName($option->value); $item_opts['mapping__' . $property->name]['hubspot__' . $property->name . '__' . $option->value] = [ '#type' => 'entity_autocomplete', '#target_type' => 'taxonomy_term', '#title' => $option->label, '#description' => $option->description, '#default_value' => (isset($this->mapping[$property->name]['#terms'][$option->value])) ? Term::load($this->mapping[$property->name]['#terms'][$option->value]) : NULL, '#default_value' => (isset($this->mapping[$property->name]['#terms'][$option->value])) ? $this->entityTypeManager->getStorage('taxonomy_term')->load($this->mapping[$property->name]['#terms'][$option->value]) : NULL, '#tags' => FALSE, '#selection_settings' => [ 'target_bundles' => [(isset($this->mapping[$property->name]['#vocabulary'])) ? $this->mapping[$property->name]['#vocabulary'] : NULL], Loading @@ -243,9 +286,12 @@ class HubspotMappingForm extends ConfigFormBase { * Ajax callback when selecting a mapping option. * * @param array $form * The form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state object. * * @return array * The form field. */ public function populateMappingOptions(array &$form, FormStateInterface $form_state) { $property_name = str_replace('vocabulary__', '', $form_state->getUserInput()['_triggering_element_name']); Loading @@ -256,9 +302,12 @@ class HubspotMappingForm extends ConfigFormBase { * Ajax callback when add a new mapping item form. * * @param array $form * The form array. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state object. * * @return array * The form field. */ public function addProperty(array &$form, FormStateInterface $form_state) { return $form['mapping']; Loading @@ -276,7 +325,7 @@ class HubspotMappingForm extends ConfigFormBase { } /** * * Get the mapping. */ protected static function getMappingFromFormState(FormStateInterface $form_state) { $mapping = []; Loading Loading
src/Form/HubspotMappingForm.php +71 −22 Original line number Diff line number Diff line Loading @@ -3,12 +3,12 @@ namespace Drupal\hubspot_integration\Form; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\hubspot_integration\Services\HubspotAPI; use Drupal\taxonomy\Entity\Vocabulary; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\taxonomy\Entity\Term; /** * Configures Hubspot mapping for this site. Loading @@ -16,27 +16,51 @@ use Drupal\taxonomy\Entity\Term; class HubspotMappingForm extends ConfigFormBase { /** * The config factory service. * * @var \Drupal\Core\Config\ConfigFactoryInterface|null */ protected $configFactory = NULL; /** * The Hubspot API service. * * @var \Drupal\hubspot_integration\Services\HubspotAPI|null */ protected $hubspotApi = NULL; /** * The messenger service. * * @var \Drupal\Core\Messenger\MessengerInterface */ protected $messenger; /** * The entity type manager service. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The properties. * * @var array */ protected $properties = []; /** * The mapping array. * * @var array|array[]|\Drupal\Component\Render\MarkupInterface|\Drupal\hubspot_integration\Services\unknown[]|mixed|string|null */ protected $mapping = []; /** * @var string[] * The managed types. * * @var array */ protected $managedTypes = ['enumeration']; Loading @@ -46,7 +70,9 @@ class HubspotMappingForm extends ConfigFormBase { public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), $container->get('hubspot_integration.api') $container->get('hubspot_integration.api'), $container->get('messenger'), $container->get('entity_type.manager') ); } Loading @@ -57,14 +83,22 @@ class HubspotMappingForm extends ConfigFormBase { * The config factory service. * @param \Drupal\hubspot_integration\Services\HubspotAPI $hubspotApi * The Hubspot API service. * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager service. */ public function __construct(ConfigFactoryInterface $configFactory, HubspotAPI $hubspotApi) { public function __construct(ConfigFactoryInterface $configFactory, HubspotAPI $hubspotApi, MessengerInterface $messenger, EntityTypeManagerInterface $entityTypeManager) { $this->configFactory = $configFactory; $this->hubspotApi = $hubspotApi; $this->messenger = $messenger; $this->entityTypeManager = $entityTypeManager; $properties = $this->hubspotApi->getContactProperties(); if ($properties) { foreach ($properties as $property) { $this->properties[$property->name] = $property; } } $this->mapping = $this->hubspotApi->getMapping(); parent::__construct($configFactory); } Loading @@ -89,7 +123,7 @@ class HubspotMappingForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state) { $settings = $this->config('hubspot_integration.settings'); if (empty($settings->get('token_private_app'))) { \Drupal::messenger()->addError($this->t('No token yet.')); $this->messenger->addError($this->t('No token yet.')); return $form; } if ($form_state->isSubmitted()) { Loading Loading @@ -132,9 +166,8 @@ class HubspotMappingForm extends ConfigFormBase { '#description' => $property->description, '#open' => FALSE, ]; $form['mapping']['details__' . $property_name] += $this->mappingItemForm($property, $form_state, $mapping); $form['mapping']['details__' . $property_name] += $this->mappingItemForm($property, $form_state); } if ($triggering_element = $form_state->getUserInput()['_triggering_element_name']) { if ($triggering_element == 'add_item') { $property_name = $form_state->getValue('property_select'); Loading @@ -160,18 +193,22 @@ class HubspotMappingForm extends ConfigFormBase { * Build a form item depending on hubspot item type. * * @param $property * The properties. * @param \Drupal\Core\Form\FormStateInterface $form_state * @param $mapping * The form state object. * * @return array * The item options. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ protected function mappingItemForm($property, FormStateInterface $form_state, $mapping = NULL) { protected function mappingItemForm($property, FormStateInterface $form_state) { $item = []; switch ($property->type) { case 'enumeration': $item = $this->mappingItemEnumerationForm($property, $form_state, $mapping); $item = $this->mappingItemEnumerationForm($property, $form_state); break; // @todo handler ? case 'bool': // @todo handler ? Loading @@ -187,17 +224,22 @@ class HubspotMappingForm extends ConfigFormBase { } /** * Build an taxonomy mapping form item (Hubspot Enumeration) * Build a form item depending on hubspot item type. * * @param $property * The properties. * @param \Drupal\Core\Form\FormStateInterface $form_state * @param $mapping * The form state object. * * @return array * The item options. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ protected function mappingItemEnumerationForm($property, FormStateInterface $form_state, $mapping = NULL) { protected function mappingItemEnumerationForm($property, FormStateInterface $form_state) { $item_opts = []; $vocabularies = Vocabulary::loadMultiple(); $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->loadMultiple(); $vocabulariesList = []; foreach ($vocabularies as $vid => $vocablary) { $vocabulariesList[$vid] = $vocablary->get('name'); Loading @@ -220,14 +262,15 @@ class HubspotMappingForm extends ConfigFormBase { '#attributes' => ['id' => 'mapping__' . $property->name], ]; foreach ($property->options as $option) { // Hubspot label & values are human readable, we have to create a machine name to avoid the forms items key issues. $option->value = HubspotAPI::machineName($option->value); // Hubspot label & values are human readable, we have to create a machine // name to avoid the forms items key issues. $option->value = $this->hubspotApi->machineName($option->value); $item_opts['mapping__' . $property->name]['hubspot__' . $property->name . '__' . $option->value] = [ '#type' => 'entity_autocomplete', '#target_type' => 'taxonomy_term', '#title' => $option->label, '#description' => $option->description, '#default_value' => (isset($this->mapping[$property->name]['#terms'][$option->value])) ? Term::load($this->mapping[$property->name]['#terms'][$option->value]) : NULL, '#default_value' => (isset($this->mapping[$property->name]['#terms'][$option->value])) ? $this->entityTypeManager->getStorage('taxonomy_term')->load($this->mapping[$property->name]['#terms'][$option->value]) : NULL, '#tags' => FALSE, '#selection_settings' => [ 'target_bundles' => [(isset($this->mapping[$property->name]['#vocabulary'])) ? $this->mapping[$property->name]['#vocabulary'] : NULL], Loading @@ -243,9 +286,12 @@ class HubspotMappingForm extends ConfigFormBase { * Ajax callback when selecting a mapping option. * * @param array $form * The form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state object. * * @return array * The form field. */ public function populateMappingOptions(array &$form, FormStateInterface $form_state) { $property_name = str_replace('vocabulary__', '', $form_state->getUserInput()['_triggering_element_name']); Loading @@ -256,9 +302,12 @@ class HubspotMappingForm extends ConfigFormBase { * Ajax callback when add a new mapping item form. * * @param array $form * The form array. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state object. * * @return array * The form field. */ public function addProperty(array &$form, FormStateInterface $form_state) { return $form['mapping']; Loading @@ -276,7 +325,7 @@ class HubspotMappingForm extends ConfigFormBase { } /** * * Get the mapping. */ protected static function getMappingFromFormState(FormStateInterface $form_state) { $mapping = []; Loading