You are here

public function SocialBulkGroupInvitation::buildForm in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::buildForm()
  2. 10.3.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::buildForm()
  3. 10.0.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::buildForm()
  4. 10.2.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::buildForm()

File

modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php, line 181

Class

SocialBulkGroupInvitation
Class SocialBulkGroupInvitation.

Namespace

Drupal\social_group_invite\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $form['#attributes']['class'][] = 'form--default';
  $group = $this->routeMatch
    ->getParameter('group');
  $params = [
    'user' => $this
      ->currentUser(),
    'group' => $this->routeMatch
      ->getParameter('group'),
  ];

  // Replace recipients field by select2.
  unset($form['email_address']);
  $form['users_fieldset'] = [
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#attributes' => [
      'class' => [
        'form-horizontal',
      ],
    ],
  ];
  $form['users_fieldset']['user'] = [
    '#title' => $this
      ->t('Find people by name or email address'),
    '#type' => 'select2',
    '#description' => $this
      ->t('You can enter or paste multiple entries separated by comma or semicolon'),
    '#multiple' => TRUE,
    '#tags' => TRUE,
    '#autocomplete' => TRUE,
    '#selection_handler' => 'social',
    '#target_type' => 'user',
    '#select2' => [
      'tags' => TRUE,
      'placeholder' => t('Jane Doe, johndoe@example.com'),
      'tokenSeparators' => [
        ',',
        ';',
      ],
      'autocomplete' => FALSE,
    ],
    '#required' => TRUE,
    '#validated' => TRUE,
  ];

  // Load plugin configuration.
  $group_plugin_collection = $this->pluginManager
    ->getInstalled($group
    ->getGroupType());
  $group_invite_config = $group_plugin_collection
    ->getConfiguration()['group_invitation'];

  // Get invite settings.
  $invite_settings = $this->configFactory
    ->get('social_group.settings')
    ->get('group_invite');

  // Set preview subject and message.
  $invitation_subject = $invite_settings['invite_subject'] ?? $group_invite_config['invitation_subject'];
  $invitation_body = $invite_settings['invite_message'] ?? $group_invite_config['invitation_body'];

  // Cleanup message body and replace any links on preview page.
  $invitation_body = $this->token
    ->replace($invitation_body, $params);
  $invitation_body = preg_replace('/href="([^"]*)"/', 'href="#"', $invitation_body);

  // Get default logo image and replace if it overridden with email settings.
  $theme_id = $this->configFactory
    ->get('system.theme')
    ->get('default');
  $logo = $this
    ->getRequest()
    ->getBaseUrl() . theme_get_setting('logo.url', $theme_id);
  $email_logo = theme_get_setting('email_logo', $theme_id);
  if (is_array($email_logo) && !empty($email_logo)) {
    $file = File::load(reset($email_logo));
    if ($file instanceof File) {
      $logo = file_create_url($file
        ->getFileUri());
    }
  }

  // Load event invite configuration.
  $invite_config = $this->configFactory
    ->get('social_group_invite.settings');
  $form['email_preview'] = [
    '#type' => 'fieldset',
    '#title' => [
      'text' => [
        '#markup' => t('Preview your email invite'),
      ],
      '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-preview-email',
      ],
    ],
  ];
  $form['email_preview']['preview'] = [
    '#theme' => 'invite_email_preview',
    '#logo' => $logo,
    '#subject' => $this->token
      ->replace($invitation_subject, $params),
    '#body' => $invitation_body,
    '#helper' => $this->token
      ->replace($invite_config
      ->get('invite_helper'), $params),
  ];
  $form['actions']['#type'] = 'actions';
  unset($form['submit']);
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Send your invite(s) by email'),
  ];
  return $form;
}