You are here

public function MailchimpListsFieldSubscribeFormatter::viewElements in Mailchimp 8

Same name and namespace in other branches
  1. 2.x modules/mailchimp_lists/src/Plugin/Field/FieldFormatter/MailchimpListsFieldSubscribeFormatter.php \Drupal\mailchimp_lists\Plugin\Field\FieldFormatter\MailchimpListsFieldSubscribeFormatter::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_lists/src/Plugin/Field/FieldFormatter/MailchimpListsFieldSubscribeFormatter.php, line 111

Class

MailchimpListsFieldSubscribeFormatter
Plugin implementation of the 'mailchimp_lists_field_subscribe' formatter.

Namespace

Drupal\mailchimp_lists\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  /* @var $item \Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription */
  foreach ($items as $delta => $item) {
    $form = new MailchimpListsSubscribeForm();
    $field_name = $item
      ->getFieldDefinition()
      ->getName();

    // Give each form a unqiue ID in case of mulitiple subscription forms.
    $field_form_id = 'mailchimp_lists_' . $field_name . '_form';
    $form
      ->setFormID($field_form_id);
    $form
      ->setFieldInstance($item);
    $form
      ->setFieldFormatter($this);
    $elements[$delta] = $this->formBuilder
      ->getForm($form);
  }
  return $elements;
}