sports_league-8.x-1.x-dev/modules/sl_match/sl_match.module
modules/sl_match/sl_match.module
<?php
/**
* @file
* The SL match module.
*/
declare(strict_types=1);
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_presave().
*/
function sl_match_entity_presave(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'node' && in_array($entity->bundle(), ['sl_match'])) {
$entity->field_sl_teams = [];
$entity->field_sl_teams[] = ['target_id' => $entity->field_sl_match_team_home->target_id];
$entity->field_sl_teams[] = ['target_id' => $entity->field_sl_match_team_away->target_id];
if ($entity->title->value == '' || $entity->title->value == ' x ') {
$entity->setTitle($entity->field_sl_match_team_home->entity->title->value . ' x ' . $entity->field_sl_match_team_away->entity->title->value);
$entity->field_sl_administrative_title = $entity->title->value;
}
}
}
/**
* Implements hook_entity_extra_field_info().
*/
function sl_match_entity_extra_field_info() {
$extra = [];
$extra['node']['sl_match']['display']['sl_match_home_rosters'] = [
'label' => t('Home rosters'),
'description' => t('Home rosters'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['sl_match']['display']['sl_match_home_subs'] = [
'label' => t('Home subs'),
'description' => t('Home subs'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['sl_match']['display']['sl_match_home_coach'] = [
'label' => t('Home coach'),
'description' => t('Home coach'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['sl_match']['display']['sl_match_home_moments'] = [
'label' => t('Home moments'),
'description' => t('Home moments'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['sl_match']['display']['sl_match_away_rosters'] = [
'label' => t('Away rosters'),
'description' => t('Away rosters'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['sl_match']['display']['sl_match_away_subs'] = [
'label' => t('Away subs'),
'description' => t('Away subs'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['sl_match']['display']['sl_match_away_coach'] = [
'label' => t('Away coach'),
'description' => t('Away coach'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['sl_match']['display']['sl_match_away_moments'] = [
'label' => t('Away moments'),
'description' => t('Away moments'),
'weight' => 100,
'visible' => TRUE,
];
return $extra;
}
/**
* Implements hook_entity_update().
*/
function sl_match_entity_update($entity) {
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'sl_match') {
/** @var \Drupal\Core\Database\Connection $database */
$database = \Drupal::service('database');
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('sl_stats') && $database->schema()
->tableExists('queue')) {
/** @var Drupal\Core\Queue\QueueFactory $queue_factory */
$queue_factory = \Drupal::service('queue');
$queue = $queue_factory->get('sl_match_worker');
$item = new \stdClass();
$item->nid = $entity->id();
$queue_items = \Drupal::service('database')->select('queue')
->fields('queue', [
'item_id',
])
->condition('name', 'sl_match_worker')
->condition('data', serialize($item))->execute()->fetchAll();
if (!count($queue_items)) {
$queue->createItem($item);
}
}
}
}
