<?php

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function drupal_cms_olivero_theme_suggestions_form_alter(array &$suggestions, array $variables): void {
  if (isset($variables['element']['#theme']) && is_array($variables['element']['#theme'])) {
    foreach ($variables['element']['#theme'] as $theme) {
      if (str_contains($theme, '_edit_')) {
        $suggestions[] = 'form__' . str_replace('_edit_', '_', $theme);
      }
      $suggestions[] = 'form__' . $theme;
    }
  }
}

/**
 * Implements hook_preprocess_views_view__search().
 */
function drupal_cms_olivero_preprocess_views_view__search(array &$variables) {
  $variables['#attached']['library'][] = 'drupal_cms_olivero/view-search';
}

/**
 * Implements hook_preprocess_node().
 */
function drupal_cms_olivero_preprocess_node(array &$variables): void {
  // Pass layout variable to template if content type is in full view
  // mode. This is then used in the template to create a BEM style CSS class to
  // control the layout.
  if ($variables['view_mode'] === 'full') {
    $variables['layout'] = 'content-narrow';
  }
}

/**
 * Implements hook_preprocess_node__event__full().
 */
function drupal_cms_olivero_preprocess_node__event__full(array &$variables): void {
  $variables['#attached']['library'][] = 'drupal_cms_olivero/event-full';
}

/**
 * Implements hook_preprocess_node__case_study__full().
 */
function drupal_cms_olivero_preprocess_node__case_study__full(array &$variables): void {
  $variables['#attached']['library'][] = 'drupal_cms_olivero/case-study-full';
}

/**
 * Implements hook_preprocess_node__project__full().
 */
function drupal_cms_olivero_preprocess_node__project__full(array &$variables): void {
  $variables['#attached']['library'][] = 'drupal_cms_olivero/project-full';
}

/**
 * Implements hook_preprocess_node__person__full().
 */
function drupal_cms_olivero_preprocess_node__person__full(array &$variables): void {
  $variables['#attached']['library'][] = 'drupal_cms_olivero/person-full';
}


/**
 * Implements hook_preprocess_block().
 */
function drupal_cms_olivero_preprocess_block(&$variables): void {
  $plugin_id = $variables['plugin_id'];
  // Allow particular blocks to extend the width of the column.
  $wide_content = [
    'field_tags',
    'field_featured_image',
    'views_block',
  ];
  // Very loose string contains substring in array type check.
  // If too loose, we can make the above array more specific.
  if (str_replace($wide_content, '', strtolower($plugin_id)) !== strtolower($plugin_id)) {
    $variables['attributes']['class'][] = 'wide-content';
  }

  // Visually hide the page title on home
  $is_front = \Drupal::service('path.matcher')->isFrontPage();
  if ($is_front && $plugin_id === 'page_title_block') {
    $variables['attributes']['class'][] = 'visually-hidden';
  }
}
