View source
<?php
namespace Drupal\campaignmonitor\Plugin\Block;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\campaignmonitor\CampaignMonitorManager;
use Drupal\campaignmonitor\CampaignMonitorSubscriptionManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Cache\Cache;
class CampaignMonitorSubscribeBlock extends BlockBase implements BlockPluginInterface, ContainerFactoryPluginInterface {
protected $campaignMonitorManager;
protected $campaignMonitorSubscriptionManager;
protected $createSendManager;
protected $formBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CampaignMonitorManager $campaignmonitor_manager, CampaignMonitorSubscriptionManager $campaignmonitor_subscription_manager, FormBuilderInterface $form_builder) {
$this->campaignMonitorManager = $campaignmonitor_manager;
$this->createSendManager = $this->campaignMonitorManager
->createClientObj();
$this->campaignMonitorSubscriptionManager = $campaignmonitor_subscription_manager;
$this->formBuilder = $form_builder;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('campaignmonitor.manager'), $container
->get('campaignmonitor.subscription_manager'), $container
->get('form_builder'));
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$form += $this->campaignMonitorSubscriptionManager
->subscribeSettingsForm($config);
return $form;
}
public function blockValidate($form, FormStateInterface $form_state) {
$subscribe_option = $form_state
->getValue([
'campaignmonitor',
'list',
]);
if ($subscribe_option == 'single') {
$list_id = $form_state
->getValue([
'campaignmonitor',
'list_id',
]);
if (empty($list_id)) {
$form_state
->setErrorByName('settings[campaignmonitor][list_id]', 'List Selection required');
}
}
if ($subscribe_option == 'segments') {
$segment_list_id = $form_state
->getValue([
'campaignmonitor',
'segment_list_id',
]);
if (empty($segment_list_id)) {
$form_state
->setErrorByName('settings[campaignmonitor][segment_list_id]', 'List Selection required');
}
}
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['list'] = $form_state
->getValue([
'lists',
'list',
]);
$this->configuration['list_id'] = $form_state
->getValue([
'lists',
'list_id',
]);
$this->configuration['list_id_text'] = $form_state
->getValue([
'lists',
'list_id_text',
]);
$this->configuration['segment_list_id'] = $form_state
->getValue([
'lists',
'segment_list_id',
]);
$this->configuration['custom_fields_visible'] = $form_state
->getValue([
'custom_fields',
'custom_fields_visible',
]);
}
public function build() {
$config = $this
->getConfiguration();
$content = $this->formBuilder
->getForm('\\Drupal\\campaignmonitor\\Form\\CampaignMonitorSubscribeForm', $config);
return $content;
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'user',
]);
}
}