12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace Drupal\popsu_link_block\Plugin\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Url;
- use Drupal\Core\Link;
- // use Drupal\Core\Utility\Token;
- /**
- * Provides a 'ProgRessourceLink' block.
- *
- * @Block(
- * id = "prog_ressource_link",
- * admin_label = @Translation("Prog ressource link"),
- * )
- */
- class ProgRessourceLink extends BlockBase {
- public function blockForm($form, FormStateInterface $form_state) {
- $form = parent::blockForm($form, $form_state);
- $config = $this->getConfiguration();
- $form['link_title_wrapper'] = [
- '#type' => 'container'
- ];
- $form['link_title_wrapper']['link_title'] = [
- '#type' => 'textfield',
- '#title' => 'Link title',
- '#default_value' => isset($config['link_title']) ? $config['link_title'] : "Ressources",
- '#element_validate' => array('token_element_validate'),
- '#token_types' => array('node'),
- ];
- $form['link_title_wrapper']['token_tree'] = array(
- '#theme' => 'token_tree_link',
- '#token_types' => array('node'),
- '#show_restricted' => TRUE,
- '#global_types' => FALSE,
- '#weight' => 90,
- );
- return $form;
- }
- public function blockSubmit($form, FormStateInterface $form_state)
- {
- parent::blockSubmit($form, $form_state);
- $values = $form_state->getValues();
- $link_title = $values['link_title_wrapper']['link_title'];
- $this->configuration['link_title'] = $link_title;
- }
- /**
- * {@inheritdoc}
- */
- public function build() {
- $config = $this->getConfiguration();
- $build = [];
- $node = \Drupal::routeMatch()->getParameter('node');
- if ($node instanceof \Drupal\node\Entity\Node) {
- $nid = $node->id();
- // $title = $config["link_title"];
- $token_service = \Drupal::token();
- $title = $token_service->replace($config["link_title"], array('node' => $node));
- $url = Url::fromRoute("view.centre_de_ressources.page_1");
- $url->setRouteParameter("programme", $nid);
- $url->setOption('attributes', array("class"=>array('prog-ressources-link')));
- $build['prog_ressource_link'] = array(
- '#title' => $title,
- '#type' => 'link',
- '#url' => $url
- );
- }
- return $build;
- }
- }
|