/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $form = array();
     $field_settings = $this->fieldInstance->getFieldDefinition()->getSettings();
     $field_formatter_settings = $this->fieldFormatter->getSettings();
     $mc_list = mailchimp_get_list($field_settings['mc_list_id']);
     $email = mailchimp_lists_load_email($this->fieldInstance, $this->fieldInstance->getEntity());
     if (!$email) {
         return array();
     }
     $field_name = $this->fieldInstance->getFieldDefinition()->getName();
     // Determine if a user is subscribed to the list.
     $is_subscribed = mailchimp_is_subscribed($mc_list['id'], $email);
     $wrapper_key = 'mailchimp_' . $field_name;
     $form['wrapper_key'] = array('#type' => 'hidden', '#default_value' => $wrapper_key);
     $form[$wrapper_key] = array('#type' => 'container', '#tree' => TRUE, '#description' => $this->fieldInstance->getFieldDefinition()->getDescription(), '#attributes' => array('class' => array('mailchimp-newsletter-wrapper', 'mailchimp-newsletter-' . $field_name)));
     // Add the title and description to lists for anonymous users or if requested:
     $form[$wrapper_key]['subscribe'] = array('#type' => 'checkbox', '#title' => 'Subscribe', '#disabled' => $this->fieldInstance->getFieldDefinition()->isRequired(), '#required' => $this->fieldInstance->getFieldDefinition()->isRequired(), '#default_value' => $this->fieldInstance->getFieldDefinition()->isRequired() || $is_subscribed);
     // Present interest groups:
     if ($field_settings['show_interest_groups'] && $field_formatter_settings['show_interest_groups']) {
         // Perform test in case error comes back from MCAPI when getting groups:
         if (is_array($mc_list['intgroups'])) {
             $form[$wrapper_key]['interest_groups'] = array('#type' => 'fieldset', '#title' => isset($settings['interest_groups_label']) ? $settings['interest_groups_label'] : t('Interest Groups'), '#weight' => 100, '#states' => array('invisible' => array(':input[name="' . $wrapper_key . '[subscribe]"]' => array('checked' => FALSE))));
             $groups_default = $this->fieldInstance->getInterestGroups();
             if ($groups_default == NULL) {
                 $groups_default = array();
             }
             $form[$wrapper_key]['interest_groups'] += mailchimp_interest_groups_form_elements($mc_list, $groups_default, $email);
         }
     }
     $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     /* @var $instance \Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription */
     $instance = $items[0];
     $subscribe_default = $instance->getSubscribe();
     $email = NULL;
     if (!empty($instance->getEntity())) {
         $email = mailchimp_lists_load_email($instance, $instance->getEntity(), FALSE);
         if ($email) {
             $subscribe_default = mailchimp_is_subscribed($instance->getFieldDefinition()->getSetting('mc_list_id'), $email);
         }
     }
     $element += array('#title' => SafeMarkup::checkPlain($element['#title']), '#type' => 'fieldset');
     $element['subscribe'] = array('#title' => t('Subscribe'), '#type' => 'checkbox', '#default_value' => $subscribe_default ? TRUE : $this->fieldDefinition->isRequired(), '#required' => $this->fieldDefinition->isRequired(), '#disabled' => $this->fieldDefinition->isRequired());
     $form_id = $form_state->getFormObject()->getFormId();
     if ($this->fieldDefinition->getSetting('show_interest_groups') || $form_id == 'field_ui_field_edit_form') {
         $mc_list = mailchimp_get_list($instance->getFieldDefinition()->getSetting('mc_list_id'));
         $element['interest_groups'] = array('#type' => 'fieldset', '#title' => SafeMarkup::checkPlain($instance->getFieldDefinition()->getSetting('interest_groups_title')), '#weight' => 100, '#states' => array('invisible' => array(':input[name="' . $instance->getFieldDefinition()->getName() . '[0][value][subscribe]"]' => array('checked' => FALSE))));
         if ($form_id == 'field_ui_field_edit_form') {
             $element['interest_groups']['#states']['invisible'] = array(':input[name="field[settings][show_interest_groups]"]' => array('checked' => FALSE));
         }
         $groups_default = $instance->getInterestGroups();
         if ($groups_default == NULL) {
             $groups_default = array();
         }
         if ($mc_list['stats']['group_count']) {
             $element['interest_groups'] += mailchimp_interest_groups_form_elements($mc_list, $groups_default, $email);
         }
     }
     return array('value' => $element);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     global $request;
     $list_id = $request->attributes->get('_raw_variables')->get('list_id');
     $list = mailchimp_get_list($list_id);
     $form_state->set('list', $list);
     $default_webhook_actions = mailchimp_lists_default_webhook_actions();
     $enabled_webhook_actions = mailchimp_lists_enabled_webhook_actions($list_id);
     $form['webhook_actions'] = array('#type' => 'fieldset', '#title' => t('Enabled webhook actions for the !name list', array('!name' => $list['name'])), '#tree' => TRUE);
     foreach ($default_webhook_actions as $action => $name) {
         $form['webhook_actions'][$action] = array('#type' => 'checkbox', '#title' => $name, '#default_value' => in_array($action, $enabled_webhook_actions));
     }
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     /* @var $item \Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription */
     foreach ($items as $delta => $item) {
         $elements[$delta] = array();
         $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) {
             if (mailchimp_is_subscribed($field_settings['mc_list_id'], $email)) {
                 $status = t('Subscribed to %list', array('%list' => $mc_list['name']));
             } else {
                 $status = t('Not subscribed to %list', array('%list' => $mc_list['name']));
             }
         } else {
             $status = t('Invalid email configuration.');
         }
         $elements[$delta]['status'] = array('#markup' => $status, '#description' => t('@mc_list_description', array('@mc_list_description' => $item->getFieldDefinition()->getDescription())));
         if ($field_settings['show_interest_groups'] && $this->getSetting('show_interest_groups')) {
             $memberinfo = mailchimp_get_memberinfo($field_settings['mc_list_id'], $email);
             if (isset($memberinfo['merges']['GROUPINGS'])) {
                 $elements[$delta]['interest_groups'] = array('#type' => 'fieldset', '#title' => t('Interest Groups'), '#weight' => 100);
                 foreach ($memberinfo['merges']['GROUPINGS'] as $grouping) {
                     $items = array();
                     foreach ($grouping['groups'] as $interest) {
                         if ($interest['interested']) {
                             $items[] = $interest['name'];
                         }
                     }
                     if (count($items)) {
                         $elements[$delta]['interest_groups'][$grouping['id']] = array('#title' => $grouping['name'], '#theme' => 'item_list', '#items' => $items, '#type' => 'ul');
                     }
                 }
             }
         }
     }
     return $elements;
 }
 /**
  * Tests that a list can be loaded.
  */
 function testGetList()
 {
     $list = mailchimp_get_list(DrupalMailchimpLists::TEST_LIST_A);
     $this->assertEqual($list['id'], DrupalMailchimpLists::TEST_LIST_A, 'List can be loaded.');
 }