social_event_invite_flow-1.0.0-beta3/social_event_invite_flow.module
social_event_invite_flow.module
<?php
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\node\Entity\NodeType;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\social_event\EventEnrollmentInterface;
use Drupal\social_event_invite_flow\EventInviteSettingsInterface;
/**
* Implements hook_theme().
*/
function social_event_invite_flow_theme() {
return [
'social_event_invite_flow__actions' => [
'variables' => ['links' => NULL],
'template' => 'social-event-invite-flow--actions',
],
'social_event_invite_flow_guest_content' => [
'variables' => ['content' => NULL],
'template' => 'social-event-invite-flow--guests'
],
'page__event_guest_access' => [
// base_hook is important!
// Otherwise your template won't know what to render.
'base hook' => 'page',
],
'social_event_invite_flow_guest_access_content' => [
'variables' => [
'event_title' => NULL,
'event_managers' => NULL,
'event_start' => NULL,
'event_end' => NULL,
'content' => NULL,
'form' => NULL,
'event_image' => NULL,
]
],
];
}
/**
* Implements hook_preprocess_page().
*/
function social_event_invite_flow_preprocess_page__event_guest_access(&$variables) {
// Theme settings
$variables['logopath'] = '/' . \Drupal::theme()->getActiveTheme()->getLogo();
}
/**
* Implements template_preprocess_views_view().
*/
function social_event_invite_flow_preprocess_views_view(&$variables) {
if ($variables['view']->id() === 'event_manage_enrollment_invites') {
$node_id = \Drupal::routeMatch()->getParameter('node');
// Implement custom button to go back to the event.
$variables['more'] = [
'#title' => t('Back to event'),
'#type' => 'link',
'#url' => Url::fromRoute('entity.node.canonical', ['node' => (int) $node_id]),
'#attributes' => [
'class' => [
'btn',
'btn-default',
'btn-raised',
'waves-effect',
],
],
];
}
// We have to override the local actions block.
// and render our own block instance in the view for placement.
// hook_theme_registry_alter will ensure our hooks is invoked later.
// That is also why hook_menu_local_actions_alter won't work.
if ($variables['view']->id() === 'event_manage_enrollments') {
/** @var \Drupal\social_event_invite\SocialEventInviteAccessHelper $access */
$access = \Drupal::service('social_event_invite.access_helper');
$access = $access->eventFeatureAccess();
if (!$access instanceof AccessResultForbidden) {
// Add the roster-link block to the build-array.
/** @var \Drupal\social_event_invite_flow\Plugin\Block\SocialEventInviteFlowLocalActionsBlock $block */
$block = \Drupal::service('plugin.manager.block')
->createInstance('social_event_invite_flow_block');
if (NULL !== $block) {
$block->setContextValue('node', Node::load(\Drupal::routeMatch()->getParameter('node')));
$block_content = $block->build();
if (!empty($block_content)) {
$variables['header']['actions'] = $block_content;
}
}
}
}
}
/**
* Implements hook_theme_registry_alter().
*/
function social_event_invite_flow_theme_registry_alter(&$theme_registry) {
// Unfortunately the preprocess functions aren't ordered by module weight.
// Changing module weight doesn't work, also with dependency set to
// social_group this should be dealt with but isnt.
// So we enforce our preprocess after social_group.
if (!empty($theme_registry['views_view']['preprocess functions'])) {
$current_key = array_search('social_event_invite_flow_preprocess_views_view', $theme_registry['views_view']['preprocess functions'], FALSE);
unset($theme_registry['views_view']['preprocess functions'][$current_key]);
// Give it a new key all the way at the end.
$theme_registry['views_view']['preprocess functions'][] = 'social_event_invite_flow_preprocess_views_view';
}
}
/**
* Alter the visibility of blocks coming from the event invite flow module.
*/
function social_event_invite_flow_social_core_block_visibility_path() {
$blocks = [
'social_page_title_block' => [
// '*/all-enrollment-requests/confirm-decline/*',
'*/invite/simple',
'*/invite/upload',
// '*/invite/confirm',
// '*/event-invites',
// '*/all-enrollments/add-enrollees',
'*/all-enrollment-requests/convert-enrollment-form/*',
],
'views_block:managers-event_managers' => [
//'*/all-enrollment-requests/confirm-decline/*',
'*/invite/simple',
'*/invite/upload',
//'*/invite/confirm',
'*/all-enrollment-requests/convert-enrollment-form/*',
],
'views_block:event_enrollments-event_enrollments' => [
//'*/all-enrollment-requests/confirm-decline/*',
'*/invite/simple',
'*/invite/upload',
//'*/invite/confirm',
'*/all-enrollment-requests/convert-enrollment-form/*',
],
'shariffsharebuttons' => [
'*/invite/simple',
'*/invite/upload',
]
];
return $blocks;
}
function social_event_invite_flow_theme_suggestions_page_alter(array &$suggestions, array $variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
if ($route_name === 'social_event_invite_flow.event_guest_access') {
$suggestions[] = 'page__' . 'event_guest_access';
}
}
/**
* Override variables for the social page hero data.
*/
function social_event_invite_flow_preprocess_node(array &$variables) {
if (!empty($variables['event_enrollment'])) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['node'];
if ($node->bundle() === 'event') {
// Get our event settings
$event_invite_flow_service = \Drupal::service('social_event_invite_flow.invite_flow_service');
$event_invite_setting = $event_invite_flow_service->getEventInviteSettings($node->id());
// Don't go any further if empty
if (!$event_invite_setting) {
return;
}
// Don't go any forther if field not there
if (!$node->hasField('field_event_send_invite_by_user')) {
return;
}
// Don't go any further if field not true
if (!$node->field_event_send_invite_by_user->value) {
$variables['users_allowed_to_invite'] = FALSE;
}
else {
$access_token = $event_invite_setting->getAccessToken();
$event_url = Url::fromRoute('social_event_invite_flow.event_guest_access', ['node' => $node->id()]);
$link_options['absolute'] = TRUE;
$link_options['query'] = ['access_token' => $access_token];
$event_url->setOptions($link_options);
$url = Url::fromRoute('social_event_invite_flow.invite_simple', ['node' => $node->id()]);
if ($url->access(\Drupal::currentUser())) {
$variables['users_allowed_to_invite'] = TRUE;
$variables['invite_content'] = [
'#theme' => 'invite_to_event_by_user',
'#url' => $event_url,
'#link' => Link::fromTextAndUrl(t('Invite via email'), $url)
->toRenderable(),
];
}
}
}
}
}
/**
* Implements hook_form_alter().
*/
function social_event_invite_flow_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, ['node_event_form', 'node_event_edit_form'])) {
$event_invite_flow_service = \Drupal::service('social_event_invite_flow.invite_flow_service');
$node = $form_state->getFormObject()->getEntity();
if ($node->hasField('field_event_send_invite_by_user')) {
// Additional Submit handler
$form['actions']['submit']['#submit'][] = '_social_event_invite_flow_update';
}
$invite_flow_component = $form_state->get('form_display')->getComponent('invite_flow');
if (isset($invite_flow_component) && is_array($invite_flow_component) && !empty($invite_flow_component)) {
//$invite_flow_settings_form = \Drupal::formBuilder()->getForm('Drupal\social_event_invite_flow\Form\EventInviteSettingsForm');
$form['field_hide_enroll_button']['#states'] = [
'visible' => [
':input[name="field_content_visibility"]' => ['value' => 'public'],
':input[name="field_enroll_method"]' => ['value' => '1'],
],
'invisible' => [
':input[name="field_content_visibility"]' => ['value' => 'group'],
':input[name="field_content_visibility"]' => ['value' => 'community'],
]
];
$form['invite_flow'] = [
'#type' => 'container',
'#weight' => $invite_flow_component['weight'],
'#states' => [
'visible' => [
':input[name="field_content_visibility"]' => ['value' => 'public'],
':input[name="field_enroll_method"]' => ['value' => '1'],
],
'invisible' => [
':input[name="field_content_visibility"]' => ['value' => 'group'],
':input[name="field_content_visibility"]' => ['value' => 'community'],
]
]
];
$form['invite_flow']['event_invite_settings'] = [
'#type' => 'fieldset',
'#collapsed' => FALSE,
'#collapsible' => TRUE,
'#title' => t('Event Invite Settings')
];
if (!$event_invite_flow_service->isInviteFlowSettingsPresent()) {
// Get destination
$destination = \Drupal::destination()->getAsArray();
$form['invite_flow']['event_invite_settings']['set_invite_flow_settings']['link'] = [
'#type' => 'link',
'#title' => t('Please configure settings first'),
'#url' => Url::fromRoute('social_event_invite_flow.settings',[], ['query' => $destination]),
];
return $form;
}
// Build our flex box container
$form['invite_flow']['event_invite_settings']['invite_container'] = [
'#type' => 'container',
'#attributes' => ['class' => ['invite--flow']],
];
// Build our flex box left column
$form['invite_flow']['event_invite_settings']['invite_container']['left'] = [
'#type' => 'container'
];
// Build our flex box right column
$form['invite_flow']['event_invite_settings']['invite_container']['right'] = [
'#type' => 'container'
];
$default_lang = \Drupal::service('language_manager')->getDefaultLanguage();
$curr_user = \Drupal::currentUser()->id();
$user_object = \Drupal::entityTypeManager()->getStorage('user')->load($curr_user);
$pref_lang = $user_object->getPreferredLangcode();
$form['invite_flow']['event_invite_settings']['invite_container']['left']['invite_mode_existing_accounts'] = [
'#type' => 'radios',
'#title' => t('Invite mode for existing accounts'),
'#options' => $event_invite_flow_service->getInviteModeExistingAccountsOptions(),
'#required' => TRUE,
'#attributes' => [
'title' => t("Here you can choose whether to allow your users to share the invitation link with others."),
],
'#default_value' => $event_invite_flow_service->getInviteSettingsDefaultData()['invite_mode_existing_accounts'],
];
$selected_webform_existing_accounts_default = $event_invite_flow_service->getInviteSettingsDefaultData()['selected_webform_existing_accounts'];
$form['invite_flow']['event_invite_settings']['invite_container']['left']['selected_webform_existing_accounts'] = [
'#type' => 'select',
'#title' => t('Select webform'),
'#options' => $event_invite_flow_service->getWebformsExistingAccountsOptions(),
'#states' => [
'visible' => [
':input[name="invite_mode_existing_accounts"]' => [
'value' => 'webform_existing_accounts',
],
],
],
'#default_value' => isset($selected_webform_existing_accounts_default) ? $selected_webform_existing_accounts_default : ''
];
$form['invite_flow']['event_invite_settings']['invite_container']['right']['invite_mode_new_accounts'] = [
'#type' => 'radios',
'#title' => t('Invite mode for new accounts'),
'#options' => $event_invite_flow_service->getInviteModeNewAccountsOptions(),
'#required' => TRUE,
'#attributes' => [
'title' => t("Here you can choose whether to allow your users to share the invitation link with others."),
],
'#default_value' => $event_invite_flow_service->getInviteSettingsDefaultData()['invite_mode_new_accounts']
];
$selected_webform_new_accounts = $event_invite_flow_service->getInviteSettingsDefaultData()['selected_webform_new_accounts'];
$form['invite_flow']['event_invite_settings']['invite_container']['right']['selected_webform_new_accounts'] = [
'#type' => 'select',
'#title' => t('Select webform'),
'#options' => $event_invite_flow_service->getWebformsNewAccountsOptions(),
'#states' => [
'visible' => [
':input[name="invite_mode_new_accounts"]' => [
'value' => 'webform_new_accounts'
],
],
],
'#default_value' => isset($selected_webform_new_accounts) ? $selected_webform_new_accounts : ''
];
}
}
}
function social_event_invite_flow_form_event_an_enroll_action_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Get the user
$current_user = \Drupal::currentUser();
$node = \Drupal::routeMatch()->getParameter('node');
if (!$node instanceof NodeInterface) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($node);
}
$event_invite_flow_service = \Drupal::service('social_event_invite_flow.invite_flow_service');
$event_invite_setting = $event_invite_flow_service->getEventInviteSettings($node->id());
if ($event_invite_setting instanceof EventInviteSettingsInterface) {
if ($event_invite_setting->getInviteModeNewAccounts() === 'webform_new_accounts') {
$webform_id = $event_invite_setting->getSelectedWebformNewAccounts();
$attributes = [
'class' => [
'js-form-submit',
'form-submit',
'btn',
'btn-accent',
'btn-lg',
]
];
$event_url = Url::fromRoute('entity.webform.canonical', ['webform' => $webform_id]);
// Add query parameter event
$link_options['query'] = [
'event' => $node->id(),
'webform_redirect' => '/node/' . $node->id(),
];
$event_url->setOptions($link_options);
$form['enroll_wrapper']['event_enrollment']['#url'] = $event_url;
$form['enroll_wrapper']['event_enrollment']['#attributes'] = $attributes;
}
}
}
function social_event_invite_flow_form_enroll_action_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Get the user
$current_user = \Drupal::currentUser();
$node = \Drupal::routeMatch()->getParameter('node');
if (!$node instanceof NodeInterface) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($node);
}
$event_invite_flow_service = \Drupal::service('social_event_invite_flow.invite_flow_service');
$event_invite_setting = $event_invite_flow_service->getEventInviteSettings($node->id());
if ($event_invite_setting instanceof EventInviteSettingsInterface) {
if ($event_invite_setting->getInviteModeNewAccounts() === 'webform_existing_accounts') {
$webform_id = $event_invite_setting->getSelectedWebformExistingAccounts();
$attributes = [
'class' => [
'js-form-submit',
'form-submit',
'btn',
'btn-accent',
'btn-lg',
]
];
$event_url = Url::fromRoute('entity.webform.canonical', ['webform' => $webform_id]);
// Add query parameter event
$link_options['query'] = [
'event' => $node->id(),
'webform_redirect' => '/node/' . $node->id(),
];
$event_url->setOptions($link_options);
$form['enroll_wrapper']['event_enrollment']['#url'] = $event_url;
$form['enroll_wrapper']['event_enrollment']['#attributes'] = $attributes;
}
}
}
/**
* Custom sumbit handler for event
*/
function _social_event_invite_flow_update(array $form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
// Construct config entity fields
$event_invite_settings_fields = [
'invite_mode_existing_accounts' => $form_state->getValue('invite_mode_existing_accounts'),
'selected_webform_existing_accounts' => $form_state->getValue('selected_webform_existing_accounts'),
'invite_mode_new_accounts' => $form_state->getValue('invite_mode_new_accounts'),
'selected_webform_new_accounts' => $form_state->getValue('selected_webform_new_accounts'),
'enable_shareable_link' => 0,
];
// Get our event settings
$event_invite_flow_service = \Drupal::service('social_event_invite_flow.invite_flow_service');
$event_invite_setting = $event_invite_flow_service->getEventInviteSettings($node->id());
// Save Event Invite Setting when empty
if (!$event_invite_setting) {
$event_invite_flow_service->saveEventInviteSettings($node);
}
else {
$event_invite_flow_service->saveEventInviteSettings($node, $event_invite_settings_fields);
}
}
/**
* Implements hook_entity_extra_field_info().
*/
function social_event_invite_flow_entity_extra_field_info() {
$extra = [];
foreach (NodeType::loadMultiple() as $bundle_key => $bundle) {
if ($bundle_key === 'event') {
$extra['node'][$bundle_key]['form']['invite_flow'] = [
'label' => t('Invite Flow'),
'description' => t('Invite Flow Settings'),
'weight' => 99,
'visible' => TRUE,
];
}
}
return $extra;
}
/**
* Implements hook_module_implements_alter().
*/
function social_event_invite_flow_module_implements_alter(&$implementations, $hook) {
if ($hook == 'event_enrollment_insert') {
unset($implementations['social_event_invite']);
unset($implementations['social_event_an_enroll']);
}
}
/**
* Implements hook_entity_operation_alter().
*/
function social_event_invite_flow_entity_operation_alter(array &$operations, EntityInterface $entity) {
// Check access first.
if (!social_event_manager_or_organizer()) {
return;
}
// Get the node, so we can pass it as a parameter.
$node = \Drupal::routeMatch()->getParameter('node');
// Check if the entity type is one of event_enrollment and that we're on the
// correct view. Otherwise it would update all actions across the platform.
if ($entity->getEntityTypeId() === 'event_enrollment' && \Drupal::routeMatch()->getRouteName() === 'view.event_manage_enrollment_requests.page_manage_enrollment_requests') {
// Add the "Convert to Invite" option.
$operations['convert_invite']['title'] = t('Convert to invite');
$operations['convert_invite']['url'] = Url::fromRoute('social_event_invite_flow.convert_enroll_request', [
'node' => $node,
'event_enrollment' => $entity->id()
]);
/*
$operations['convert_invite']['attributes']= ['class' => ['use-ajax']];
$operations['convert_invite']['url'] = Url::fromRoute('social_event_invite_flow.convert_enrollment_request', [
'node' => $node,
'event_enrollment' => $entity->id(),
]);
*/
}
}
function social_event_invite_flow_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$view_ids = ['invite_flow_log'];
$view = $form_state->getStorage('view');
if ($form_id == 'views_exposed_form' && in_array($view['view']->id(), $view_ids)) {
$form['#attached']['library'][] = 'social_event_invite_flow/flow_design';
}
}
function social_event_invite_flow_event_enrollment_delete(EntityInterface $entity) {
// Get the user from the entity
$account = $entity->field_account;
// Get the node from the entity
$event = $entity->field_event;
if ($account) {
$user = \Drupal::entityTypeManager()->getStorage('user')->load($account->target_id);
$invitee_email = $user->getEmail();
if (!$invitee_email && isset($entity->field_email)) {
$invitee_email = $entity->field_email->value;
}
}
if ($event) {
$event_id = $event->target_id;
}
if ($invitee_email && $event_id) {
$conditions = [
'invitee_email' => $invitee_email,
'event' => $event_id
];
\Drupal::service('social_event_invite_flow.invite_flow_service')->deleteInviteLogRecord($conditions);
}
}
