You are here

dlike.module in Drupal like (Flag counter) 7.3

Same filename and directory in other branches
  1. 8 dlike.module
  2. 7 dlike.module
  3. 7.2 dlike.module

File

dlike.module
View source
<?php

include_once dirname(__FILE__) . '/dlike.inc';

/**
 * Implementation of hook_init().
 */
function dlike_init() {
  drupal_add_css(drupal_get_path('module', 'dlike') . '/dlike.css');
  drupal_add_js(drupal_get_path('module', 'dlike') . '/dlike.js');
}

/**
 * Implementation of hook_views_api().
 */
function dlike_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'dlike'),
  );
}

/**
 * Implementation of hook_perm().
 */
function dlike_permission() {
  $dlike_perm = array();
  $dlike_perm['dlike access list'] = array(
    'title' => t('Access list of users who flagged a content'),
  );
  return $dlike_perm;
}

/**
 * Implementation of hook_menu().
 */
function dlike_menu() {
  $items['dlike/%/%/%/%ctools_js'] = array(
    'page callback' => 'dlike_user_list',
    'page arguments' => array(
      1,
      2,
      3,
      4,
    ),
    'access arguments' => array(
      'dlike access list',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['dlike/%/%/%/append'] = array(
    'page callback' => 'dlike_append',
    'page arguments' => array(
      1,
      2,
      3,
    ),
    'access arguments' => array(
      'dlike access list',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_form_form_id_alter().
 */
function dlike_form_flag_form_alter(&$form, $form_state) {
  $form['dlike-' . $form['#flag']->name . '_option'] = array(
    '#type' => 'checkbox',
    '#title' => 'Add Drupal like',
    '#default_value' => variable_get('dlike-' . $form['#flag']->name . '_option', 0),
    '#weight' => '900',
  );
  $form['dlike-' . $form['#flag']->name . '_text_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Add the text you want to display'),
    '#description' => t('Use <em>@count</em> token for displaying the number of times content has been flagged.<br>e.g. "(@count likes)" or "@count people have flagged this" or "See who all have flagged this content"'),
    '#default_value' => variable_get('dlike-' . $form['#flag']->name . '_text_value', t('(@count)')),
    '#weight' => '901',
  );
  $form['dlike-modal-window-title-' . $form['#flag']->name] = array(
    '#type' => 'textfield',
    '#title' => t('Title for modal window'),
    '#default_value' => variable_get('dlike-modal-window-title-' . $form['#flag']->name, NULL),
    '#description' => t('e.g. "People who like this"'),
    '#weight' => '902',
  );
  $form['#submit'][] = 'dlike_form_data';
}

/**
 * Submit function to store the field values.
 */
function dlike_form_data(&$form, $form_state) {
  variable_set('dlike-' . $form_state['input']['name'] . '_option', $form_state['input']['dlike-' . $form_state['input']['name'] . '_option']);
  variable_set('dlike-' . $form_state['input']['name'] . '_text_value', $form_state['input']['dlike-' . $form_state['input']['name'] . '_text_value']);
  variable_set('dlike-modal-window-title-' . $form_state['input']['name'], $form_state['input']['dlike-modal-window-title-' . $form_state['input']['name']]);
}

/**
 * Implements hook_entity_view().
 */
function dlike_entity_view($entity, $type, $view_mode, $langcode) {
  $links = dlike_flag_link($type, $entity, $view_mode);
  $entity->content['links']['flag'] = array(
    '#theme' => 'links__node__flag',
    '#links' => $links,
    '#attributes' => array(
      'class' => array(
        'links',
        'inline',
      ),
    ),
  );
}

/**
 * This function is part of flag.module file.
 * This function is overridden here.
 */
function dlike_flag_link($type, $object = NULL, $view_mode) {

  // Get all possible flags for this entity type.
  $flags = flag_get_flags($type);
  foreach ($flags as $flag) {

    // Check if the flag outputs on entity view.
    if (!($flag->show_as_field || $flag
      ->shows_in_entity_links($view_mode))) {

      // Flag is not configured to output on entity view, so skip it to save on
      // calls to access checks.
      continue;
    }
    $entity_id = $flag
      ->get_entity_id($object);

    // For a new, unsaved entity, make a dummy entity ID so that the flag
    // handler can remember the entity. This allows access to the flag to be
    // correctly handled in node and comment preview.
    if (is_null($entity_id)) {
      $entity_id = 'new';
    }
    $flag
      ->remember_entity($entity_id, $object);
    if (!$flag
      ->access($entity_id) && (!$flag
      ->is_flagged($entity_id) || !$flag
      ->access($entity_id, 'flag'))) {

      // User has no permission to use this flag or flag does not apply to this
      // entity. The link is not skipped if the user has "flag" access but
      // not "unflag" access (this way the unflag denied message is shown).
      continue;
    }

    // We're good to go. Output the flag in the appropriate manner(s).
    $dlike_append = dlike_append($type, $entity_id, $flag->name);

    // The old-style entity links output.
    if ($flag
      ->shows_in_entity_links($view_mode)) {

      // The flag links are actually fully rendered theme functions.
      // The HTML attribute is set to TRUE to allow whatever the themer desires.
      $links['flag-' . $flag->name] = array(
        'title' => $flag
          ->theme($flag
          ->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id) . $dlike_append,
        'html' => TRUE,
      );
    }

    // The pseudofield output.
    if ($flag->show_as_field) {
      $entity->content['flag_' . $flag->name] = array(
        '#markup' => $flag
          ->theme($flag
          ->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id, array(
          'needs_wrapping_element' => FALSE,
        )) . $dlike_append,
      );
    }
  }
  if (isset($links)) {
    return $links;
  }
}
function dlike_create_link($type, $flag_name, $content_id) {
  $flag = flag_get_flag($flag_name);
  if (!$flag) {

    // Flag does not exist.
    return;
  }
  if (!$flag
    ->access($content_id) && (!$flag
    ->is_flagged($content_id) || !$flag
    ->access($content_id, 'flag'))) {

    // User has no permission to use this flag.
    return;
  }
  $dlike_append = dlike_append($type, $content_id, $flag_name);
  return $flag
    ->theme($flag
    ->is_flagged($content_id) ? 'unflag' : 'flag', $content_id) . $dlike_append;
}

Functions

Namesort descending Description
dlike_create_link
dlike_entity_view Implements hook_entity_view().
dlike_flag_link This function is part of flag.module file. This function is overridden here.
dlike_form_data Submit function to store the field values.
dlike_form_flag_form_alter Implementation of hook_form_form_id_alter().
dlike_init Implementation of hook_init().
dlike_menu Implementation of hook_menu().
dlike_permission Implementation of hook_perm().
dlike_views_api Implementation of hook_views_api().