You are here

class CampaignMonitorSubscribeForm in Campaign Monitor 8.2

Same name and namespace in other branches
  1. 8 src/Form/CampaignMonitorSubscribeForm.php \Drupal\campaignmonitor\Form\CampaignMonitorSubscribeForm

Subscribe to a campaignmonitor list.

Hierarchy

Expanded class hierarchy of CampaignMonitorSubscribeForm

File

src/Form/CampaignMonitorSubscribeForm.php, line 19

Namespace

Drupal\campaignmonitor\Form
View 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

Namesort descending Modifiers Type Description Overrides
CampaignMonitorSubscribeForm::$account protected property The current user.
CampaignMonitorSubscribeForm::$campaignMonitorManager protected property The campaign monitor manager service.
CampaignMonitorSubscribeForm::$campaignMonitorSubscriptionManager protected property The campaign monitor subscription manager service.
CampaignMonitorSubscribeForm::$entityTypeManager protected property The entity type manager.
CampaignMonitorSubscribeForm::$formId private property The form id which set as class property so it can be overwritten as needed.
CampaignMonitorSubscribeForm::$moduleHandler protected property The module manager service.
CampaignMonitorSubscribeForm::$renderer protected property The renderer.
CampaignMonitorSubscribeForm::$token protected property The current user.
CampaignMonitorSubscribeForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
CampaignMonitorSubscribeForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
CampaignMonitorSubscribeForm::getEditableConfigNames protected function
CampaignMonitorSubscribeForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CampaignMonitorSubscribeForm::setFormId public function
CampaignMonitorSubscribeForm::singleSubscribeForm protected function Returns form with single subscribe option.
CampaignMonitorSubscribeForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
CampaignMonitorSubscribeForm::userSelectSubscribeForm protected function Return form as per user select subscribe form.
CampaignMonitorSubscribeForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
CampaignMonitorSubscribeForm::__construct public function Class constructor.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
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. 1
FormBase::container private function Returns the service container.
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. Overrides UrlGeneratorTrait::redirect
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.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.