HubspotBlock.php in Hubspot forms 8
File
src/Plugin/Block/HubspotBlock.php
View source
<?php
namespace Drupal\hubspot_forms\Plugin\Block;
use Drupal\Component\Utility\Html;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\hubspot_forms\HubspotFormsCore;
class HubspotBlock extends BlockBase {
public function defaultConfiguration() {
return [
'form_id' => '',
];
}
public function blockForm($form, FormStateInterface $form_state) {
$HubspotFormsCore = new HubspotFormsCore();
$form['form_id'] = [
'#type' => 'select',
'#title' => $this
->t('Hubspot Form'),
'#description' => $this
->t('Please choose a form you would like to display.'),
'#options' => $HubspotFormsCore
->getFormIds(),
'#default_value' => $this->configuration['form_id'],
'#required' => TRUE,
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['form_id'] = $form_state
->getValue('form_id');
}
public function build() {
list($portal_id, $form_id) = explode('::', $this->configuration['form_id']);
$langcode = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
return [
'#theme' => 'hubspot_form',
'#target' => Html::getUniqueId('block-' . $this
->getBaseId() . '-' . $form_id),
'#portal_id' => $portal_id,
'#form_id' => $form_id,
'#locale' => $langcode,
];
}
}