You are here

invite_by_email.module in Invite 8

Same filename and directory in other branches
  1. 7.4 modules/invite_by_email/invite_by_email.module

File

modules/invite_by_email/invite_by_email.module
View source
<?php

/**
 * @file
 * Contains invite_by_email.module.
 */
use Drupal\invite\InviteConstants;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function invite_by_email_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'invite_type_add_form' || $form_id == 'invite_type_edit_form') {
    $invite_type = $form_state
      ->getFormObject()
      ->getEntity();
    $data = unserialize($invite_type
      ->getData());
    $invite_by_email_settings = !empty($data) ? $data : [
      'use_default' => TRUE,
      'subject_editable' => '',
      'html_email' => '',
      'mail_subject' => '',
      'mail_template' => '',
    ];
    $form['invite_by_email'] = [
      '#type' => 'fieldset',
      '#title' => t('Invite by e-mail message settings'),
      '#tree' => TRUE,
      '#attributes' => [
        'class' => [
          'invite-by-email-wrapper',
        ],
      ],
      '#states' => [
        'invisible' => [
          ':input[name="send_method[invite_by_email]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['invite_by_email']['use_default'] = [
      '#type' => 'checkbox',
      '#title' => t('Use default settings'),
      '#default_value' => $invite_by_email_settings['use_default'],
      '#states' => [
        'invisible' => [
          ':input[name="invite_by_email"]' => [
            '!value' => 'invite_by_email',
          ],
        ],
      ],
    ];
    $form['invite_by_email']['subject_editable'] = [
      '#type' => 'checkbox',
      '#title' => t('Editable subject'),
      '#description' => t('Choose whether users should be able to customize the subject.'),
      '#default_value' => $invite_by_email_settings['subject_editable'],
      '#states' => [
        'invisible' => [
          ':input[name="invite_by_email[use_default]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['invite_by_email']['html_email'] = [
      '#type' => 'checkbox',
      '#title' => t('HTML email'),
      '#description' => t('Send HTML emails. WARNING: This option has security implications! Give to trusted users only.'),
      '#default_value' => $invite_by_email_settings['html_email'],
      '#states' => [
        'invisible' => [
          ':input[name="invite_by_email[use_default]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['invite_by_email']['help'] = [
      '#type' => 'container',
      'help' => [
        '#markup' => t('You may update the default subject and body for all invites on the <a href="@fields_path">fields edit page</a>.', [
          '@fields_path' => '/admin/structure/invite/settings/fields',
        ]),
      ],
      '#states' => [
        'invisible' => [
          ':input[name="invite_by_email[use_default]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Put our submit in top of list to ensure, that values will be added to
    // 'data'.
    array_unshift($form['actions']['submit']['#submit'], 'invite_by_email_form_invite_type_form_submit');
  }
}

/**
 * Submit handler for invite types form.
 *
 * @param array $form
 *   The form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The formstate.
 */
function invite_by_email_form_invite_type_form_submit(array &$form, FormStateInterface $form_state) {
  $form_state
    ->setValue('data', $form_state
    ->getValue('invite_by_email'));
}

/**
 * Implements hook_theme().
 */
function invite_by_email_theme($existing, $type, $theme, $path) {
  return [
    'invite_by_email' => [
      'variables' => [
        'body' => NULL,
      ],
    ],
  ];
}

/**
 * Loads Invite using information stored in session.
 *
 * @return bool|Invite
 *   Return the Invite of FALSE otherwise.
 */
function invite_by_email_load_from_session() {
  if (isset($_SESSION[InviteConstants::INVITE_SESSION_CODE])) {

    /** @var \Drupal\invite\InviteInterface $invite */
    $invite = \Drupal::entityTypeManager()
      ->getStorage('invite')
      ->loadByProperties([
      'reg_code' => $_SESSION[InviteConstants::INVITE_SESSION_CODE],
    ]);
  }
  if (!empty($invite)) {
    return $invite;
  }
  else {
    return [];
  }
}

/**
 * Implements hook_form_FORMID_alter().
 */
function invite_by_email_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\invite\InviteInterface $invite_object */
  $invite = invite_by_email_load_from_session();
  $invite_object = current($invite);
  if ($invite_object) {
    $form['account']['mail']['#default_value'] = $invite_object->field_invite_email_address->value;
    $form['account']['mail']['#disabled'] = TRUE;
  }
}

Functions

Namesort descending Description
invite_by_email_form_alter Implements hook_form_alter().
invite_by_email_form_invite_type_form_submit Submit handler for invite types form.
invite_by_email_form_user_register_form_alter Implements hook_form_FORMID_alter().
invite_by_email_load_from_session Loads Invite using information stored in session.
invite_by_email_theme Implements hook_theme().