marketo_suite-1.0.x-dev/modules/e3_marketo_site_studio/e3_marketo_site_studio.module
modules/e3_marketo_site_studio/e3_marketo_site_studio.module
<?php
/**
* @file
* Contains module hooks for site studio integration with E3 Marketo.
*/
/**
* Implements hook_preprocess_HOOK().
*/
function e3_marketo_site_studio_preprocess_custom_component_marketo_form_ss(&$variables) {
$form_uuid = $variables['field']['marketo_form']['#entity']['#entityId'] ?? NULL;
$form_title_override = $variables['field']['form_title_override'] ?? NULL;
$form_title = '';
// Load the form.
if ($form_uuid) {
/** @var \Drupal\e3_marketo\Entity\MarketoFormEntityInterface|NULL $marketo_form */
$marketo_form = \Drupal::service('entity.repository')->loadEntityByUuid('marketo_form', $form_uuid);
if ($marketo_form) {
$form_title = $marketo_form->label();
// We need to build the form render array here, NOT in the template. This
// will ensure submission overrides will apply, since Site Studio doesn't
// properly track component parents on references.
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$form_view = \Drupal::entityTypeManager()
->getViewBuilder('marketo_form')
->view($marketo_form, 'default', $language);
// Check if component overrides form submission behavior.
$sb_override = $variables['field']['submission_behavior'] ?? '';
if ($sb_override) {
/** @var \Twig\Markup|string $submission_confirmation */
$submission_confirmation = $variables['field']['submission_confirmation']['#text'] ?? '';
if ($submission_confirmation) {
$submission_confirmation = $submission_confirmation->__toString();
}
// Load in the plugin and grab submission callbacks from it.
/** @var \Drupal\e3_marketo\Plugin\SubmissionBehaviorInterface $sb_override_plugin */
$sb_override_plugin = \Drupal::service('plugin.manager.submission_behavior_manager')->createInstance($sb_override);
$submission_callbacks = $sb_override_plugin->getSubmissionCallbacks($marketo_form, [
'replacement' => $submission_confirmation,
'redirect_url' => $variables['field']['redirect_url'] ?? '',
]);
// Apply submission callback overrides.
$form_view['#pre_render'][] = function (array $build) use ($submission_callbacks) {
/** @var \Drupal\Core\Template\Attribute $attributes */
$attributes = $build['marketo_form_embed']['#form_attributes'] ?? NULL;
$form_id = $attributes->offsetGet('data-form-id')?->value();
$form_instance = $attributes->offsetGet('data-instance')?->value() ?: 0;
if ($form_id && isset($build['marketo_form_embed']['#attached']['drupalSettings']['marketoForms']["marketo-form-{$form_id}"][$form_instance])) {
$instance_build_info =& $build['marketo_form_embed']['#attached']['drupalSettings']['marketoForms']["marketo-form-{$form_id}"][$form_instance];
$instance_build_info['submissionCallbacks'] = $submission_callbacks;
}
return $build;
};
}
$st_override = $variables['field']['override_submit_text'] ?? '';
if ($st_override) {
// Apply overrides.
$form_view['#pre_render'][] = function (array $build) use ($st_override) {
/** @var \Drupal\Core\Template\Attribute $attributes */
$attributes = $build['marketo_form_embed']['#form_attributes'] ?? NULL;
$form_id = $attributes->offsetGet('data-form-id')?->value();
$form_instance = $attributes->offsetGet('data-instance')?->value() ?: 0;
if ($form_id && isset($build['marketo_form_embed']['#attached']['drupalSettings']['marketoForms']["marketo-form-{$form_id}"][$form_instance])) {
$instance_build_info =& $build['marketo_form_embed']['#attached']['drupalSettings']['marketoForms']["marketo-form-{$form_id}"][$form_instance];
$instance_build_info['overrideSubmitText'] = $st_override;
}
return $build;
};
}
}
}
// Setup either title override or form title as the heading.
$variables['form_title'] = $form_title_override ?: $form_title;
// Render the form.
if (isset($form_view)) {
$variables['marketo_form'] = $form_view;
}
}
