Insert campaign programmatically with custom content and custom segment

Created on 29 November 2023, 7 months ago

Problem/Motivation

The campaign module doesn't allow custom segment_opts. Can something be made about this?

Steps to reproduce

The admin interface is too complicated for my customers. So i made mine.
My custom contacts have 3 fields that have been sent to mailchimp : priority, target, department

Here is the prototyped submit of my custom form:

  public function submitForm(array &$form, FormStateInterface $form_state) {	
      $nidref = $form_state->getValue('nid');
      $g_cible = $form_state->getValue('cible');
      $cible = [];
      foreach($g_cible as $value){
        if($value>0){
          $cible[] = $value;
        }
      }
      $g_priorite = $form_state->getValue('priorite');
      $priorite = [];
      foreach($g_priorite as $value){
        if($value>0){
          $priorite[] = $value;
        }
      }
      $g_departement = $form_state->getValue('departement');
      $departement = [];
      foreach($g_departement as $value){
        if($value>0){
          $departement[] = $value;
        }
      }

      $node = \Drupal::entityTypeManager()->getStorage('node')->load($nidref);
      $site_config = \Drupal::config('system.site');

      // CREER L'ENVOI A MAILCHIMP
      // 1/ CREER UN TEMPLATE HTML (node ID) [[ $template_content ]]
      $template_content = [
        'html' => [
          'value' => '[mailchimp_campaign|entity_type=node|entity_id='.$nidref.'|view_mode=mailchimp]',
          'format' => 'mailchimp_campaign',
        ],
      ];
      // 2/ CREER DES CONDITIONS POUR LA LISTE [[ $recipients ]] : utiliser [conditions] et [match] Γ  la place de [saved_segment_id]
      $recipients = (object) [
        'list_id' => 'dxxxxxxxx8',
      ];
      $recipients->segment_opts = (object) [
        'match' => 'all',//all ou any
        'conditions' => [],
      ];

      // LES CIBLES
      $les_conditions = [];
      foreach($cible as $value){
        $condition = (object) [
          'condition_type' => 'TextMerge',
          'field' => 'CIBLE',
          'op' => 'is',
          'value' => (string) $value,
        ];
        $les_conditions[] = $condition;
      }
      // LES PRIORITES
      foreach($priorite as $value){
        $condition = (object) [
          'condition_type' => 'TextMerge',
          'field' => 'PRIORITE',
          'op' => 'is',
          'value' => (string) $value,
        ];
        $les_conditions[] = $condition;
      }
      // LES DEPARTEMENTS
      foreach($departement as $value){
        $condition = (object) [
          'condition_type' => 'TextMerge',
          'field' => 'DEPARTEMEN',
          'op' => 'is',
          'value' => (string) $value,
        ];
        $les_conditions[] = $condition;
      }
      $recipients->segment_opts->conditions = $les_conditions;

      // 3/ CREER LES SETTINGS [[ $settings ]]
      $settings = (object) [
        'subject_line' => 'Envoi RDV '.$nidref,
        'title' => $node->get('field_titre')->value,
        'from_name' => $site_config->get('name'),
        'reply_to' => $site_config->get('mail'),
        'preview_text' => 'Campagne rendez-vous du node'.$nidref,
      ];

      $campaign_id = mailchimp_campaign_save_campaign($template_content, $recipients, $settings, NULL);
     // The function mailchimp_campaign_save_campaign need to accept custom segment_opts to work
      $form_state->setRedirect('mailchimp_campaign.overview');
  }

Proposed resolution

First issue :
About mailchimp_campaign_save_campaign() :
This function don't allow the use of custom segment_opts :

  if (isset($recipients->segment_opts)) {
    if (//!isset($recipients->segment_opts->saved_segment_id) // => MY FIRST ISSUE IS HERE
      //|| => SHOULD IT BE A && ?
      ( mailchimp_test_list_segment($recipients->list_id, $recipients->segment_opts->saved_segment_id) === NULL
        && !isset($recipients->segment_opts->match, $recipients->segment_opts->conditions) 
        )
    ) {
      //\Drupal::messenger()->addMessage('mailchimp_campaign_save_campaign return NULL ===> '.print_r($recipients,true), 'status');

      return NULL;
    }

Remaining tasks

Second issue
The campaign is successfully sent to mailchimp, ready to launch, but no entity has been created on drupal, the campaign interface is
empty.
What i am missing?

✨ Feature request
Status

Active

Version

2.2

Component

Campaign Module

Created by

πŸ‡«πŸ‡·France thomas-at-passerelle

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @thomas-at-passerelle
  • πŸ‡«πŸ‡·France thomas-at-passerelle

    The second issue was just like creating a node

          $campaign_id = mailchimp_campaign_save_campaign($template_content, $recipients, $settings, NULL);
          if (empty($campaign_id)) {
            $this->messenger()->addMessage(t('An error occurred while saving this campaign to MailChimp service.'), 'error');
          }
          else {
            // Create campaign entity and save
            $campaign = \Drupal::entityTypeManager()->getStorage('mailchimp_campaign')->create();
            $campaign->setMcCampaignId($campaign_id);
            $campaign->setTemplate($template_content);
            $campaign->save();
          }
    
Production build 0.69.0 2024