MaterioSubscriptionsBlockForm.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\materio_simplenews\Form;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\simplenews\Form\SubscriptionsFormBase;
  5. /**
  6. * Configure simplenews subscriptions of the logged user.
  7. */
  8. class MaterioSubscriptionsBlockForm extends SubscriptionsFormBase {
  9. protected $uniqueId;
  10. /**
  11. * A message to use as description for the block.
  12. *
  13. * @var string
  14. */
  15. public $message;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getFormId() {
  20. if (empty($this->uniqueId)) {
  21. throw new \Exception('Unique ID must be set with setUniqueId.');
  22. }
  23. return 'materio_simplenews_subscriptions_block_' . $this->uniqueId;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function setUniqueId($id) {
  29. $this->uniqueId = $id;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function form(array $form, FormStateInterface $form_state) {
  35. // Hide subscription widget if only one newsletter available.
  36. if (count($this->getNewsletters()) == 1) {
  37. $this->getSubscriptionWidget($form_state)->setHidden();
  38. }
  39. $form = parent::form($form, $form_state);
  40. // $form['message'] = array(
  41. // '#type' => 'item',
  42. // '#markup' => $this->message,
  43. // );
  44. unset($form['subscriptions']['widget']['#title']);
  45. unset($form['subscriptions']['widget']['#description']);
  46. unset($form['mail']['widget']['#title']);
  47. unset($form['mail']['widget']['#description']);
  48. unset($form['mail']['widget'][0]['#title']);
  49. unset($form['mail']['widget'][0]['#title_display']);
  50. unset($form['mail']['widget'][0]['#description']);
  51. $form['mail']['widget'][0]['value']['#placeholder'] = t('email');
  52. unset($form['mail']['widget'][0]['value']['#title']);
  53. unset($form['mail']['widget'][0]['value']['#description']);
  54. $mailvalue = $form['mail']['widget'][0];
  55. $test='test';
  56. return $form;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function actions(array $form, FormStateInterface $form_state) {
  62. $actions = parent::actions($form, $form_state);
  63. $actions[static::SUBMIT_UPDATE]['#value'] = $this->t('Update');
  64. return $actions;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function getSubmitMessage(FormStateInterface $form_state, $op, $confirm) {
  70. switch ($op) {
  71. case static::SUBMIT_UPDATE:
  72. return $this->t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state->getValue('mail')[0]['value']));
  73. case static::SUBMIT_SUBSCRIBE:
  74. if ($confirm) {
  75. return $this->t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.');
  76. }
  77. return $this->t('You have been subscribed.');
  78. case static::SUBMIT_UNSUBSCRIBE:
  79. if ($confirm) {
  80. return $this->t('You will receive a confirmation e-mail shortly containing further instructions on how to cancel your subscription.');
  81. }
  82. return $this->t('You have been unsubscribed.');
  83. }
  84. }
  85. }