social_lms_integrator-1.0.0-beta4/modules/social_lms_integrator_application/social_lms_integrator_application.module

modules/social_lms_integrator_application/social_lms_integrator_application.module
<?php

/**
 * @file
 * Provides an application entity type.
 */

use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\views\ViewExecutable;
use Drupal\group\Entity\GroupInterface;

/**
 * Implements hook_theme().
 */
function social_lms_integrator_application_theme() {
  return [
    'application' => [
      'render element' => 'elements',
    ],
  ];
}

/**
 * Prepares variables for application templates.
 *
 * Default template: application.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the application information and any
 *     fields attached to the entity.
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_application(array &$variables) {
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // Add back to team applications button
  $attributes = array('class' => 'btn button button-primary btn-primary');
  $variables['button_team_applications'] = [
    '#title' => t('Team Applications'),
    '#type' => 'link',
    '#url' => Url::fromRoute('view.applications.page_applications'),
    '#attributes' => $attributes
  ];

}

/**
 * Implements hook_social_user_account_header_account_links_alter().
 *
 * Provides a glue layer between the new system and the deprecated hook
 * social_user_account_header_links to provide backwards compatibility.
 *
 * This should be removed once social_user_account_header_links is also removed.
 */
function social_lms_integrator_application_social_user_account_header_account_links_alter(array &$account_links, array $context) {

  $user = \Drupal::currentUser();

  // Check for permission
  if ($user->hasPermission('access application view')) {

    $account_links['team_applications'] = [
      '#type' => 'link',
      '#attributes' => [
        'title' => t('Team Applications'),
      ],
      '#url' => Url::fromRoute('view.applications.page_applications'),
      '#title' => t('Team Applications'),
      '#weight' => 1,
    ];

  }

}

/**
 * Implements helper function to filter iterations by group().
 */
function social_lms_integrator_application_get_filtered_iteration_options($group_field, $iteration_options) {

  $filter_allowed_iterations = [];

  if(isset($group_field) && isset($iteration_options)) {
     $gid = $group_field->entity->id();
     $group = \Drupal::entityTypeManager()->getStorage('group')->load($gid);

    if ($group instanceof GroupInterface) {
      $group_content = $group->getContent();
      foreach($group_content as $content) {
        if ($content->getEntity()->bundle() === 'iteration') {
          $entity_id = $content->getEntity()->id();
          $filter_allowed_iterations[$entity_id] = $entity_id;
        }  
      }	
    }
    
    foreach ($iteration_options as $key => $option) {
      if (is_int($key) && !array_key_exists($key, $filter_allowed_iterations)) {
        unset($iteration_options[$key]);
      }
    }

   return $iteration_options;


  }

  return FALSE;


}
/**
 * Implements hook_form_alter()
 */
function social_lms_integrator_application_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'application_edit_form') {
    // Per default the iteration is set to disabled to avoid manipulation
    $form['field_iteration']['widget']['#disabled'] = TRUE;
    $entity = $form_state->getFormObject()->getEntity();
    $current_state = $entity->field_state->value;
    $group_field = $entity->field_group;
    $iteration_options = $form['field_iteration']['widget']['#options'];
    $filtered_iteration_options = social_lms_integrator_application_get_filtered_iteration_options($group_field, $iteration_options);
    
    // Filter Iteration options
    if ($filtered_iteration_options) {
      $form['field_iteration']['widget']['#options'] = $filtered_iteration_options;
    }
    // Get visible on waitlist
    if ($current_state === 'waitlist') {
      $form['field_iteration']['widget']['#disabled'] = FALSE;
    }
  }
  
}

/**
 * Implements hook_views_pre_render().
 */
function social_lms_integrator_application_views_pre_render(ViewExecutable $view) {
  if (!empty($view->result)) {
    $entities = [
      'application',
    ];

    foreach ($view->result as $key => $result) {
      if (empty($result->_entity)) {
        continue;
      }

      if (!in_array($result->_entity->getEntityTypeId(), $entities)) {
        continue;
      }

      $access = \Drupal::entityTypeManager()
        ->getAccessControlHandler($result->_entity->getEntityTypeId())
        ->access($result->_entity, 'view', NULL, TRUE);

      if (!$access->isAllowed()) {
        unset($view->result[$key]);
      }
    }
  }
}

/**
 * Implements hook_user_delete()
 */
function social_lms_integrator_application_user_delete($account) {
  // We want to make sure if an user gets removed
  // we also want to remove it's application
  $applications = \Drupal::entityTypeManager()->getStorage('application')->loadByProperties(['uid' => $account->id()]);
  if (isset($applications) && !empty($applications)) {
    foreach($applications as $application) {
      // Delete any application linked to
      // the deleted user
      $application->delete();
    }
  }
}





Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc