edlp_studio.module 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @file
  4. * Contains edlp_studio.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\Core\Url;
  8. use Drupal\edlp_studio\Entity\Chutier;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function edlp_studio_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. // Main module help for the edlp_studio module.
  15. case 'help.page.edlp_studio':
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('Edlp module that handle chutier and compositions entities') . '</p>';
  19. return $output;
  20. default:
  21. }
  22. }
  23. /**
  24. * Implements hook_page_attachments().
  25. * @param array $attachments
  26. */
  27. function edlp_studio_page_attachments(array &$attachments) {
  28. $attachments['#attached']['library'][] = 'edlp_studio/edlp_studio-library';
  29. // chutier ui
  30. $url = Url::fromRoute('edlp_studio.studio_chutier_ui_ajax', [], ['absolute' => TRUE]);
  31. $attachments['#attached']['drupalSettings']['edlp_studio']['chutier_ui_ajax'] = $url->getInternalPath();
  32. // open compo
  33. $url = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action'=>'open'], ['absolute' => TRUE]);
  34. $attachments['#attached']['drupalSettings']['edlp_studio']['open_compo_ajax_url'] = $url->getInternalPath();
  35. // save compo
  36. $url = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action'=>'save'], ['absolute' => TRUE]);
  37. $attachments['#attached']['drupalSettings']['edlp_studio']['save_compo_ajax_url'] = $url->getInternalPath();
  38. }
  39. /**
  40. * Implements hook_theme().
  41. */
  42. function edlp_studio_theme($existing, $type, $theme, $path) {
  43. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  44. return array(
  45. 'composition' => array(
  46. 'file' => 'includes/composition.inc',
  47. 'render element'=>'elements',
  48. ),
  49. 'edlp_chutier_ui' => array(
  50. 'file' => 'includes/edlp_chutier_ui.inc',
  51. 'variables' => array(
  52. 'title' => t('Chutier'),
  53. 'document_nodes' => array(),
  54. 'uid' => null,
  55. ),
  56. ),
  57. 'edlp_composition_ui' => array(
  58. 'file' => 'includes/edlp_composition_ui.inc',
  59. 'variables' => array(
  60. 'title' => t('Compositon'),
  61. 'compositions' => null,
  62. 'composer_header' => null,
  63. 'lastcomposition' => null,
  64. 'composer_actions' => null,
  65. ),
  66. ),
  67. 'edlp_compositions_list' => array(
  68. 'file' => 'includes/edlp_compositions_list.inc',
  69. 'variables' => array(
  70. 'composition_entities' => array(),
  71. 'new_composition_url' => Null,
  72. ),
  73. ),
  74. 'edlp_studio_ui' => array(
  75. 'file' => 'includes/edlp_studio_ui.inc',
  76. 'variables' => array(
  77. 'chutier_ui' => null,
  78. 'composition_ui' => null,
  79. ),
  80. ),
  81. );
  82. }
  83. /**
  84. * Implements hook_theme_suggestions_HOOK().
  85. */
  86. function edlp_studio_theme_suggestions_composition(array $vars) {
  87. // dpm($vars);
  88. $suggestions = [];
  89. /** @var \Drupal\edlp_studio\CompositionInterface $compo */
  90. $compo = $vars['elements']['#composition'];
  91. //
  92. $suggestions[] = 'composition';
  93. $suggestions[] = 'composition__' . $compo->id();
  94. $suggestions[] = 'composition__' . $vars['elements']['#view_mode'];
  95. $suggestions[] = 'composition__' . $compo->id() . '__' . $vars['elements']['#view_mode'];
  96. // $suggestions[] = 'taxonomy_term__' . $term->id();
  97. // dpm($suggestions);
  98. return $suggestions;
  99. }
  100. /**
  101. * hook_entity_extra_field_info()
  102. *
  103. */
  104. function edlp_studio_entity_extra_field_info(){
  105. $extra = [];
  106. // TODO: get node content type by settings @see readme
  107. $extra['node']['enregistrement']['display']['chutier_actions'] = [
  108. 'label' => t('Chutier actions'),
  109. 'description' => 'Display a link to add the content to chutier',
  110. 'weight' => 99,
  111. 'visible' => FALSE,
  112. ];
  113. return $extra;
  114. }
  115. /**
  116. * Implements hook_ENTITY_TYPE_view().
  117. * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  118. */
  119. function edlp_studio_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  120. $display_settings = $display->getComponent('chutier_actions');
  121. if (!empty($display_settings)) {
  122. // dpm($entity);
  123. // dpm($entity->id());
  124. $user = \Drupal::currentUser();
  125. // dpm($user);
  126. // check if user loged in ? no -> popup message : yes -> links
  127. $register_url = Url::fromRoute('user.register');
  128. if($user->id() == 0){
  129. $build['chutier_actions'] = array(
  130. '#type' => 'container',
  131. '#attributes' => array(
  132. 'class' => array('chutier-icon', 'not-logedin')
  133. ),
  134. "popup"=>array(
  135. '#type'=>'container',
  136. '#attributes' => array(
  137. 'class' => array('popup')
  138. ),
  139. "content"=>array(
  140. '#type'=>'container',
  141. '#attributes' => array(
  142. 'class' => array('inner')
  143. ),
  144. 'description'=>array(
  145. '#prefix'=>'<p>',
  146. '#markup'=>t('To bookmark sounds and make your own playlists, you must log in. Click the studio icon on the low-right corner of the page.'),
  147. '#suffix'=>'</p>'
  148. ),
  149. // TODO: ajouter le login form
  150. 'register'=>array(
  151. '#prefix'=>'<p>',
  152. 'register'=> array(
  153. '#type' => 'link',
  154. '#title' => t('Create new account'),
  155. '#url' => $register_url,
  156. '#options'=>array(
  157. 'attributes' => array(
  158. 'data-drupal-link-system-path' => $register_url->getInternalPath()
  159. )
  160. )
  161. ),
  162. '#suffix'=>'</p>'
  163. ),
  164. // 'login'=>array(
  165. // '#prefix'=>'<p>',
  166. // '#markup'=>t("Ou connectez vous en survolant l'icone du studio en bas a droite de la page"),
  167. // '#suffix'=>'</p>'
  168. // ),
  169. )
  170. )
  171. );
  172. }else{
  173. // TODO: check if user has permission 'use chutier'
  174. $url = Chutier::getActionsUrl($entity->id(), $user->id());
  175. $build['chutier_actions'] = array(
  176. '#type' => 'link',
  177. '#title' => 'Chutier',
  178. '#url' => $url,
  179. '#options'=>array(
  180. 'attributes' => array(
  181. 'data-drupal-link-system-path' => $url->getInternalPath()
  182. )
  183. )
  184. );
  185. }
  186. }
  187. }