sports_league-8.x-1.x-dev/modules/sl_default_ui/sl_default_ui.module
modules/sl_default_ui/sl_default_ui.module
<?php
/**
* @file
* The SL default UI module.
*/
declare(strict_types=1);
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\views\Views;
/**
* Implements hook_theme_suggestions_alter().
*/
function sl_default_ui_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if ($hook == 'views_view_table' && $view = $variables['view']) {
if ($view->id() == 'sl_stats_players' && in_array($view->current_display, ['block_1', 'block_3', 'block_5'])) {
$suggestions = ['views-sl-stats-main'];
}
if ($view->id() == 'sl_stats_players' && in_array($view->current_display, ['block_2', 'block_4', 'block_6'])) {
$suggestions = ['views-sl-stats-secondary'];
}
}
}
/**
* Implements hook_theme().
*/
function sl_default_ui_theme($existing, $type, $theme, $path) {
return [
'views-sl-stats-main' => [
'variables' => [
'view' => NULL,
'rows' => NULL,
'title' => NULL,
'options' => NULL,
'theme_hook_original' => '',
'responsive' => NULL,
'sl_stats_footer' => NULL,
],
'render element' => 'elements',
'template' => 'sl_stats_main',
"preprocess functions" => [
"_sl_stats_table_preprocess",
"template_preprocess",
"template_preprocess_views_view_table",
"contextual_preprocess",
],
],
'views-sl-stats-secondary' => [
'variables' => [
'view' => NULL,
'rows' => NULL,
'title' => NULL,
'options' => NULL,
'theme_hook_original' => '',
'responsive' => NULL,
],
'render element' => 'elements',
'template' => 'sl_stats_secondary',
"preprocess functions" => [
"_sl_stats_secondary_preprocess",
"template_preprocess",
"template_preprocess_views_view_table",
"contextual_preprocess",
],
],
];
}
/**
* Preprocesses secondary stats.
*
* phpcs:disable Drupal.NamingConventions.ValidFunctionName.InvalidPrefix
*/
function _sl_stats_secondary_preprocess(&$variables) {
$rows = $variables['rows'];
/**
* @var \Drupal\views\ResultRow $row
*/
foreach ($rows as &$row) {
$row->_entity;
}
}
/**
* Preprocesses stats table().
*
* phpcs:disable Drupal.NamingConventions.ValidFunctionName.InvalidPrefix
*/
function _sl_stats_table_preprocess(&$variables) {
$view = $variables['view'];
// Builds footer total.
foreach ($view->field as $fid => $field) {
if (in_array($fid, ['field_sl_stats_red_cards', 'field_sl_stats_yellow_cards'])) {
continue;
}
if (isset($field->options['type']) && $field->options['type'] == 'number_integer') {
$footer[$fid] = 0;
}
}
foreach ($view->result as $rid => $row) {
foreach ($view->field as $fid => $field) {
$value = $field->getValue($row);
if (in_array($fid, ['field_sl_stats_red_cards', 'field_sl_stats_yellow_cards'])) {
continue;
}
if (is_numeric($value) && isset($footer[$fid])) {
$footer[$fid] += $value;
}
}
}
$footer['field_sl_administrative_title'] = t('Total');
$variables['sl_stats_footer'] = $footer;
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function sl_default_ui_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($display->getComponent('sl_stats_person') && $view = Views::getView('sl_stats_players')) {
$build['sl_stats_person'] = $view->buildRenderable('default', [$entity->id()]);
}
if ($display->getComponent('sl_match_home_rosters') && $view = Views::getView('sl_match_rosters')) {
$build['sl_match_home_rosters'] = [
'#title' => t('Home rosters'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_home_rosters',
];
$build['sl_match_home_rosters']['view'] = $view->buildRenderable('block_2', [$entity->id()]);
}
if ($display->getComponent('sl_match_home_subs') && $view = Views::getView('sl_match_rosters')) {
$build['sl_match_home_subs'] = [
'#title' => t('Home subs'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_home_subs',
];
$build['sl_match_home_subs']['view'] = $view->buildRenderable('block_4', [$entity->id()]);
}
if ($display->getComponent('sl_match_home_coach') && $view = Views::getView('sl_match_rosters')) {
$build['sl_match_home_coach'] = [
'#title' => t('Home coach'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_home_coach',
];
$build['sl_match_home_coach']['view'] = $view->buildRenderable('block_5', [$entity->id()]);
}
if ($display->getComponent('sl_match_home_moments') && $view = Views::getView('sl_match_moments')) {
$build['sl_match_home_moments'] = [
'#title' => t('Home moments'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_home_moments',
];
$build['sl_match_home_moments']['view'] = $view->buildRenderable('block_1', [$entity->id()]);
}
if ($display->getComponent('sl_match_away_rosters') && $view = Views::getView('sl_match_rosters')) {
$build['sl_match_away_rosters'] = [
'#title' => t('Away rosters'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_away_rosters',
];
$build['sl_match_away_rosters']['view'] = $view->buildRenderable('block_1', [$entity->id()]);
}
if ($display->getComponent('sl_match_away_subs') && $view = Views::getView('sl_match_rosters')) {
$build['sl_match_away_subs'] = [
'#title' => t('Away subs'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_away_subs',
];
$build['sl_match_away_subs']['view'] = $view->buildRenderable('block_3', [$entity->id()]);
}
if ($display->getComponent('sl_match_away_coach') && $view = Views::getView('sl_match_rosters')) {
$build['sl_match_away_coach'] = [
'#title' => t('Away coach'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_away_coach',
];
$build['sl_match_away_coach']['view'] = $view->buildRenderable('block_6', [$entity->id()]);
}
if ($display->getComponent('sl_match_away_moments') && $view = Views::getView('sl_match_moments')) {
$build['sl_match_away_moments'] = [
'#title' => t('Away moments'),
'#type' => 'fieldgroup',
'#id' => 'field_sl_match_away_moments',
];
$build['sl_match_away_moments']['view'] = $view->buildRenderable('block_2', [$entity->id()]);
}
}
