You are here

function activity_send_email_form_user_form_alter in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  2. 8 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  3. 8.2 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  4. 8.3 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  5. 8.4 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  6. 8.5 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  7. 8.6 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  8. 8.7 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  9. 8.8 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  10. 10.0.x modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  11. 10.1.x modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()
  12. 10.2.x modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_form_user_form_alter()

Implements hook_form_FORM_ID_alter() for user_form().

File

modules/custom/activity_send/modules/activity_send_email/activity_send_email.module, line 116
Contains activity_basics.module..

Code

function activity_send_email_form_user_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\social_user\Entity\User $account */
  $account = $form_state
    ->getFormObject()
    ->getEntity();

  // Only expose these settings to existing users so it's not shown on the
  // user create form.
  if ($account
    ->isNew()) {
    return;
  }
  $form['email_notifications'] = [
    '#type' => 'fieldset',
    '#title' => [
      'text' => [
        '#markup' => t('Email notifications'),
      ],
      'icon' => [
        '#markup' => '<svg class="icon icon-expand_more"><use xlink:href="#icon-expand_more" /></svg>',
        '#allowed_tags' => [
          'svg',
          'use',
        ],
      ],
    ],
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => [
      'class' => [
        'form-horizontal',
        'form-email-notification',
      ],
    ],
  ];
  $form['email_notifications']['description'] = [
    '#type' => 'html_tag',
    '#tag' => 'p',
    '#value' => t('For each email notification below, you can choose to turn it off, receive it immediately or in a daily or weekly digest. Email notifications will only be sent when you are not active in the platform.'),
  ];
  $items = _activity_send_email_default_template_items();
  $email_message_templates = EmailActivityDestination::getSendEmailMessageTemplates();

  // Give other modules the chance to add their own email notifications or
  // change the title or order of the e-mail notifications on this form.
  // Copy templates so that they can't be altered (arrays are assigned by copy).
  $context = $email_message_templates;
  \Drupal::moduleHandler()
    ->alter('activity_send_email_notifications', $items, $context);

  // Sort a list of email frequencies by weight.
  $email_frequencies = sort_email_frequency_options();
  $notification_options = [];

  // Place the sorted data in an actual form option.
  foreach ($email_frequencies as $option) {
    $notification_options[$option['id']] = $option['name'];
  }
  $user_email_settings = EmailActivityDestination::getSendEmailUserSettings($account);
  foreach ($items as $item_id => $item) {

    // Don't render the fieldset when there are no templates.
    if (empty($item['templates'])) {
      continue;
    }
    $form['email_notifications'][$item_id] = [
      '#type' => 'fieldset',
      '#title' => [
        'text' => [
          '#markup' => $item['title'],
        ],
        'icon' => [
          '#markup' => '<svg class="icon icon-expand_more"><use xlink:href="#icon-expand_more" /></svg>',
          '#allowed_tags' => [
            'svg',
            'use',
          ],
        ],
      ],
      '#attributes' => [
        'class' => [
          'form-fieldset',
        ],
      ],
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#open' => TRUE,
    ];
    $mail_configs = Drupal::config('social_swiftmail.settings');
    $template_frequencies = $mail_configs
      ->get('template_frequencies') ?: [];
    foreach ($item['templates'] as $template) {
      $default_frequency = isset($template_frequencies[$template]) ? $template_frequencies[$template] : FREQUENCY_IMMEDIATELY;
      $form['email_notifications'][$item_id][$template] = [
        '#type' => 'select',
        '#title' => $email_message_templates[$template],
        '#options' => $notification_options,
        '#default_value' => isset($user_email_settings[$template]) ? $user_email_settings[$template] : $default_frequency,
      ];
    }
  }

  // Submit function to save send email settings.
  $form['actions']['submit']['#submit'][] = '_activity_send_email_form_user_form_submit';

  // Attach library.
  $form['#attached']['library'][] = 'activity_send_email/admin';
}