You are here

heartbeat_like_comment.module in Heartbeat 8

File

modules/heartbeat_like_comment/heartbeat_like_comment.module
View source
<?php

/**
 * Implements hook_form_FORM_ID_alter().
 * Tokenize the flag link strings.
 */
function heartbeat_like_comment_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'flag_add_form' || $form_id == 'flag_edit_form') {

    // Setup for the "Browse available tokens" link.
    $browse_array = array(
      '#theme' => 'token_tree_link',
      '#token_types' => array(
        'heartbeat_like_comment',
      ),
    );

    // Validation options to add to form elements.
    $token_options = array(
      '#element_validate' => array(
        'token_element_validate',
      ),
      '#token_types' => array(),
      '#min_tokens' => 1,
      '#max_tokens' => 10,
    );

    // Let admin know that this field is now tokenized.
    $form['messages']['flag_short']['#description'] .= '<br />' . t('This field supports tokens (try: "Like [flagcount:count]")');
    $form['messages']['flag_short']['#suffix'] = render($browse_array);
    $form['messages']['flag_short'] += $token_options;
    $form['messages']['unflag_short']['#description'] .= '<br />' . t('This field supports tokens (try: "Unlike [flagcount:count]")');
    $form['messages']['unflag_short']['#suffix'] = render($browse_array);
    $form['messages']['unflag_short'] += $token_options;
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function heartbeat_like_comment_preprocess_flag(&$variables) {

  // Replace the link title with any tokens.
  $token = Drupal::token();
  $variables['title'] = $token
    ->replace($variables['title'], array(
    'flag_id' => $variables['flag']
      ->id(),
    'entity_id' => $variables['flaggable']
      ->id(),
  ));
}

Functions