ercore-8.x-1.20/ercore.module
ercore.module
<?php
/**
* @file
* Hook implementations for the ERCore module.
*/
use Drupal\ercore\ErcoreStartDate;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Description.
*
* @param array $roles
* User roles associated with an account.
*
* @return bool
* Return a boolean if the user account belongs to an ERCore admin account.
*/
function ercore_is_admin(array $roles) {
$admin_roles = array(
'administrator',
'administrative staff',
'site manager',
);
if (!in_array($roles, $admin_roles)) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_theme_registry_alter().
*/
function ercore_theme_registry_alter(&$theme_registry) {
$modulepath = drupal_get_path('module', 'ercore');
// Find all .tpl.php files in this module's folder recursively.
$template_file_objects = drupal_find_theme_templates($theme_registry, '.html.twig', $modulepath);
// Iterate through all found template file objects.
foreach ($template_file_objects as $key => $template_file_object) {
$exploded_type = explode('--', $template_file_object['template']);
$type = str_replace('-', '_', $exploded_type[0]);
if (isset($theme_registry[$type]['preprocess functions'])) {
// Use field type as base value.
$functions = $theme_registry[$type]['preprocess functions'];
// If the template has not already been overridden by a theme.
if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
// Alter the theme path and template elements.
$theme_registry[$key] = $template_file_object;
$theme_registry[$key]['theme path'] = $modulepath;
$theme_registry[$key]['type'] = 'base_theme_engine';
$theme_registry[$key]['preprocess functions'] = $functions;
}
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function ercore_theme_suggestions_field_alter(&$suggestions, $variables) {
$element = $variables['element'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#view_mode'];
}
/**
* Implements hook_page_attachments().
*/
function ercore_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'ercore/ercore-style';
}
/**
* Get filter dates.
*
* @return array
* Array of date objects.
*/
function ercore_get_filter_dates() {
$dates = \Drupal::service('user.private_tempstore')
->get('ercore_core');
return [
'start' => $dates->get('ercore_value_start'),
'end' => $dates->get('ercore_value_end'),
];
}
/**
* Get filter dates.
*
* @return array
* Array of date objects.
*/
function ercore_get_project_filter_dates() {
return [
'start' => ErcoreStartDate::startUnix(),
'end' => ErcoreStartDate::endUnix(),
];
}
/**
* Gets Institution names for custom token.
*
* @param array $uids
* Receives user IDs from token, returns formatted text.
*
* @return string
* Returns string of institution names.
*/
function ercore_token_get_institutions(array $uids) {
$text = '';
foreach ($uids as $uid) {
$user = User::loadMultiple([$uid['target_id']]);
$nid = $user[$uid['target_id']]->get('field_ercore_user_partic_inst')
->getValue()[0]['target_id'];
if (!empty($nid)) {
$node = Node::loadMultiple([$nid]);
if (!empty($node)) {
$title = $node[$nid]->getTitle();
if (empty($text)) {
$text = $title;
}
else {
$text .= ', ' . $title;
}
}
}
}
return $text;
}
/**
* Format addresses for export.
*
* @param int $id
* Receives user IDs from token, returns formatted text.
*
* @return string
* Returns string of institution names.
*/
function ercore_token_format_address($id) {
$data = [];
$para = Paragraph::load($id);
$add1 = $para->get('field_ercore_loc_add1')->value;
if (!empty($add1)) {
$data[] = $add1;
}
$add2 = $para->get('field_ercore_loc_add2')->value;
if (!empty($add12)) {
$data[] = $add2;
}
$city = $para->get('field_ercore_loc_city')->value;
$city_state = '';
if (!empty($city)) {
$city_state = $city;
}
$state = $para->get('field_ercore_loc_state')->value;
if (!empty($state)) {
if (!empty($city_state)) {
$city_state .= ', ' . $state;
}
else {
$city_state .= $state;
}
}
$postal = $para->get('field_ercore_loc_postal')->value;
if (!empty($postal)) {
if (!empty($city_state)) {
$city_state .= ' ' . $postal;
}
else {
$city_state .= $postal;
}
}
$data[] = $city_state;
return implode("\n", $data);
}
function ercore_token_info_alter(&$info){
$info['tokens']['ercore_token_group']['ercore_related_institutions_token'] = array(
'name' => t('ercore: ercore_related_institutions_token TARGET'),
);
$info['tokens']['ercore_token_group']['ercore_address_token'] = array(
'name' => t('ercore: ercore_address_token TARGET'),
);
}
