View source
<?php
namespace Drupal\simplenews\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\simplenews\Entity\Subscriber;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SimplenewsSubscriptionBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $formBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $formBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->formBuilder = $formBuilder;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('form_builder'));
}
public function defaultConfiguration() {
return array(
'newsletters' => array(),
'message' => t('Stay informed - subscribe to our newsletter.'),
'unique_id' => '',
);
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'subscribe to newsletters');
}
public function blockForm($form, FormStateInterface $form_state) {
$newsletters = simplenews_newsletter_get_visible();
foreach ($newsletters as $newsletter) {
$options[$newsletter
->id()] = $newsletter->name;
}
$form['newsletters'] = array(
'#type' => 'checkboxes',
'#title' => t('Newsletters'),
'#options' => $options,
'#required' => TRUE,
'#default_value' => $this->configuration['newsletters'],
);
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('Block message'),
'#size' => 60,
'#maxlength' => 255,
'#default_value' => $this->configuration['message'],
);
$form['unique_id'] = array(
'#type' => 'textfield',
'#title' => t('Unique ID'),
'#size' => 60,
'#maxlength' => 255,
'#description' => t('Each subscription block must have a unique form ID. If no value is provided, a random ID will be generated. Use this to have a predictable, short ID, e.g. to configure this form use a CAPTCHA.'),
'#default_value' => $this->configuration['unique_id'],
);
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['newsletters'] = array_filter($form_state
->getValue('newsletters'));
$this->configuration['message'] = $form_state
->getValue('message');
$this->configuration['unique_id'] = empty($form_state
->getValue('unique_id')) ? \Drupal::service('uuid')
->generate() : $form_state
->getValue('unique_id');
}
public function build() {
$form_object = $this->entityTypeManager
->getFormObject('simplenews_subscriber', 'block');
$form_object
->setUniqueId($this->configuration['unique_id']);
$form_object
->setNewsletterIds($this->configuration['newsletters']);
$form_object->message = $this->configuration['message'];
if ($user = \Drupal::currentUser()) {
if ($subscriber = simplenews_subscriber_load_by_uid($user
->id())) {
$form_object
->setEntity($subscriber);
}
else {
$form_object
->setEntity(Subscriber::create()
->fillFromAccount($user));
}
}
else {
$form_object
->setEntity(Subscriber::create());
}
return $this->formBuilder
->getForm($form_object);
}
}