You are here

function _mailchimp_lists_mergevars_populate in Mailchimp 2.x

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

Helper function to complete a mailchimp-api-ready mergevars array.

See also

\Drupal\ctools\TypedDataResolver::getContextFromProperty()

3 calls to _mailchimp_lists_mergevars_populate()
mailchimp_lists_populate_member_batch in modules/mailchimp_lists/mailchimp_lists.module
Batch processor for member mergevar updates to built the mergevar arrays.
mailchimp_lists_process_subscribe_form_choices in modules/mailchimp_lists/mailchimp_lists.module
Processor for various list form submissions.
_mailchimp_lists_subscription_has_changed in modules/mailchimp_lists/mailchimp_lists.module
Helper function to avoid sending superfluous updates to Mailchimp.

File

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

Code

function _mailchimp_lists_mergevars_populate($merge_fields, EntityInterface $entity) {
  $mergevars = [];
  foreach (array_filter($merge_fields) as $label => $property_path) {
    $data = $entity
      ->getTypedData();
    $value = NULL;
    foreach (explode(':', $property_path) as $name) {
      if ($data instanceof ListInterface) {
        if (!is_numeric($name)) {

          // Implicitly default to delta 0 for audiences when not specified.
          $data = $data
            ->first();
        }
        else {

          // If we have a delta, fetch it and continue with the next part.
          $data = $data
            ->get($name);
          continue;
        }
      }

      // Forward to the target value if this is a data reference.
      if ($data instanceof DataReferenceInterface) {
        $data = $data
          ->getTarget();
      }

      // If there no data then the field is empty, ignore this.
      if (!$data) {
        break;
      }
      if (!$data
        ->getDataDefinition()
        ->getPropertyDefinition($name)) {

        // @todo What should we do here, ignore silently?
        throw new \Exception("Unknown property {$name} in property path {$property_path}");
      }
      $data = $data
        ->get($name);
    }

    // It would be easier if the structure would always include the property
    // as well. For backwards compatibility, that is not done, in that case
    // default to the main property.
    if ($data instanceof FieldItemListInterface || $data instanceof FieldItemInterface) {
      $main_property = $data
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getMainPropertyName();
      $value = $data->{$main_property};
    }
    elseif ($data instanceof TypedDataInterface) {
      $value = $data
        ->getValue();
    }

    // Cast to string to avoid problems with NULL values that the API does not
    // accept.
    // @todo: Check if this causes problems with integers or other non-string
    // merge fields, assuming the exist.
    $mergevars[$label] = (string) $value;
  }

  // Allow other modules to alter the merge vars.
  // @todo: Remove entity type argument.
  $entity_type_id = $entity
    ->getEntityTypeId();
  \Drupal::moduleHandler()
    ->alter('mailchimp_lists_mergevars', $mergevars, $entity, $entity_type_id);
  return $mergevars;
}