class ListMailchimpEventsForMember in Mailchimp 2.x
A sample form for listing Mailchimp Events for a member.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\mailchimp_events_example\Form\ListMailchimpEventsForMember
Expanded class hierarchy of ListMailchimpEventsForMember
1 string reference to 'ListMailchimpEventsForMember'
- 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/ ListMailchimpEventsForMember.php, line 12
Namespace
Drupal\mailchimp_events_example\FormView source
class ListMailchimpEventsForMember extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'list_mailchimp_events_for_member';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description']['#markup'] = $this
->t('View a list of events for a specific audience member, directly pulled from Mailchimp.');
$mc_lists = mailchimp_get_lists();
$list_options = [];
if ($events = $form_state
->get('events')) {
$listing = [];
foreach ($events->events as $event) {
$properties = [];
if (isset($event->properties)) {
foreach ($event->properties as $key => $value) {
$properties[] = $key . ': ' . $value;
}
}
$listing[] = [
'#prefix' => isset($event->name) ? $event->name : '',
'#theme' => 'item_list',
'#items' => $properties,
];
}
$form['results'] = [
'#theme' => 'item_list',
'#items' => $listing,
];
}
foreach ($mc_lists as $key => $value) {
$list_options[$key] = $value->name;
}
$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 of the member to view events for.'),
'#weight' => '0',
];
$form['count'] = [
'#type' => 'number',
'#title' => $this
->t('Count'),
'#description' => $this
->t('The number of records to return. Default value is 10. Maximum value is 1000.'),
'#default_value' => 10,
'#max' => 1000,
];
$form['offset'] = [
'#type' => 'number',
'#title' => $this
->t('Offset'),
'#description' => $this
->t('Used for pagination, this it the number of records from a collection to skip. Default value is 0.'),
'#default_value' => 0,
];
$form['fields'] = [
'#type' => 'textfield',
'#title' => $this
->t('Fields'),
'#description' => $this
->t('A comma-separated list of fields to return. Reference parameters of sub-objects with dot notation. If left empty, events.name, events.properties, and events.occurred_at will be returned.'),
'#default_value' => 'events.name,events.properties,events.occurred_at',
];
$form['exclude_fields'] = [
'#type' => 'textfield',
'#title' => $this
->t('Exclude fields'),
'#description' => $this
->t('A comma-separated list of fields to exclude. Reference parameters of sub-objects with dot notation. The options available match the options in "Fields".'),
'#default_value' => '',
];
$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');
$count = $form_state
->getValue('count');
$offset = $form_state
->getValue('offset');
$fields = explode(',', $form_state
->getValue('fields'));
$exclude_fields = explode(',', $form_state
->getValue('exclude_fields'));
$events = mailchimp_events_list_member_events($list, $email, $count, $offset, $fields, $exclude_fields);
$form_state
->set('events', $events);
$form_state
->setRebuild();
$debug = $this
->t("Called function: mailchimp_events_list_member_events(%list, %email, %count, %offset, %fields, %exclude_fields).", [
'%list' => $list,
'%email' => $email,
'%count' => $count,
'%offset' => $offset,
'%fields' => print_r($fields, TRUE),
'%exclude_fields' => print_r($exclude_fields, TRUE),
]);
if ($events !== 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
ListMailchimpEventsForMember:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ListMailchimpEventsForMember:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ListMailchimpEventsForMember:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |