Skip to content

Commit

Permalink
Merge pull request #10 from iqual-ch/issue/US-1621_d10-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe von Bergen authored Nov 9, 2023
2 parents c72b3b3 + 77769e2 commit 38eb3a5
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 125 deletions.
62 changes: 34 additions & 28 deletions iq_whitepaper.module
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

/**
* @file
* Custom hooks and function for the iq_group module.
*/

use Drupal\iq_whitepaper\IqWhitepaperEvents;
use Drupal\iq_whitepaper\Event\IqWhitepaperEvent;
use Drupal\views\ViewExecutable;
use Drupal\iq_whitepaper\Form\WhitepaperForm;
use Drupal\user\Entity\User;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\group\Entity\Group;
use Drupal\group\Entity\GroupContent;
use Drupal\iq_whitepaper\Event\IqWhitepaperEvent;
use Drupal\iq_whitepaper\Form\WhitepaperForm;
use Drupal\iq_whitepaper\IqWhitepaperEvents;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\views\ViewExecutable;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
Expand All @@ -41,7 +42,7 @@ function iq_whitepaper_file_download($uri) {
->getValue();
$downloads = [];
if (!empty($existing_downloads)) {
foreach ($existing_downloads as $key => $val) {
foreach ($existing_downloads as $val) {
if ($val['target_id'] != $whitepaper->id()) {
$downloads[] = $val['target_id'];
}
Expand All @@ -53,36 +54,36 @@ function iq_whitepaper_file_download($uri) {

// User branches.
$selected_branches = $user->get('field_iq_group_branches')->getValue();
foreach ($selected_branches as $key => $value) {
$default_branches = array_merge($default_branches, [$value['target_id']]);
foreach ($selected_branches as $value) {
$default_branches = [...$default_branches, $value['target_id']];
}
// Whitepaper branches.
$whitepaper_branches = $whitepaper->get('field_iq_group_branches')->getValue();
foreach ($whitepaper_branches as $key => $value) {
$default_branches = array_merge($default_branches, [$value['target_id']]);
foreach ($whitepaper_branches as $value) {
$default_branches = [...$default_branches, $value['target_id']];
}

$user->set('field_iq_group_branches', $default_branches);

// User products.
$default_products = [];
$selected_products = $user->get('field_iq_group_products')->getValue();
foreach ($selected_products as $key => $value) {
$default_products = array_merge($default_products, [$value['target_id']]);
foreach ($selected_products as $value) {
$default_products = [...$default_products, $value['target_id']];
}

// Whitepaper products.
$whitepaper_products = $whitepaper->get('field_iq_group_products')->getValue();
foreach ($whitepaper_products as $key => $value) {
$default_products = array_merge($default_products, [$value['target_id']]);
foreach ($whitepaper_products as $value) {
$default_products = [...$default_products, $value['target_id']];
}
$user->set('field_iq_group_products', $default_products);

$downloads[] = $whitepaper->id();
$user->set('field_iq_group_downloads', $downloads);
$user->save();
$eventDispatcher = \Drupal::service('event_dispatcher');
$eventDispatcher->dispatch(IqWhitepaperEvents::USER_DOWNLOAD_WHITEPAPER, new IqWhitepaperEvent($user));
$eventDispatcher->dispatch(new IqWhitepaperEvent($user), IqWhitepaperEvents::USER_DOWNLOAD_WHITEPAPER);
}
}
}
Expand Down Expand Up @@ -124,7 +125,8 @@ function iq_whitepaper_form_alter(&$form, FormStateInterface $form_state, $form_
}
}

// You need for the edit form a different callback because maybe you have to delete or create GroupContent.
// You need for the edit form a different callback because maybe you have to
// delete or create GroupContent.
if (in_array($form_id, $settings['form_id_edit'])) {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
Expand All @@ -135,7 +137,7 @@ function iq_whitepaper_form_alter(&$form, FormStateInterface $form_state, $form_
}

/**
*
* Node field group add custom callback.
*/
function _node_field_group_add_custom_callback_group($form, FormStateInterface $form_state) {
$settings = _node_field_group_settings();
Expand Down Expand Up @@ -164,7 +166,10 @@ function _node_field_group_add_custom_callback_group($form, FormStateInterface $
}

/**
* This function is a bit more complex because you have to do some mathematical set operations.
* Node field group edit custom callback group.
*
* This function is a bit more complex because you have to do some mathematical
* set operations.
*/
function _node_field_group_edit_custom_callback_group($form, FormStateInterface $form_state) {
$settings = _node_field_group_settings();
Expand Down Expand Up @@ -241,7 +246,8 @@ function _node_field_group_edit_custom_callback_group($form, FormStateInterface
function iq_whitepaper_views_pre_render(ViewExecutable $view) {
if ($view->id() == 'whitepaper_block') {
$whitepaperForm = \Drupal::formBuilder()->getForm(WhitepaperForm::class);
// If there is a resource selected (currently whitepaper, later it may vary).
// If there is a resource selected
// (currently whitepaper, later it may vary).
if (\Drupal::currentUser()->isAnonymous()) {
if ($view->filter['nid']->value['value'] != NULL) {
unset($whitepaperForm['preferences']);
Expand Down Expand Up @@ -404,7 +410,7 @@ function iq_whitepaper_file_access(FileInterface $file, $operation, AccountInter
*/
function iq_whitepaper_form_iq_group_user_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$user = \Drupal::entityTypeManager()->getStorage('user')->load(\Drupal::currentUser()->id());
// Show him the link to the resource.
// Show the link to the resource.
$result = \Drupal::entityTypeManager()
->getStorage('group')
->loadMultiple();
Expand All @@ -413,7 +419,7 @@ function iq_whitepaper_form_iq_group_user_edit_form_alter(&$form, FormStateInter
* @var int $key
* @var \Drupal\group\Entity\Group $group
*/
foreach ($result as $key => $group) {
foreach ($result as $group) {
if ($group->id() != \Drupal::config('iq_group.settings')->get('general_group_id')) {
$options[$group->id()] = $group->label();
}
Expand All @@ -423,9 +429,9 @@ function iq_whitepaper_form_iq_group_user_edit_form_alter(&$form, FormStateInter
$selected_preferences = $user->get('field_iq_group_preferences')
->getValue();
$default_value = [];
foreach ($selected_preferences as $key => $value) {
foreach ($selected_preferences as $value) {
if ($value['target_id'] != \Drupal::config('iq_group.settings')->get('general_group_id')) {
$default_value = array_merge($default_value, [$value['target_id']]);
$default_value = [...$default_value, $value['target_id']];
}
}

Expand Down Expand Up @@ -453,8 +459,8 @@ function iq_whitepaper_form_iq_group_user_edit_form_alter(&$form, FormStateInter
$selected_branches = $user->get('field_iq_group_branches')
->getValue();
$default_branches = [];
foreach ($selected_branches as $key => $value) {
$default_branches = array_merge($default_branches, [$value['target_id']]);
foreach ($selected_branches as $value) {
$default_branches = [...$default_branches, $value['target_id']];
}
if (!empty($node) && $node->bundle() == 'iq_group_whitepaper') {
$form['branches'] = [
Expand Down
Loading

0 comments on commit 38eb3a5

Please sign in to comment.