You are here

public function HubspotFormFormatter::viewElements in Hubspot forms 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/HubspotFormFormatter.php, line 27

Class

HubspotFormFormatter
Plugin implementation of the 'field_hubspot_form_formatter' formatter.

Namespace

Drupal\hubspot_forms\Plugin\Field\FieldFormatter

Code

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;
}