<?php

/**
 * @file
 * Functions to support theming blocks in the Toc Master theme.
 */

use Drupal\block\Entity\Block;
use Drupal\Core\Template\Attribute;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Url;

/**
 * Preprocess for all menu blocks.
 */
function toc_emr_preprocess_block__block_content__footer_top_c(&$vars) {
  $vars["tpl_title"] = $vars["old_label"]["#title"];
  $vars["tpl_url"] = $vars["old_label"]["#url"]->toString();
  $vars["tpl_icon"] = $vars["icon_left"];
  $vars["tpl_mid"] = 'footer-top-c';
  $vars["tpl_collapse"] = false;
  $vars["tpl_links"] = [];
}

/**
 * Preprocess for blocks in footer top group.
 *
 * Any block (or block group) in this region gets equal cols.
 *
 * @see block--blockgroup--footer-top.html.twig
 */
function toc_emr_preprocess_block__blockgroup__footer_top(&$vars) {
  $blocks = \Drupal::entityTypeManager()
    ->getStorage('block')
    ->loadByProperties([
      'theme' => \Drupal::config('system.theme')->get('default'),
      'region' => 'footer_top',
    ]);
  $numb_cols = 12 / count($blocks);
  $vars['cols'] = "col-12 col-xl-$numb_cols";
}

/**
 * Preprocess for all SEO blocks.
 */
function toc_emr_preprocess_block__seo_block__full(&$vars) {
  $body_format = _tm_text_format([], 0, 'input', 'full_html');
  if (!empty($vars['elements']['content']['#block_content'])) {
    /* @var $entity Drupal\block_content\Entity\BlockContent  */
    $entity = $vars['elements']['content']['#block_content'];
    $title = _tm_misc_get_field_content($entity, 'info');
    $body = _tm_misc_get_field_content($entity, 'body', 'render', $body_format);
    $cta_list = _tm_misc_get_field_content($entity, 'field_cta_list', 'render', [], -1);
    $cta_link = _tm_misc_get_field_content($entity, 'field_cta_link');
    $less_text = _tm_misc_get_field_content($entity, 'field_less_text');
    $more_text = _tm_misc_get_field_content($entity, 'field_more_text');
    $cta_text = '';
    $cta_url = '';
    if (!empty($cta_link)) {
      $cta_link = _tm_misc_get_link_from_string($cta_link);
      $cta_text = $cta_link['text'];
      $cta_url = $cta_link['url'];
    }
    $vars['title'] = $title;
    $vars['content'] = $body;
    $vars['button_text'] = $cta_text;
    $vars['button_url'] = $cta_url;
    $vars['more_str'] = $more_text;
    $vars['less_str'] = $less_text;
    $vars['cat_list'] = $cta_list;
  }
}

/**
 * Implements hook_preprocess_journey_smart_booking_form().)
 */
function toc_emr_preprocess_journey_smart_booking_form(&$vars) {
  $vars['#attached']['library'][] = 'toc_emr/feature_block__journey_smart_booking_form';
}
