degov-8.x-2.0/modules/degov_simplenews/degov_simplenews.module

modules/degov_simplenews/degov_simplenews.module
<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */
function degov_simplenews_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'simplenews_confirm_removal') {
    $form['#submit'][] = '_degov_optout_submitform';
  }
  // Alter the Simplenews form block with an additional data protection checkbox.
  if (strstr($form_id, 'simplenews_subscriptions_block') !== FALSE) {
    $privacy_policy_pages = \Drupal::config('degov_simplenews.settings')
      ->get('privacy_policy');
    $newsletter_ids = $form_state->getFormObject()->getNewsletterIds();
    $is_subscribed = TRUE;
    // Add cache contexts by role.
    $form['#cache']['contexts'][] = 'user.roles';
    // Require privacy policy settings and newsletters to be configured.
    if (!empty($privacy_policy_pages) && $newsletter_ids) {
      // Check if the user is authenticated.
      if (\Drupal::currentUser()->isAuthenticated()) {
        $current_user_email = \Drupal::currentUser()->getEmail();
        /** @var \Drupal\simplenews\Subscription\SubscriptionManager $subscriptionManager */
        $subscriptionManager = \Drupal::service('simplenews.subscription_manager');
        foreach ($newsletter_ids as $newsletter_id) {
          // Check if the user is not subscribed to a newsletter.
          if (!$subscriptionManager->isSubscribed($current_user_email, $newsletter_id)) {
            $is_subscribed = FALSE;
            break;
          }
        }
      }
      else {
        $is_subscribed = FALSE;
      }
      // Show the required checkbox if the user is not yet subscribed.
      if (!$is_subscribed) {
        $language_id = \Drupal::languageManager()
          ->getCurrentLanguage()
          ->getId();
        $url = Drupal\Core\Url::fromRoute('entity.node.canonical',
          ['node' => $privacy_policy_pages[$language_id]],
          ['attributes' => ['target' => '_blank']]
        );
        $form['forename'] = [
          '#type' => 'textfield',
          '#title' => t('Forename'),
          '#required' => TRUE,
          '#weight' => 97,
        ];
        $form['surname'] = [
          '#type' => 'textfield',
          '#title' => t('Surname'),
          '#required' => TRUE,
          '#weight' => 98,
        ];
        $form['privacy_policy'] = [
          '#type' => 'checkbox',
          '#title' => t('I hereby agree that my personal data transmitted with the contact information may be saved and processed. I confirm to be at least 16 years of age or have made available authorisation from the custodian(s) permitting the use of the contact information and dissemination of the data. I have read the @privacy_policy. The right of withdrawal is known to me.', [
            '@privacy_policy' => \Drupal\Core\Link::fromTextAndUrl(t('privacy policy'), $url)
              ->toString(),
          ]),
          '#required' => TRUE,
          '#weight' => 99,
        ];
      }
    }
  }

  if (strstr($form_id, 'simplenews_subscriptions_block') !== FALSE) {
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview'
        && isset($form['actions'][$action]['#type'])
        && $form['actions'][$action]['#type'] === 'submit'
      ) {
        $form['actions'][$action]['#submit'][] = 'degov_simplenews_form_submit';
      }
    }
  }
}

/**
 * {@inheritdoc}
 */
function _degov_optout_submitform(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $config = \Drupal::config('simplenews.settings');
  if (!$path = $config->get('subscription.confirm_unsubscribe_page')) {
    $form_state->setRedirectUrl(\Drupal\Core\Url::fromRoute('<front>'));
  }
  else {
    $form_state->setRedirectUrl(\Drupal\Core\Url::fromUri("internal:/$path"));
  }
}

/**
 * Implements hook_locale_translation_projects_alter().
 */
function degov_simplenews_locale_translation_projects_alter(&$projects) {
  $projects['degov_simplenews'] = [
    'info' => [
      'interface translation project' => 'degov_simplenews',
      'interface translation server pattern' =>
        drupal_get_path('module', 'degov_simplenews') . '/translations/%language.po',
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function degov_simplenews_form_simplenews_subscriber_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'simplenews_subscriber_form') {
    $form['subscriber_forename'] = [
      '#type' => 'textfield',
      '#title' => t('Forename'),
      '#default_value' => 'keine Angabe',
      '#disabled' => 'disabled',
      '#weight' => 5,
    ];
    $form['subscriber_surname'] = [
      '#type' => 'textfield',
      '#title' => t('Surname'),
      '#default_value' => 'keine Angabe',
      '#disabled' => 'disabled',
      '#weight' => 6,
    ];
    $form['data_protection_regulations'] = [
      '#title' => t('Data protection regulations'),
      '#type' => 'fieldset',
      '#description' => t('The user has not yet accepted the data protection regulations.'),
      '#weight' => 15,
    ];

    $result = \Drupal::service('database')
      ->select('simplenews_subscriber', 'ss')
      ->fields('ss', ['forename', 'surname', 'created'])
      ->condition('ss.mail', $form['mail']['widget'][0]['value']['#default_value'], '=')
      ->execute()
      ->fetchAll();
    if (!empty($result)) {
      $subscriberData = array_pop($result);
      $form['subscriber_forename']['#default_value'] = $subscriberData->forename;
      $form['subscriber_surname']['#default_value'] = $subscriberData->surname;

      $dateTime = new DateTime();
      $dateTime->setTimestamp($subscriberData->created);
      $form['data_protection_regulations']['#description'] =
        t('Has accepted the data protection regulations at @date @time.', [
          '@date' => $dateTime->format('d.m.Y'),
          '@time' => $dateTime->format('H:i'),
        ]);
    }
  }
}

function degov_simplenews_form_submit($form, FormStateInterface $form_state) {
  /** @var \Drupal\user\Entity\User $user */
  $user = \Drupal::currentUser();
  /** @var \Drupal\Core\Database\Connection $database */
  $database = \Drupal::service('database');

  $subscriberData = [
    'mail' => $form_state->getValues()['mail'][0]['value'],
    'forename' => $form_state->getValues()['forename'],
    'surname' => $form_state->getValues()['surname'],
  ];

  /** @var \Drupal\degov_simplenews\Service\InsertNameService $insertName */
  $insertName = \Drupal::service('degov_simplenews.insert_name');
  $insertName->updateForeAndSurname($user, $subscriberData);

  $result = $database
    ->select('nrw_simplenews_subscription', 'nss')
    ->fields('nss', ['uid'])
    ->condition('nss.uid', \Drupal::currentUser()->id(), '=')
    ->execute()
    ->fetchCol();
  if (empty($result)) {
    $database
      ->insert('nrw_simplenews_subscription')
      ->fields([
        'uid' => \Drupal::currentUser()->id(),
      ])
      ->execute();
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc