You are here

public function MailchimpListsSubscribeDefaultFormatter::viewElements in Mailchimp 8

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

Class

MailchimpListsSubscribeDefaultFormatter
Plugin implementation of the 'mailchimp_lists_subscribe_default' 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) {
    $elements[$delta] = [];
    $field_settings = $this
      ->getFieldSettings();
    $mc_list = mailchimp_get_list($field_settings['mc_list_id']);
    $email = mailchimp_lists_load_email($item, $item
      ->getEntity(), FALSE);
    if ($email && !empty($mc_list)) {
      if (mailchimp_is_subscribed($field_settings['mc_list_id'], $email)) {
        $status = $this
          ->t('Subscribed to %list', [
          '%list' => $mc_list->name,
        ]);
      }
      else {
        $status = $this
          ->t('Not subscribed to %list', [
          '%list' => $mc_list->name,
        ]);
      }
    }
    else {
      $status = $this
        ->t('Invalid email configuration.');
    }
    $elements[$delta]['status'] = [
      '#markup' => $status,
      '#description' => $this
        ->t('@mc_list_description', [
        '@mc_list_description' => $item
          ->getFieldDefinition()
          ->getDescription(),
      ]),
    ];
    if ($field_settings['show_interest_groups'] && $this
      ->getSetting('show_interest_groups')) {
      $member_info = mailchimp_get_memberinfo($field_settings['mc_list_id'], $email);
      if (!empty($mc_list->intgroups)) {
        $elements[$delta]['interest_groups'] = [
          '#type' => 'fieldset',
          '#title' => $this
            ->t('Interest Groups'),
          '#weight' => 100,
        ];
        foreach ($mc_list->intgroups as $interest_group) {
          $items = [];
          foreach ($interest_group->interests as $interest) {
            if (isset($member_info->interests->{$interest->id}) && $member_info->interests->{$interest->id} === TRUE) {
              $items[] = $interest->name;
            }
          }
          if (count($items) > 0) {
            $elements[$delta]['interest_groups'][$interest_group->id] = [
              '#title' => $interest_group->title,
              '#theme' => 'item_list',
              '#items' => $items,
              '#type' => 'ul',
            ];
          }
        }
      }
    }
  }
  return $elements;
}