sports_league-8.x-1.x-dev/modules/sl_match_moments/sl_match_moments.module
modules/sl_match_moments/sl_match_moments.module
<?php
/**
* @file
* The SL match moments module.
*/
declare(strict_types=1);
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function sl_match_moments_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
// Entering new roster.
if ($entity_form['#entity_type'] == 'sl_match_moments') {
$parent_entity = $form_state->getFormObject()->getEntity();
// Define players.
$entity_form['field_sl_match_moments_player']['widget'][0]['target_id']['#type'] = 'select';
$team_to_pick = $entity_form['#parents'][0];
if (strstr($team_to_pick, 'field_sl_match_home')) {
$team_to_pick = 'field_sl_match_team_home';
}
else {
$team_to_pick = 'field_sl_match_team_away';
}
$slBaseHelper = \Drupal::service('sl_base.helper');
$team = $parent_entity->get($team_to_pick)->entity->id();
$players = $slBaseHelper->findTeamPlayers((int) $team);
$options[] = '';
foreach ($players as $player_id => $player) {
$options[$player_id] = $player['label'];
$numbers[$player_id] = $player['number'];
}
asort($options);
$entity_form['field_sl_match_moments_player']['widget'][0]['target_id']['#size'] = 1;
$entity_form['field_sl_match_moments_player']['widget'][0]['target_id']['#options'] = $options;
if (!empty($entity_form['field_sl_match_moments_player_in']['widget'][0])) {
$entity_form['field_sl_match_moments_player_in']['widget'][0]['target_id']['#type'] = 'select';
$entity_form['field_sl_match_moments_player_in']['widget'][0]['target_id']['#size'] = 1;
$entity_form['field_sl_match_moments_player_in']['widget'][0]['target_id']['#options'] = $options;
}
// Default _value.
if (!empty($entity_form['#entity']->id()) && !empty($entity_form['#entity']->field_sl_match_moments_player->entity)) {
$entity_form['field_sl_match_moments_player']['widget'][0]['target_id']['#default_value'] = $entity_form['#entity']->field_sl_match_moments_player->entity->id();
}
if (!empty($entity_form['field_sl_match_moments_player_in']['widget'][0]) && !empty($entity_form['#entity']->field_sl_match_moments_player_in->entity) && !empty($entity_form['#entity']->id())) {
$entity_form['field_sl_match_moments_player_in']['widget'][0]['target_id']['#default_value'] = $entity_form['#entity']->field_sl_match_moments_player_in->entity->id();
}
$entity_form['field_sl_match']['widget'][0]['target_id']['#default_value'] = $parent_entity->id();
$entity_form['field_sl_match']['widget'][0]['target_id']['#type'] = 'hidden';
}
}
