PageRenderTime 22ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/docroot/themes/custom/gavias_linoor/includes/functions.php

https://gitlab.com/kryber/wizionary
PHP | 151 lines | 135 code | 15 blank | 1 comment | 24 complexity | 1812306dd18beb1f7db54d1dbed63df8 MD5 | raw file
  1. <?php
  2. function gavias_linoor_render_block($key) {
  3. $block = \Drupal\block\Entity\Block::load($key);
  4. if($block){
  5. $block_content = \Drupal::entityTypeManager()
  6. ->getViewBuilder('block')
  7. ->view($block);
  8. return \Drupal::service('renderer')->render($block_content);
  9. }
  10. return '';
  11. }
  12. function gavias_linoor_makeid($length = 5){
  13. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  14. $randomString = '';
  15. for ($i = 0; $i < $length; $i++) {
  16. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  17. }
  18. return $randomString;
  19. }
  20. function scrape_insta_hash($tag) {
  21. $insta_source = file_get_contents('https://www.instagram.com/'.trim($tag)); // instagrame tag url
  22. $shards = explode('window._sharedData = ', $insta_source);
  23. $insta_json = explode(';</script>', $shards[1]);
  24. $insta_array = json_decode($insta_json[0], TRUE);
  25. return $insta_array; // this return a lot things print it and see what else you need
  26. }
  27. function gavias_linoor_base_url(){
  28. global $base_url;
  29. $theme_path = drupal_get_path('theme', 'gavias_linoor');
  30. return $base_url . '/' . $theme_path . '/';
  31. }
  32. global $gva_node_index;
  33. function gavias_linoor_preprocess_node(&$variables) {
  34. global $gva_node_index;
  35. $gva_node_index = $gva_node_index + 1;
  36. $variables['gva_node_index'] = $gva_node_index;
  37. $date = $variables['node']->getCreatedTime();
  38. $variables['date'] = t(date( 'F', $date)) . ' ' . t(date( 'j', $date)) . ', ' .t(date( 'Y', $date));
  39. if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
  40. unset($variables['content']['links']['comment']['#links']['comment-add']);
  41. }
  42. if ($variables['node']->getType() == 'article') {
  43. $node = $variables['node'];
  44. $variables['comment_count'] = $node->get('comment')->comment_count;
  45. $post_format = 'standard';
  46. try{
  47. $field_post_format = $node->get('field_post_format');
  48. if(isset($field_post_format->value) && $field_post_format->value){
  49. $post_format = $field_post_format->value;
  50. }
  51. }catch(Exception $e){
  52. $post_format = 'standard';
  53. }
  54. $iframe = '';
  55. if($post_format == 'video' || $post_format == 'audio'){
  56. try{
  57. $field_post_embed = $node->get('field_post_embed');
  58. if(isset($field_post_embed->value) && $field_post_embed->value){
  59. $autoembed = new AutoEmbed();
  60. $iframe = $autoembed->parse($field_post_embed->value);
  61. }else{
  62. $iframe = '';
  63. $post_format = 'standard';
  64. }
  65. }
  66. catch(Exception $e){
  67. $post_format = 'standard';
  68. }
  69. }
  70. $variables['gva_iframe'] = $iframe;
  71. $variables['post_format'] = $post_format;
  72. }
  73. }
  74. function gavias_linoor_preprocess_node__portfolio(&$variables){
  75. $node = $variables['node'];
  76. // Override lesson list on single course
  77. $output = '';
  78. $count_information = 0;
  79. if($node->hasField('field_portfolio_information')){
  80. $informations = $node->get('field_portfolio_information');
  81. $count_information = count($informations);
  82. foreach ($informations as $key => $information) {
  83. $texts = preg_split('/--/', $information->value);
  84. $information_text = '';
  85. foreach ($texts as $k => $text) {
  86. $information_text .= '<span>' . $text . '</span>';
  87. }
  88. $output .= '<div class="item-information">' . $information_text . '</div>';
  89. }
  90. }
  91. $variables['count_information'] = $count_information;
  92. $variables['informations'] = $output;
  93. }
  94. function gavias_linoor_preprocess_node__event(&$variables){
  95. $node = $variables['node'];
  96. $event_date = array();
  97. if($node->hasField('field_event_start')){
  98. $event_start = $node->field_event_start->value;
  99. if($event_start){
  100. $event_date['day'] = \Drupal::service('date.formatter')->format(strtotime($event_start), 'custom', 'd');
  101. $event_date['month'] = \Drupal::service('date.formatter')->format(strtotime($event_start), 'custom', 'F');
  102. }
  103. }
  104. $variables['event_date'] = $event_date;
  105. }
  106. function gavias_linoor_preprocess_breadcrumb(&$variables){
  107. $variables['#cache']['max-age'] = 0;
  108. $request = \Drupal::request();
  109. $title = '';
  110. if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
  111. $title = \Drupal::service('title_resolver')->getTitle($request, $route);
  112. }
  113. if($variables['breadcrumb']){
  114. foreach ($variables['breadcrumb'] as $key => &$value) {
  115. if($value['text'] == 'Node'){
  116. unset($variables['breadcrumb'][$key]);
  117. }
  118. }
  119. if(!empty($title)){
  120. $variables['breadcrumb'][] = array(
  121. 'text' => ''
  122. );
  123. $variables['breadcrumb'][] = array(
  124. 'text' => $title
  125. );
  126. }
  127. }
  128. }
  129. function gavias_linoor_print_icon($file, $alt){
  130. $file_content = file_get_contents($file);
  131. if(substr($file, -4) == '.svg'){
  132. echo $file_content;
  133. }else{
  134. echo '<img src="' . ($file) . '" alt="' . ($alt) . '" />';
  135. }
  136. }