You are here

class AddMailchimpEvent in Mailchimp 2.x

A sample form for adding a Mailchimp Event.

Hierarchy

Expanded class hierarchy of AddMailchimpEvent

1 string reference to 'AddMailchimpEvent'
mailchimp_events_example.routing.yml in modules/mailchimp_events/modules/mailchimp_events_example/mailchimp_events_example.routing.yml
modules/mailchimp_events/modules/mailchimp_events_example/mailchimp_events_example.routing.yml

File

modules/mailchimp_events/modules/mailchimp_events_example/src/Form/AddMailchimpEvent.php, line 16

Namespace

Drupal\mailchimp_events_example\Form
View source
class AddMailchimpEvent extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'add_mailchimp_event';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['description']['#markup'] = $this
      ->t('Cause an event to occur now, for a specific audience member in Mailchimp.');
    $add_link = Link::createFromRoute('You can add event types here.', 'entity.mailchimp_event.add_form', [], [
      'attributes' => [
        'target' => '_blank',
      ],
    ]);
    $mc_lists = mailchimp_get_lists();
    $list_options = [];
    foreach ($mc_lists as $key => $value) {
      $list_options[$key] = $value->name;
    }
    $events = MailchimpEvent::loadMultiple();
    $event_options = [];
    if (empty($events)) {
      $this
        ->messenger()
        ->addError($this
        ->t('At least one event type is required. @add-link', [
        '@add-link' => $add_link
          ->toString(),
      ]));
      return $form;
    }
    else {
      foreach ($events as $key => $event) {
        $event_options[$event
          ->getName()] = $event
          ->getName();
      }
    }
    $form['list'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Mailchimp audience'),
      '#weight' => '0',
      '#required' => TRUE,
      '#options' => $list_options,
    ];
    $form['email'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('Email'),
      '#description' => $this
        ->t('The email address to associate with this event.'),
      '#weight' => '0',
      '#required' => TRUE,
    ];
    $form['event_name'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Event Name'),
      '#description' => $this
        ->t('The name of the Event. %add_link', [
        '%add_link' => $add_link
          ->toString(),
      ]),
      '#weight' => '0',
      '#required' => TRUE,
      '#options' => $event_options,
    ];
    $form['event_value'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Event value'),
      '#description' => $this
        ->t('This can be any string.'),
      '#maxlength' => 64,
      '#size' => 64,
      '#weight' => '0',
      '#required' => TRUE,
    ];
    $form['is_syncing'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Is Syncing?'),
      '#description' => $this
        ->t('Events created with the is_syncing value set to true will not trigger automations.'),
    ];
    $form['occurred_at'] = [
      '#type' => 'datetime',
      '#description' => $this
        ->t('The date and time the event occurred. Defaults to the time when the form loaded.'),
      '#title' => $this
        ->t('Occurred at'),
      '#size' => 20,
      '#date_time_format' => 'H:i',
      '#default_value' => DrupalDateTime::createFromTimestamp(time()),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $email = $form_state
      ->getValue('email');
    $list = $form_state
      ->getValue('list');
    $properties = [
      'value' => $form_state
        ->getValue('event_value'),
      'another_property' => 'Always the same!',
    ];
    $event_name = $form_state
      ->getValue('event_name');
    $occurred_at = $form_state
      ->getValue('occurred_at')
      ->getTimestamp();
    $is_syncing = $form_state
      ->getValue('is_syncing');
    $result = mailchimp_events_add_member_event($list, $email, $event_name, $properties, $is_syncing, $occurred_at);
    $debug = $this
      ->t("Called function: mailchimp_events_add_member_event(%list, %email, %event_name, %properties, %is_syncing, %occurred_at).", [
      '%list' => $list,
      '%email' => $email,
      '%event_name' => $event_name,
      '%properties' => print_r($properties, TRUE),
      '%is_syncing' => $is_syncing,
      '%occurred_at' => $occurred_at,
    ]);
    if ($result !== FALSE) {
      $this
        ->messenger()
        ->addStatus($debug);
    }
    else {
      $this
        ->messenger()
        ->addError($debug);
      $this
        ->messenger()
        ->addError($this
        ->t('No results returned. Check the <a href=":watchdog">logs for Mailchimp</a>', [
        ':watchdog' => Url::fromRoute('dblog.overview')
          ->toString(),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddMailchimpEvent::buildForm public function Form constructor. Overrides FormInterface::buildForm
AddMailchimpEvent::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AddMailchimpEvent::submitForm public function Form submission handler. Overrides FormInterface::submitForm
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.