HubspotForms.php in Hubspot forms 8
File
src/Plugin/Filter/HubspotForms.php
View source
<?php
namespace Drupal\hubspot_forms\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
class HubspotForms extends FilterBase {
public function process($text, $langcode) {
if (preg_match_all('/\\[hubspot\\-form(\\:(.+))?( .+)?\\]/isU', $text, $matches_code)) {
foreach ($matches_code[0] as $ci => $code) {
$form = [
'form_id' => $matches_code[2][$ci],
];
if (!empty($matches_code[3][$ci]) && preg_match_all('/\\s+([a-zA-Z_]+)\\:(\\s+)?([0-9a-zA-Z\\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
foreach ($matches_attributes[0] as $ai => $attribute) {
$form[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
}
}
$element = [
'#theme' => 'hubspot_form',
'#target' => Html::getUniqueId('filter-' . $this
->getBaseId() . '-' . $form['form_id']),
'#portal_id' => $form['portal_id'],
'#form_id' => $form['form_id'],
];
$replacement = \Drupal::service('renderer')
->render($element);
$text = str_replace($code, $replacement, $text);
}
}
return new FilterProcessResult($text);
}
}