You are here

function mailchimp_lists_load_email in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_load_email()
  2. 7.3 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_load_email()
  3. 7.4 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_load_email()
  4. 2.x modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_load_email()

Helper function to check if a valid email is configured for an entity field.

Returns an email address or FALSE.

4 calls to mailchimp_lists_load_email()
MailchimpListsSelectWidget::formElement in modules/mailchimp_lists/src/Plugin/Field/FieldWidget/MailchimpListsSelectWidget.php
Returns the form for a single field widget.
MailchimpListsSubscribeDefaultFormatter::viewElements in modules/mailchimp_lists/src/Plugin/Field/FieldFormatter/MailchimpListsSubscribeDefaultFormatter.php
Builds a renderable array for a field value.
MailchimpListsSubscribeForm::buildForm in modules/mailchimp_lists/src/Form/MailchimpListsSubscribeForm.php
Form constructor.
mailchimp_lists_process_subscribe_form_choices in modules/mailchimp_lists/mailchimp_lists.module
Processor for various list form submissions.

File

modules/mailchimp_lists/mailchimp_lists.module, line 92
Mailchimp lists/audiences module.

Code

function mailchimp_lists_load_email(MailchimpListsSubscription $instance, EntityInterface $entity, $log_errors = TRUE) {
  $merge_fields = $instance
    ->getFieldDefinition()
    ->getSetting('merge_fields');
  if (empty($merge_fields) || !isset($merge_fields['EMAIL'])) {
    if ($log_errors) {
      \Drupal::logger('mailchimp_lists')
        ->notice('Mailchimp Audiences field "{field}" on {entity} -> {bundle} has no EMAIL field configured, subscription actions cannot take place.', [
        'field' => $instance
          ->getFieldDefinition()
          ->getName(),
        'entity' => $entity
          ->getEntityType()
          ->getLabel(),
        'bundle' => $entity
          ->bundle(),
      ]);
    }
    return FALSE;
  }

  // Get the entity's email property value from the user-defined email field.
  $mail_property = $instance
    ->getFieldDefinition()
    ->getSetting('merge_fields')['EMAIL'];

  /* @var $mail_field \Drupal\Core\Field\FieldItemList */
  $mail_field = $entity
    ->get($mail_property);
  if ($mail_field != NULL && \Drupal::service('email.validator')
    ->isValid($mail_field
    ->getString())) {
    return $mail_field
      ->getString();
  }
  else {
    return FALSE;
  }
}