<?php

/**
 * @file
 * Functions to support theming.
 */

use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\mimemail\Utility\MimeMailFormatHelper;

/**
 * Implements hook_preprocess_image_widget().
 */
function diatron_preprocess_image_widget(array &$variables) {
  $data = &$variables['data'];

  // This prevents image widget templates from rendering preview container HTML
  // to users that do not have permission to access these previews.
  // @todo revisit in https://drupal.org/node/953034
  // @todo revisit in https://drupal.org/node/3114318
  if (isset($data['preview']['#access']) && $data['preview']['#access'] === FALSE) {
    unset($data['preview']);
  }
}

/**
 * Implements hook_preprocess_html().
 */
function diatron_preprocess_html(&$variables) {
  $route_name = \Drupal::routeMatch()->getRouteName();

  if ($route_name === 'entity.user.canonical') {
    $variables['attributes']['class'][] = 'user-view';
  }

  if ($route_name === 'entity.user.edit_form') {
    $variables['attributes']['class'][] = 'user-edit';
  }
}

/**
 * Implements hook_preprocess_mimemail_message().
 */
function diatron_preprocess_mimemail_message(&$variables) {
  $logo_attachment = diatron_custom_hooks_get_mail_logo_attachment();
  $variables['logo_cid'] = $logo_attachment['cid'] ?? NULL;
}

/**
 * Implements hook_preprocess_page().
 */
function diatron_preprocess_page_title(&$vars) {
  // Add the link that should be next to the title.
  $route_match = \Drupal::routeMatch();
  if ($route_match->getRouteName() == 'entity.user.edit_form' && $route_match->getParameter('user')->id() == \Drupal::currentUser()->id()) {
    $vars['title_button'] = Link::createFromRoute(t('Log out'), 'user.logout', [], ['attributes' => ['class' => ['button', 'diatron-title-button']]])->toString();
  }
  if ($route_match->getRouteName() == 'entity.user.canonical' && $route_match->getParameter('user')->id() == \Drupal::currentUser()->id()) {
    $vars['title_button'] = Link::createFromRoute(t('Add or remove instruments'), 'view.instrument_selector.page_1', [], ['attributes' => ['class' => ['button', 'diatron-title-button']]])->toString();
  }
  if ($route_match->getRouteName() == 'view.instrument_selector.page_1') {
    $vars['title_text'] = t('Please select the instrument(s) you are working with.');
  }
}
