<?php
/**
 * saar theme overrides and preprocess functions.
 */
/**
 * Sanitise input urls.
 */
function santise_val_url($url) {
	if ($url != '') {
		// Sanitise (remove all illegal characters) from url.
		$url = filter_var($url, FILTER_SANITIZE_URL);
		// Validate url.
		if (filter_var($url, FILTER_VALIDATE_URL) == TRUE) {
			$url = $url;
		} else {
			$url = '#';
		}
		return $url;
	}
}
/**
 * Html preprocess for saar theme.
 */
function saar_preprocess_html(&$variables) {
	$variables['base_path'] = base_path();
	$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
	$variables['container_width'] = theme_get_setting('container_width', 'saar');
	$variables['header_width'] = theme_get_setting('header_width', 'saar');
	$variables['main_width'] = theme_get_setting('main_width', 'saar');
	$variables['footer_width'] = theme_get_setting('footer_width', 'saar');
	$variables['highlight_author_comment'] = theme_get_setting('highlight_author_comment', 'saar');
	$variables['bootstrapicons'] = theme_get_setting('bootstrapicons', 'saar');
}
/**
 * page preprocess
 */
function saar_preprocess_page(array &$variables) {
	$config = \Drupal::config('system.site');
	$variables['site_name'] = $config->get('name');
	$variables['site_slogan'] = $config->get('slogan');
	$variables['copyright_text'] = theme_get_setting('copyright_text', 'saar');
	$variables['social_icons'] = theme_get_setting('social_icons', 'saar');
	$variables['facebook_url'] = santise_val_url(theme_get_setting('facebook_url', 'saar'));
	$variables['twitter_url'] = santise_val_url(theme_get_setting('twitter_url', 'saar'));
	$variables['instagram_url'] = santise_val_url(theme_get_setting('instagram_url', 'saar'));
	$variables['linkedin_url'] = santise_val_url(theme_get_setting('linkedin_url', 'saar'));
	$variables['youtube_url'] = santise_val_url(theme_get_setting('youtube_url', 'saar'));
	$variables['vk_url'] = santise_val_url(theme_get_setting('vk_url', 'saar'));
	$variables['vimeo_url'] = santise_val_url(theme_get_setting('vimeo_url', 'saar'));
	$variables['whatsapp_url'] = santise_val_url(theme_get_setting('whatsapp_url', 'saar'));
	$variables['github_url'] = santise_val_url(theme_get_setting('github_url', 'saar'));
	$variables['telegram_url'] = santise_val_url(theme_get_setting('telegram_url', 'saar'));
	$variables['demo_slider'] = theme_get_setting('demo_slider', 'saar');
	$variables['scrolltotop'] = theme_get_setting('scrolltotop', 'saar');
	$variables['copyright_text'] = theme_get_setting('copyright_text', 'saar');
}

/**
 * Node preprocess.
 */
/**
 * Add current page to breadcrumb.
 */
function saar_preprocess_breadcrumb(&$variables) {
	$request = \Drupal::request();
	$route_match = \Drupal::routeMatch();
	$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
	if (!empty($page_title)) {
		$variables['breadcrumb'][] = [
			'text' => $page_title,
		];
		// Add cache context based on url.
		$variables['#cache']['contexts'][] = 'url';
	}
}