<?php

/**
 * @file
 * Defines Marketo form field type.
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function marketo_form_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'marketo_form.page.link':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>Marketo form integration</p>';
      return $output;
  }
}

/**
 * Implements hook_theme().
 */
function marketo_form_theme($existing, $type, $theme, $path) {
  return [
    'paragraph__marketo_form' => [
      'template' => 'paragraph',
      'base hook' => 'paragraph',
      'path' => drupal_get_path('module', 'parade') . '/templates',
    ],
    'marketo_form_field' => [
      'variables' => [
        'forms' => NULL,
        'entity' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_preprocess_field__parade_marketo_form().
 */
function marketo_form_preprocess_field__parade_marketo_form(&$variables) {
  // Add rendered_confirmation_message to field variables.
  $element = &$variables['element']['#object'];

  // Get the field's raw value and format.
  list($markup, $format) = [
    $element->parade_text->value,
    $element->parade_text->format,
  ];

  if (isset($markup) && isset($format)) {
    $element->rendered_confirmation_message = check_markup($markup, $format);
  }
}
