<?php

use Drupal\views\ViewExecutable;
use Drupal\node\NodeInterface;

/**
 * Implements hook_theme().
 *
 * Register a module or theme's theme implementations.
 * The implementations declared by this hook specify how a particular render array is to be rendered as HTML.
 *
 * See: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme/8.2.x
 *
 * If you change this method, clear theme registry and routing table 'drush cc theme-registry' and 'drush cc router'.
 */
function ufc_theme($existing, $type, $theme, $path) {
  return [
    'ufc_recent_fights' => [
      'variables' => [
        'data' => [],
      ],
    ],
    'fighters_dcjs' => [
      'variables' => [
        'count' => 0,
      ],
    ],
    'fighters_dcjs_list' => [
      'variables' => [
        'fighters' => [],
      ],
    ],
    'fighter_for_dcjs' => [
      'variables' => [
        'node' => NULL,
        'personal_info' => [],
        'stats' => [],
      ],
    ],
  ];
}


function ufc_views_pre_view(ViewExecutable $view) {
  if ($view->id() !== 'fighter_fight_list' || $view->getDisplay()->display['id'] !== 'past_fights') {
    return;
  }
  $node = \Drupal::routeMatch()->getParameter('node');
  assert($node instanceof NodeInterface);
  $fighter_name = \Drupal::routeMatch()->getParameter('node')->getTitle();
  $filters = $view->display_handler->getOption('filters');
  $filters['combine']['value'] = $fighter_name;
  $view->display_handler->overrideOption('filters', $filters);
}

function ufc_preprocess_block(&$vars) {
  if ($vars['plugin_id'] !== 'views_block:fighter_fight_list-past_fights') {
    return;
  }
  $vars['#cache']['contexts'] = ['url.path', 'url.query_args', 'user.permissions'];
}
