You are here

public function MailchimpSignupFormFormatter::viewElements in Mailchimp 8

Same name and namespace in other branches
  1. 2.x modules/mailchimp_signup/src/Plugin/Field/FieldFormatter/MailchimpSignupFormFormatter.php \Drupal\mailchimp_signup\Plugin\Field\FieldFormatter\MailchimpSignupFormFormatter::viewElements()

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

modules/mailchimp_signup/src/Plugin/Field/FieldFormatter/MailchimpSignupFormFormatter.php, line 34

Class

MailchimpSignupFormFormatter
Plugin implementation of the 'mailchimp_signup_form' formatter.

Namespace

Drupal\mailchimp_signup\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    if (!empty($item->_loaded)) {
      $entity = $item->entity;
    }
    $signup_id = $entity
      ->id();

    /* @var $signup \Drupal\mailchimp_signup\Entity\MailchimpSignup */
    $signup = mailchimp_signup_load($signup_id);
    $messenger = \Drupal::messenger();
    $form = new \Drupal\mailchimp_signup\Form\MailchimpSignupPageForm($messenger);
    $form
      ->setFormID($this
      ->getFormId($signup));
    $form
      ->setSignup($signup);
    $elements[$delta] = \Drupal::formBuilder()
      ->getForm($form);
  }
  return $elements;
}