edlp_studio.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. if($user->id() == 0){
  128. $build['chutier_actions'] = array(
  129. '#type' => 'container',
  130. '#attributes' => array(
  131. 'class' => array('chutier-icon', 'not-logedin')
  132. ),
  133. "popup"=>array(
  134. '#type'=>'container',
  135. '#attributes' => array(
  136. 'class' => array('popup')
  137. ),
  138. "content"=>array(
  139. '#type'=>'container',
  140. '#attributes' => array(
  141. 'class' => array('inner')
  142. ),
  143. 'text'=>array(
  144. '#prefix'=>'<p>',
  145. '#markup'=>t('Le Studio rassemble vos documents favoris.Il permet de les sauvgarder et de les agencer en compositions.'),
  146. '#suffix'=>'</p>'
  147. ),
  148. 'links'=>array(
  149. '#prefix'=>'<p>',
  150. '#markup'=>'todo: login link',
  151. '#suffix'=>'</p>'
  152. )
  153. )
  154. )
  155. );
  156. }else{
  157. // TODO: check if user has permission 'use chutier'
  158. $url = Chutier::getActionsUrl($entity->id(), $user->id());
  159. $build['chutier_actions'] = array(
  160. '#type' => 'link',
  161. '#title' => 'Chutier',
  162. '#url' => $url,
  163. '#options'=>array(
  164. 'attributes' => array(
  165. 'data-drupal-link-system-path' => $url->getInternalPath()
  166. )
  167. )
  168. );
  169. }
  170. }
  171. }