You are here

function campaignmonitor_registration_hold_user_update in Campaign Monitor 8

Implements hook_ENTITY_TYPE_update().

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

File

modules/campaignmonitor_registration_hold/campaignmonitor_registration_hold.module, line 37
Campaign Monitor hold functions .

Code

function campaignmonitor_registration_hold_user_update(EntityInterface $entity) {

  // When the user is updated and is activated check for a hold record.
  // Todo provide a check to see if previous status was 0.
  if ($entity
    ->get('status')
    ->getValue()[0]['value'] == 1) {
    if ($lists = campaignmonitor_registration_hold_user_records($entity
      ->id())) {
      $mail = $entity
        ->get('mail')
        ->getValue()[0]['value'];
      foreach ($lists as $list_id) {
        $settings = campaignmonitor_get_list_settings($list_id);
        $name = $entity
          ->get('name')
          ->getValue()[0]['value'];
        if (isset($settings['display']['name_field']) && is_string($settings['display']['name_field']) && $settings['display']['name_field'] != '') {
          $field_name = $entity
            ->get($settings['display']['name_field'])
            ->getValue();
          if (isset($field_name[0]['value']) && $field_name[0]['value'] != '') {
            $name = $field_name[0]['value'];
          }
        }

        // Try to subscribe to the list.
        try {
          campaignmonitor_subscribe($list_id, SafeMarkup::checkPlain($mail), SafeMarkup::checkPlain($name));

          // Todo send out an alert?
          campaignmonitor_registration_hold_delete_record($entity
            ->id(), $list_id);
        } catch (Exception $e) {
          \Drupal::logger('campaignmonitor_registration_hold')
            ->error('An error occurred subscribing {email} to list {list}.
          "{message}"', [
            'email' => $mail,
            'list' => $list_id,
            'message' => $e
              ->getMessage(),
          ]);
        }
      }
    }
  }
}