[ 'render element' => 'children', // If no template name is defined here, it defaults to the name of the theme hook, ie. module-name-theme-hook.html.twig 'template' => 'ufc-fight', // Optionally define path to Twig template files. Defaults to the module's ./templates/ directory. 'path' => $path . '/templates', // Optionally define variables that will be passed to the Twig template and set default values for them. 'variables' => [ 'fighter_1' => [], 'fighter_2' => [], ], ], ]; } /** * Implements hook_entity_presave(). */ function ufc_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { return; $bundle = $entity->bundle(); if ($bundle === 'fight') { // Get fighter ids, pass to predictor. $f1_id = $entity->get('field_fighter_one')->getValue()[0]['target_id']; $f2_id = $entity->get('field_fighter_two')->getValue()[0]['target_id']; updatePrediction($f1_id, $f2_id, $entity); } } /** * Implements hook_cron(). */ function ufc_cron() { // Reset the file. // file_put_contents("./modules/custom/ufc/fights_output.txt", ""); // Run through predictions. // \Drupal::service("ufc.predictor")->updatePredictions(); // // Check for lines in file to see incorrect fights. // $file_check = file("./modules/custom/ufc/fights_output.txt"); // echo '
';
  // print_r($file_check);
  // echo '
'; // exit(); } /** * Updates the prediction field on save. * * @param int $f1 * @param int $f2 * @return void */ function updatePrediction($f1, $f2, EntityInterface $entity) { return; if (!$f1 || !$f2) { return; } $fighter_one = Node::load($f1); $fighter_two = Node::load($f2); $predictor = \Drupal::service('ufc.predictor'); $results = $predictor->calculate($fighter_one, $fighter_two); $prediction = ''; $fight_points = [ $results['fighter_one']['points'], $results['fighter_two']['points'], ]; rsort($fight_points); $score = ' (' . $fight_points[0] . '-' . $fight_points[1] . ')'; if ($results['fighter_one']['points'] > $results['fighter_two']['points']) { $prediction .= $results['fighter_one']['name']; $prediction_name = $results['fighter_one']['name']; } elseif ($results['fighter_one']['points'] < $results['fighter_two']['points']) { $prediction .= $results['fighter_two']['name']; $prediction_name = $results['fighter_two']['name']; } else { $prediction .= 'TIE:'; } $prediction .= $score; $entity->set('field_prediction', $prediction); // If there is a result, compare it to the prediction. $result = $entity->get('field_result')->getValue(); if (empty($result)) { return; } $result_name = getFighterNameById(reset($result[0])); /* * The code below will attempt to update fight * predictions until they are correct * * (EXPERIMENTAL) */ if ($result_name !== $prediction_name) { \Drupal::logger('ufc')->notice($entity->label() . " incorrect. Adjusting."); // Isolate advantages. foreach ($results as $fighter) { if ($fighter['name'] === $result_name) { $advantages = $fighter['advantages']; } if ($fighter['name'] === $prediction_name) { $disadvantages = $fighter['disadvantages']; } } $predictor->adjustWeights($advantages, $disadvantages); // updatePrediction($f1, $f2, $entity); } // Calculate Confidence. $skips = $results['skips']; $confidence_raw = (1 - ($skips / 17)) * 100; $percentage = round($confidence_raw, 2); $entity->set('field_accuracy', (float) $percentage); } function getFighterNameById($nid) { $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid); $name = $node->label(); return $name; } function ufc_views_pre_render(ViewExecutable $view) { foreach ($view->result as $row) { $html = "

This is some markup

"; $markup = Markup::create($html); $row->_entity->set('field_plaintext_field', $markup); } }