HubspotFormFormatter.php in Hubspot forms 8
File
src/Plugin/Field/FieldFormatter/HubspotFormFormatter.php
View source
<?php
namespace Drupal\hubspot_forms\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\hubspot_forms\HubspotFormsCore;
class HubspotFormFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$HubspotFormsCore = new HubspotFormsCore();
foreach ($items as $delta => $item) {
list($portal_id, $form_id) = explode('::', $item->form_id);
if ($HubspotFormsCore
->isConnected()) {
$elements[$delta] = [
'#theme' => 'hubspot_form',
'#target' => Html::getUniqueId('field-' . $this
->getBaseId() . '-' . $form_id),
'#portal_id' => $portal_id,
'#form_id' => $form_id,
'#locale' => $langcode,
];
}
else {
$elements[$delta] = [
'#markup' => $this
->t('Please provide a valid Hubspot API key.'),
];
}
}
return $elements;
}
}