Project Working
This is first
<?php
use Drupal\block\Entity\Block;
/**
* @file
* Functions to support theming in the hook theme.
*/
/**
* Implements hook_preprocess_HOOK() for html.html.twig.
*/
function hook_preprocess_html(&$variables) {
$variables['base_path'] = base_path();
$current_path = \Drupal::service('path.current')->getPath();
$internal_path = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
// Assign it to body class
$variables['attributes']['class'][] = str_replace("/", "", $internal_path);
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if ($hook == 'node_edit_form') {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$content_type = $node->bundle();
} else {
$current_path = \Drupal::service('path.current')->getPath();
$path_args = explode('/', $current_path);
$content_type = $path_args[3];
}
$suggestions[] = 'node_edit_form__' . $content_type;
}
}
/**
* Implements hook_preprocess_HOOK() for page.html.twig.
*/
function hook_preprocess_page(&$variables) {
// If page is front, add to the $variables values for twig template
if ($variables['is_front']) {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('langcode', $language);
$query->condition('type', ['home_page_slider','advertisement'], 'IN');
$nids = $query->execute();
$variables['home_page']['slides'] = array();
$variables['home_page']['scroller'] = array();
// get and set values from content type to variable
foreach ($nids as $nid) {
$node = \Drupal\node\Entity\Node::load($nid);
$translation = $node->getTranslation($language);
$type = $node->bundle();
if($type == 'home_page_slider') {
$text = $translation->body->value;
$img = $node->get('field_image')->getValue();
$file = Drupal\file\Entity\File::load($img[0]['target_id']);
$img_src = $file->createFileUrl();
$variables['home_page']['slides'][] = array(
'text' => $text,
'img_src' => $file->createFileUrl(),
);
}
else
{
$text = $translation->body->value;
$variables['home_page']['scroller'][] = array(
'text' => $text,
);
}
}
}
$current_path = \Drupal::service('path.current')->getPath();
$internal_path = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
if ($variables['is_front'] !== TRUE && $internal_path != "/awards" && $internal_path != '/organization-chart') {
$variables['#attached']['library'][] = 'hook/page-library';
}
}
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*/
function hook_preprocess_node(&$variables) {
}
/**
* Implements hook_preprocess_HOOK().
*
* Pass block region value to content so this can be used in
* MYTHEME_theme_suggestions_menu_alter.
*/
function hook_preprocess_block(&$variables) {
if (isset($variables['elements']['#id'])) {
$region = Block::load($variables['elements']['#id'])->getRegion();
$variables['content']['#attributes']['region'] = $region;
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Provide region based menu suggestions.
*/
function hook_theme_suggestions_menu_alter(&$suggestions, array $variables) {
if (isset($variables['attributes']['region'])) {
$suggestion = 'menu__' . $variables['menu_name'] . '__' . $variables['attributes']['region'];
$suggestion = str_replace('-', '_', $suggestion);
$suggestions[] = $suggestion;
}
}
No comments:
Post a Comment
If you have any problem please let me know.