materio_page_title.module 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Implements hook_permission().
  4. */
  5. function materio_page_title_permission() {
  6. $perms = array();
  7. $entity_infos = entity_get_info();
  8. foreach ($entity_infos['node']['bundles'] as $bundle => $value) {
  9. $perms['show '.$bundle.' title'] = array(
  10. 'title' => t('Show !bundle node title.', array('!bundle'=>$value['label'])),
  11. );
  12. }
  13. return $perms;
  14. }
  15. /**
  16. * Implements hook_field_access().
  17. */
  18. function materio_page_title_field_access($op, $field, $entity_type, $entity, $account) {
  19. if($field['field_name'] == 'title_field' && $entity_type == 'node' && isset($entity->type)){
  20. return user_access('show '.$entity->type.' title');
  21. }
  22. return TRUE;
  23. }
  24. function materio_page_title_preprocess_html(&$vars){
  25. // dsm($vars, '$vars');
  26. // if (arg(0) == 'node' && $node = node_load(arg(1))) {
  27. // if(!user_access('show '.$node->type.' title')){
  28. // if($node->type == "materiau"){
  29. // $title = field_get_items('node', $node, 'field_nature_titre');
  30. // $vars['head_title_array']['title'] = $title[0]['safe_value'];
  31. // }else{
  32. // $vars['head_title_array']['title'] = 'restricted access';
  33. // }
  34. // $vars['head_title'] = implode(' | ', $vars['head_title_array']);
  35. // // dsm($vars, 'vars');
  36. // }
  37. // }
  38. $vars['head_title_array']['title'] = materio_page_title_get_title();
  39. $vars['head_title'] = implode(' | ', $vars['head_title_array']);
  40. $vars['head_array']['title'] = $vars['head_title'];
  41. }
  42. /**
  43. * Implements hook_block_info().
  44. */
  45. function materio_page_title_block_info() {
  46. // This example comes from node.module.
  47. /*
  48. *$blocks['syndicate'] = array(
  49. * 'info' => t('Syndicate'),
  50. * 'cache' => DRUPAL_NO_CACHE
  51. *);
  52. */
  53. $blocks['materio_page_title'] = array(
  54. 'info' => t('Materio page title'),
  55. 'cache' => DRUPAL_NO_CACHE
  56. );
  57. return $blocks;
  58. }
  59. /**
  60. * Implements hook_block_view().
  61. */
  62. function materio_page_title_block_view($delta = '') {
  63. $block = array();
  64. switch ($delta) {
  65. case 'materio_page_title':
  66. $block['subject'] = t('Page title');
  67. $block['content'] = theme('materio_page_title', array('title' => materio_page_title_get_title()));
  68. drupal_add_js(drupal_get_path('module', 'materio_page_title').'/js/dist/materio_page_title.min.js');
  69. break;
  70. }
  71. return $block;
  72. }
  73. /**
  74. * Implements hook_theme().
  75. */
  76. function materio_page_title_theme($existing, $type, $theme, $path) {
  77. return array(
  78. 'materio_page_title' => array(
  79. 'variables' => array('title' => NULL,),
  80. ),
  81. );
  82. }
  83. function theme_materio_page_title($vars){
  84. if($vars['title'])
  85. return '<h1 id="materio-page-title" class="page-title">'.$vars['title'].'</h1>';
  86. return;
  87. }
  88. /**
  89. * Implements hook_menu().
  90. */
  91. // function materio_page_title_menu() {
  92. // $items = array();
  93. // $base = array(
  94. // 'type' => MENU_CALLBACK,
  95. // 'file' => 'materio_page_title.pages.inc',
  96. // );
  97. // $items['materio_page_title/refresh/block'] = $base+array(
  98. // 'title' => 'Materio page title refresh block',
  99. // 'page callback' => 'materio_page_title_refresh_block',
  100. // 'page arguments' => array(),
  101. // 'access callback' => TRUE,
  102. // );
  103. // return $items;
  104. // }
  105. /**
  106. * HELPERS
  107. * */
  108. function materio_page_title_get_title(){
  109. if (arg(0) == 'node' && $node = node_load(arg(1))) {
  110. if(!user_access('show '.$node->type.' title')){
  111. if($node->type == "materiau"){
  112. $title = field_get_items('node', $node, 'field_nature_titre');
  113. return $title[0]['safe_value'];
  114. }else{
  115. return 'restricted access';
  116. }
  117. }
  118. }
  119. return drupal_get_title();
  120. }