<?php

/**
 * @file
 * Hooks and customizations for the plan module.
 */

use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function plan_help($route_name, RouteMatchInterface $route_match) {
  $output = '';

  // Main module help for the plan module.
  if ($route_name == 'help.page.plan') {
    $output = '';
    $output .= '<h3>' . t('About') . '</h3>';
    $output .= '<p>' . t('Provides plan entity') . '</p>';
  }

  return $output;
}

/**
 * Implements hook_theme().
 */
function plan_theme() {
  return [
    'plan' => [
      'render element' => 'elements',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function plan_theme_suggestions_plan(array $variables) {
  $suggestions = [];
  $plan = $variables['elements']['#plan'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  $suggestions[] = 'plan__' . $sanitized_view_mode;
  $suggestions[] = 'plan__' . $plan->bundle();
  $suggestions[] = 'plan__' . $plan->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'plan__' . $plan->id();
  $suggestions[] = 'plan__' . $plan->id() . '__' . $sanitized_view_mode;

  return $suggestions;
}

/**
 * Prepares variables for plan templates.
 *
 * Default template: plan.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the plan information and any
 *     fields attached to the plan. Properties used:
 *     - #plan: A \Drupal\plan\Entity\Plan object. The plan entity.
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_plan(array &$variables) {
  $variables['plan'] = $variables['elements']['#plan'] ?? NULL;
  // Helpful $content variable for templates.
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}
