heartbeat_like.module 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Implements hook_form_FORM_ID_alter().
  4. * Tokenize the flag link strings.
  5. */
  6. function heartbeat_like_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  7. if ($form_id == 'flag_add_form' || $form_id == 'flag_edit_form') {
  8. // Setup for the "Browse available tokens" link.
  9. $browse_array = array(
  10. '#theme' => 'token_tree_link',
  11. '#token_types' => array('heartbeat_like'),
  12. );
  13. // Validation options to add to form elements.
  14. $token_options = array(
  15. '#element_validate' => array('token_element_validate'),
  16. '#token_types' => array(),
  17. '#min_tokens' => 1,
  18. '#max_tokens' => 10,
  19. );
  20. // Let admin know that this field is now tokenized.
  21. $form['messages']['flag_short']['#description'] .= '<br />' . t('This field supports tokens (try: "Like [flagcount:count]")');
  22. $form['messages']['flag_short']['#suffix'] = render($browse_array);
  23. $form['messages']['flag_short'] += $token_options;
  24. $form['messages']['unflag_short']['#description'] .= '<br />' . t('This field supports tokens (try: "Unlike [flagcount:count]")');
  25. $form['messages']['unflag_short']['#suffix'] = render($browse_array);
  26. $form['messages']['unflag_short'] += $token_options;
  27. }
  28. }
  29. /**
  30. * Implements hook_preprocess_HOOK().
  31. */
  32. function heartbeat_like_preprocess_flag(&$variables) {
  33. // Replace the link title with any tokens.
  34. $token = Drupal::token();
  35. $variables['title'] = $token->replace($variables['title'], array(
  36. 'flag_id' => $variables['flag']->id(),
  37. 'entity_id' => $variables['flaggable']->id(),
  38. ));
  39. }