Monday, April 15, 2024

Maviven .module

 

<?php


/**

 * @file

 * Maiven Sendgrid module file.

 */


use Drupal\Component\Plugin\Exception\PluginException;

use Drupal\Core\Entity\EntityInterface;

use Symfony\Component\HttpFoundation\RedirectResponse;

use Drupal\Core\Routing\RouteMatchInterface;

use Drupal\group\Entity\GroupInterface;

use Drupal\group\Entity\GroupMembership;

use Drupal\group\Entity\GroupRelationship;

use Drupal\user\Entity\User;

use Drupal\user\UserInterface;

use Drupal\Core\Form\FormStateInterface;

/**

 * Implements hook_mail_alter().

 */

function maiven_sendgrid_mail_alter(&$message) {

  /** @var \Drupal\maiven_sendgrid\SendgridMessageManager $sendgrid_message_manager */

  $sendgrid_message_manager = \Drupal::service('plugin.manager.maiven_sendgrid_message');

  try {

    $message_id = $message['id'];

    /** @var \Drupal\maiven_sendgrid\Plugin\SendgridMessage\SendgridMessageInterface $sendgrid_message */

    $sendgrid_message = $sendgrid_message_manager->createInstance($message_id);

    $sendgrid_message->process($message);

  }

  catch (PluginException $e) {

    \Drupal::logger('maiven_sendgrid')->critical(t('A Sendgrid Message has not been configured for @message_id', ['@message_id' => $message_id]));

  }

}


/**

 * Implements hook_sendgrid_integration_alter().

 */

function maiven_sendgrid_sendgrid_integration_alter(&$sendgrid_params, &$message) {

  /** @var \SendGrid\Mail\Mail $sendgrid_message */

  $sendgrid_message = $sendgrid_params['message'];

  // For transactional messages, we bypass list management.

  // This is to ensure that the message is not suppressed by a list.

  // This must only be done for transactional messages!

  // If any non-transactional messages are sent, this should NOT be bypassed.

  $sendgrid_message->enableBypassListManagement();

}


/**

 * Implements hook_sendgrid_integration_alter().

 */

function maiven_sendgrid_node_insert(EntityInterface $entity) {

  $test = $entity;

  if ($entity->getType() == 'article') {

    $url = Url::fromRoute('entity.node.canonical', ['node' => 1]);

    $redirect = new RedirectResponse($url->toString());

    $redirect->send();

  }

}

/**

 * Implements hook_form_alter().

 */

function maiven_sendgrid_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Adding custom validation for the welcome page type field.

  if ($form_id == 'user_form') {

    $form['actions']['submit']['#submit'][] = 'maiven_sendgrid_user_edit_form_submit';

  }

}


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

  $current_user = \Drupal::currentUser();

  $current_user_uid = $current_user->id();

  $current_user_roles = $current_user->getRoles();

  $account = User::load($current_user_uid);

  $group_relationships = GroupRelationship::loadByEntity($account);

  $group_id = get_user_group_id($account);

  if (!in_array('webform_manager', $current_user_roles)) {

    $count = count($current_user_roles);

    if($count == '1')

    {

      $redirect = new RedirectResponse('/form/vea');

      $redirect->send();

    }

  }

  if (in_array('webform_manager', $current_user_roles) && !in_array('administrator', $current_user_roles)) {

    if(isset($group_id)) {

      $redirect = new RedirectResponse('/group/'. $group_id);

    }else{

      $redirect = new RedirectResponse('/group/1');

    } 

    $redirect->send();

  }

  return;

}


/**

* Implements hook_preprocess_login() for page.html.twig.

*/

function maiven_sendgrid_user_login($account) {

  // We want to redirect user on login.

  $current_user = \Drupal::currentUser();

  $current_user_uid = $current_user->id();

  $current_user_roles = $current_user->getRoles();

  $group_id = get_user_group_id($account);

  if (!in_array('webform_manager', $current_user_roles)) {

    $count = count($current_user_roles);

    if($count == '1')

    {

      $redirect = new RedirectResponse('/form/vea');

      $redirect->send();

    }

  }

  if (in_array('webform_manager', $current_user_roles)) {

    if(isset($group_id)) {

      $redirect = new RedirectResponse('/group/'. $group_id);

    }else{

      $redirect = new RedirectResponse('/group/1');

    } 

    $redirect->send();

  }

  return;

}


/**

 * Helper function to get the group ID associated with the user.

 *

 * @param \Drupal\user\UserInterface $account

 *   The user account object.

 *

 * @return int|null

 *   The group ID associated with the user, or null if not found.

 */

function get_user_group_id(UserInterface $account) {

  $group_memberships = GroupMembership::loadByUser($account);

  if (!empty($group_memberships)) {

    $group_membership = reset($group_memberships);

    $group = $group_membership->getGroup();

    if ($group instanceof GroupInterface) {

      return $group->id();

    }

  }

  return null;

}

No comments:

Post a Comment

If you have any problem please let me know.