ProgPalpiteLink.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Drupal\popsu_link_block\Plugin\Block;
  3. use Drupal\Core\Block\BlockBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Url;
  6. use Drupal\Core\Link;
  7. // use Drupal\Core\Utility\Token;
  8. /**
  9. * Provides a 'ProgExodeurbainLink' block.
  10. *
  11. * @Block(
  12. * id = "prog_palpite_link",
  13. * admin_label = @Translation("Prog palpite link"),
  14. * )
  15. */
  16. class ProgPalpiteLink extends BlockBase {
  17. public function blockForm($form, FormStateInterface $form_state) {
  18. $form = parent::blockForm($form, $form_state);
  19. $config = $this->getConfiguration();
  20. $form['link_title_wrapper'] = [
  21. '#type' => 'container'
  22. ];
  23. $form['link_title_wrapper']['link_title'] = [
  24. '#type' => 'textfield',
  25. '#title' => 'Link title',
  26. '#default_value' => isset($config['link_title']) ? $config['link_title'] : "Ressources",
  27. '#element_validate' => array('token_element_validate'),
  28. '#token_types' => array('node'),
  29. ];
  30. $form['link_title_wrapper']['token_tree'] = array(
  31. '#theme' => 'token_tree_link',
  32. '#token_types' => array('node'),
  33. '#show_restricted' => TRUE,
  34. '#global_types' => FALSE,
  35. '#weight' => 90,
  36. );
  37. return $form;
  38. }
  39. public function blockSubmit($form, FormStateInterface $form_state)
  40. {
  41. parent::blockSubmit($form, $form_state);
  42. $values = $form_state->getValues();
  43. $link_title = $values['link_title_wrapper']['link_title'];
  44. $this->configuration['link_title'] = $link_title;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function build() {
  50. // $config = $this->getConfiguration();
  51. $build = [];
  52. $node = \Drupal::routeMatch()->getParameter('node');
  53. if ($node instanceof \Drupal\node\Entity\Node) {
  54. // check if programme
  55. if($node->bundle() == 'programme'){
  56. // if yes get id
  57. // todo set the hardcoded nid number as a parameter somewhere
  58. $nid = $node->id() == 6 ? $node->id() : false;
  59. }
  60. if($nid){
  61. $title = "Le Palmarès Palpite";
  62. $url = Url::fromRoute('entity.node.canonical');
  63. // todo make the the hardcoded target nid as a parameter somewhere (recorded on the node ?)
  64. $url->setRouteParameter("node", 912);
  65. $url->setOption('attributes', array("class"=>array('prog-palpite-link')));
  66. $build['prog_palpite_link'] = array(
  67. '#title' => $title,
  68. '#type' => 'link',
  69. '#url' => $url
  70. );
  71. }
  72. }
  73. return $build;
  74. }
  75. }