class CampaignMonitorSubscribeForm in Campaign Monitor 8.2
Same name and namespace in other branches
- 8 src/Form/CampaignMonitorSubscribeForm.php \Drupal\campaignmonitor\Form\CampaignMonitorSubscribeForm
Subscribe to a campaignmonitor list.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\campaignmonitor\Form\CampaignMonitorSubscribeForm
Expanded class hierarchy of CampaignMonitorSubscribeForm
File
- src/
Form/ CampaignMonitorSubscribeForm.php, line 19
Namespace
Drupal\campaignmonitor\FormView source
class CampaignMonitorSubscribeForm extends FormBase {
/**
* The form id which set as class property so it can be overwritten as needed.
*
* @var string
*/
private $formId = 'campaignmonitor_subscribe_form';
/**
* The current user.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* The campaign monitor manager service.
*
* @var Drupal\campaignmonitor\CampaignMonitorManager
*/
protected $campaignMonitorManager;
/**
* The campaign monitor subscription manager service.
*
* @var Drupal\campaignmonitor\CampaignMonitorSubscriptionManager
*/
protected $campaignMonitorSubscriptionManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module manager service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Class constructor.
*/
public function __construct(CampaignMonitorManager $campaignmonitor_manager, CampaignMonitorSubscriptionManager $campaignmonitor_subscription_manager, AccountProxyInterface $account, EntityTypeManagerInterface $entity_type_manager, Token $token, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
$this->campaignMonitorManager = $campaignmonitor_manager;
$this->campaignMonitorSubscriptionManager = $campaignmonitor_subscription_manager;
$this->account = $account;
$this->entityTypeManager = $entity_type_manager;
$this->token = $token;
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static($container
->get('campaignmonitor.manager'), $container
->get('campaignmonitor.subscription_manager'), $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('token'), $container
->get('module_handler'), $container
->get('renderer'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return $this->formId;
}
/**
* {@inheritdoc}
*/
public function setFormId($formId) {
$this->formId = $formId;
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'campaignmonitor.subscribe',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $config = []) {
$email = '';
// Try to get the e-mail address from the user object.
if ($this->account
->id() != 0) {
$account = $this->entityTypeManager
->getStorage('user')
->load($this->account
->id());
$email = $account
->get('mail')
->getValue()[0]['value'];
}
// Use of key 'mail' here corresponds with key used in registration form.
$form['mail'] = [
'#type' => 'email',
'#title' => $this
->t('Email'),
'#required' => TRUE,
'#maxlength' => 200,
'#default_value' => $email,
];
switch ($config['list']) {
case 'single':
$form += $this
->singleSubscribeForm($config, $email);
$this
->setFormID($this->formId . '_single');
break;
default:
$form += $this
->userSelectSubscribeForm($form, $form_state, $config, $email);
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Subscribe'),
];
$form['config'] = [
'#type' => 'hidden',
'#value' => serialize($config),
];
$this->renderer
->addCacheableDependency($form, $config);
return $form;
}
/**
* Returns form with single subscribe option.
*/
protected function singleSubscribeForm($config, $email) {
return $this->campaignMonitorSubscriptionManager
->singleSubscribeForm($config, $email);
}
/**
* Return form as per user select subscribe form.
*/
protected function userSelectSubscribeForm(array $form, FormStateInterface $form_state, $config, $email) {
$form = [];
// Set options for the form.
$form = [
'#tree' => TRUE,
'#attributes' => [
'class' => [
'campaignmonitor-subscribe-form',
'campaignmonitor-subscribe-form-all-lists',
],
],
];
$lists = $this->campaignMonitorManager
->getLists();
$options = [];
foreach ($lists as $list_id => $list) {
if ($this->campaignMonitorManager
->isListEnabled($list_id)) {
$options[$list_id] = $list['name'];
}
}
$form['selection'] = [
'#type' => 'checkboxes',
'#options' => $options,
'#title' => $this
->t('Lists'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->campaignMonitorSubscriptionManager
->subscribeSubmitHandler($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CampaignMonitorSubscribeForm:: |
protected | property | The current user. | |
CampaignMonitorSubscribeForm:: |
protected | property | The campaign monitor manager service. | |
CampaignMonitorSubscribeForm:: |
protected | property | The campaign monitor subscription manager service. | |
CampaignMonitorSubscribeForm:: |
protected | property | The entity type manager. | |
CampaignMonitorSubscribeForm:: |
private | property | The form id which set as class property so it can be overwritten as needed. | |
CampaignMonitorSubscribeForm:: |
protected | property | The module manager service. | |
CampaignMonitorSubscribeForm:: |
protected | property | The renderer. | |
CampaignMonitorSubscribeForm:: |
protected | property | The current user. | |
CampaignMonitorSubscribeForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
CampaignMonitorSubscribeForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
CampaignMonitorSubscribeForm:: |
protected | function | ||
CampaignMonitorSubscribeForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CampaignMonitorSubscribeForm:: |
public | function | ||
CampaignMonitorSubscribeForm:: |
protected | function | Returns form with single subscribe option. | |
CampaignMonitorSubscribeForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
CampaignMonitorSubscribeForm:: |
protected | function | Return form as per user select subscribe form. | |
CampaignMonitorSubscribeForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
CampaignMonitorSubscribeForm:: |
public | function | Class constructor. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
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. | 1 |
FormBase:: |
private | function | Returns the service container. | |
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. Overrides UrlGeneratorTrait:: |
|
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. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
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. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
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. | 1 |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |