class HubspotFormsCore in Hubspot forms 8
Class HubspotFormsCore.
@package Drupal\hubspot_forms
Hierarchy
- class \Drupal\hubspot_forms\HubspotFormsCore uses StringTranslationTrait
Expanded class hierarchy of HubspotFormsCore
4 files declare their use of HubspotFormsCore
- HubspotBlock.php in src/
Plugin/ Block/ HubspotBlock.php - HubspotDialog.php in src/
Form/ HubspotDialog.php - HubspotFormFormatter.php in src/
Plugin/ Field/ FieldFormatter/ HubspotFormFormatter.php - HubspotFormSelectWidget.php in src/
Plugin/ Field/ FieldWidget/ HubspotFormSelectWidget.php
File
- src/
HubspotFormsCore.php, line 13
Namespace
Drupal\hubspot_formsView source
class HubspotFormsCore {
use StringTranslationTrait;
/**
* Get form ids.
*/
public function getFormIds() {
$cid = 'hubspot_forms';
$forms = NULL;
if ($cache = \Drupal::cache()
->get($cid)) {
$forms = $cache->data;
}
else {
$forms = $this
->fetchHubspotForms();
usort($forms, function ($a, $b) {
return $b->createdAt - $a->createdAt;
});
\Drupal::cache()
->set($cid, $forms);
}
$form_ids = [
'' => $this
->t('Choose a Hubspot form'),
];
if (!empty($forms)) {
foreach ($forms as $item) {
$form_ids[$item->portalId . '::' . $item->guid] = $item->name;
}
}
return $form_ids;
}
/**
* Make an API call to Hubspot Forms API
* and get a list of all available forms.
*/
public function fetchHubspotForms() {
$config = \Drupal::config('hubspot_forms.settings');
$api_key = $config
->get('hubspot_api_key');
try {
// [Get all forms from a portal](http://developers.hubspot.com/docs/methods/forms/v2/get_forms)
$uri = 'https://api.hubapi.com/forms/v2/forms?hapikey=' . $api_key;
$client = \Drupal::httpClient([
'base_url' => $uri,
]);
$request = $client
->request('GET', $uri, [
'timeout' => 5,
'headers' => [
'Accept' => 'application/json',
],
]);
if ($request
->getStatusCode() == 200) {
$response = json_decode($request
->getBody());
if (empty($response)) {
return [];
}
else {
return $response;
}
}
else {
return [];
}
} catch (\GuzzleHttp\Exception\ClientException $e) {
$message = $e
->getMessage() . '. Make sure you provided correct Hubspot API Key on the configuration page.';
\Drupal::logger('hubspot_forms')
->notice($message);
return [];
} catch (\GuzzleHttp\Exception\ConnectException $e) {
$message = $e
->getMessage();
\Drupal::logger('hubspot_forms')
->notice($message);
return [];
}
}
/**
* Check Hubspot connection.
*/
public function isConnected() {
$forms = $this
->fetchHubspotForms();
return count($forms);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HubspotFormsCore:: |
public | function | Make an API call to Hubspot Forms API and get a list of all available forms. | |
HubspotFormsCore:: |
public | function | Get form ids. | |
HubspotFormsCore:: |
public | function | Check Hubspot connection. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |