edlp_studio.module 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. }
  30. /**
  31. * hook_entity_extra_field_info()
  32. *
  33. */
  34. function edlp_studio_entity_extra_field_info(){
  35. $extra = [];
  36. // TODO: get node content type by settings @see readme
  37. $extra['node']['enregistrement']['display']['chutier_actions'] = [
  38. 'label' => t('Chutier actions'),
  39. 'description' => 'Display a link to add the content to chutier',
  40. 'weight' => 99,
  41. 'visible' => FALSE,
  42. ];
  43. return $extra;
  44. }
  45. /**
  46. * Implements hook_ENTITY_TYPE_view().
  47. * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  48. */
  49. function edlp_studio_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  50. $display_settings = $display->getComponent('chutier_actions');
  51. if (!empty($display_settings)) {
  52. // dpm($entity);
  53. // dpm($entity->id());
  54. $user = \Drupal::currentUser();
  55. // dpm($user);
  56. if($user->id() == 0){
  57. // // TODO: check if user loged in ? yes -> links : no : popup message
  58. // $action = 'add';
  59. }
  60. // else{
  61. // // TODO: check if node already in chutier ? no -> add link : yes -> remove link
  62. // // use getUserChutiersContents() from \Drupal\edlp_studio\ChutierInterface
  63. // $contents = Chutier::getUserChutiersContents($user->id());
  64. // dpm($contents);
  65. // if(array_search($entity->id(), $contents) === false){
  66. // $action = 'add';
  67. // }else{
  68. // $action = 'remove';
  69. // }
  70. // }
  71. // $args = array(
  72. // 'action'=>$action,
  73. // 'id'=>$entity->id()
  74. // );
  75. // $url = Url::fromRoute('edlp_studio.chutier_controller_ajax_add_content', $args, array(
  76. // 'attributes' => array(
  77. // 'class' => ['chutier-link','chutier-ajax-link'],
  78. // 'action' => $action,
  79. // 'target_id' => $entity->id(),
  80. // )
  81. // ));
  82. $url = Chutier::getActionsUrl($entity->id(), $user->id());
  83. $build['chutier_actions'] = array(
  84. '#title' => t("Chutier."),
  85. '#type' => 'link',
  86. '#url' => $url,
  87. '#options'=>array(
  88. 'attributes' => array(
  89. 'data-drupal-link-system-path' => $url->getInternalPath()
  90. )
  91. )
  92. );
  93. }
  94. }