You are here

class flag_node in Flag 7.2

Same name and namespace in other branches
  1. 5 flag.inc \flag_node
  2. 6.2 flag.inc \flag_node
  3. 6 flag.inc \flag_node
  4. 7.3 includes/flag/flag_node.inc \flag_node

Implements a node flag.

Hierarchy

Expanded class hierarchy of flag_node

2 string references to 'flag_node'
flag_flag_definitions in ./flag.inc
Implements hook_flag_definitions().
hook_flag_definitions in ./flag.api.php
Define one or more flag types.

File

./flag.inc, line 1490
Implements various flags. Uses object oriented style inspired by that of Views 2.

View source
class flag_node extends flag_entity {
  function options() {
    $options = parent::options();

    // Use own display settings in the meanwhile.
    unset($options['show_on_entity']);
    $options += array(
      'show_on_page' => TRUE,
      'show_on_teaser' => TRUE,
      'show_on_form' => FALSE,
      'show_contextual_link' => FALSE,
      'i18n' => 0,
    );
    return $options;
  }

  /**
   * Options form extras for node flags.
   */
  function options_form(&$form) {
    parent::options_form($form);
    $form['access']['access_author'] = array(
      '#type' => 'radios',
      '#title' => t('Flag access by content authorship'),
      '#options' => array(
        '' => t('No additional restrictions'),
        'own' => t('Users may only flag content they own'),
        'others' => t('Users may only flag content of others'),
      ),
      '#default_value' => $this->access_author,
      '#description' => t("Restrict access to this flag based on the user's ownership of the content. Users must also have access to the flag through the role settings."),
    );

    // Support for i18n flagging requires Translation helpers module.
    $form['i18n'] = array(
      '#type' => 'radios',
      '#title' => t('Internationalization'),
      '#options' => array(
        '1' => t('Flag translations of content as a group'),
        '0' => t('Flag each translation of content separately'),
      ),
      '#default_value' => $this->i18n,
      '#description' => t('Flagging translations as a group effectively allows users to flag the original piece of content regardless of the translation they are viewing. Changing this setting will <strong>not</strong> update content that has been flagged already.'),
      '#access' => module_exists('translation_helpers'),
      '#weight' => 5,
    );
    $form['display']['show_on_teaser'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display link on node teaser'),
      '#default_value' => $this->show_on_teaser,
      '#access' => empty($this->locked['show_on_teaser']),
    );
    $form['display']['show_on_page'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display link on node page'),
      '#default_value' => $this->show_on_page,
      '#access' => empty($this->locked['show_on_page']),
    );

    // Override the UI texts for nodes.
    $form['display']['show_on_form'] = array(
      '#title' => t('Display checkbox on node edit form'),
      '#description' => t('If you elect to have a checkbox on the node edit form, you may specify its initial state in the settings form <a href="@content-types-url">for each content type</a>.', array(
        '@content-types-url' => url('admin/structure/types'),
      )),
    ) + $form['display']['show_on_form'];
    $form['display']['show_contextual_link'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display in contextual links'),
      '#default_value' => $this->show_contextual_link,
      '#access' => empty($this->locked['show_contextual_link']) && module_exists('contextual'),
      '#weight' => 10,
    );
    unset($form['display']['show_on_entity']);
  }
  function type_access_multiple($content_ids, $account) {
    $access = array();

    // If all subtypes are allowed, we have nothing to say here.
    if (empty($this->types)) {
      return $access;
    }

    // Ensure that only flaggable node types are granted access. This avoids a
    // node_load() on every type, usually done by applies_to_content_id().
    $result = db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->condition('nid', array_keys($content_ids), 'IN')
      ->condition('type', $this->types, 'NOT IN')
      ->execute();
    foreach ($result as $row) {
      $access[$row->nid] = FALSE;
    }
    return $access;
  }

  /**
   * Adjust the Content ID to find the translation parent if i18n-enabled.
   *
   * @param $content_id
   *   The nid for the content.
   * @return
   *   The tnid if available, the nid otherwise.
   */
  function get_translation_id($content_id) {
    if ($this->i18n) {
      $node = $this
        ->fetch_content($content_id);
      if (!empty($node->tnid)) {
        $content_id = $node->tnid;
      }
    }
    return $content_id;
  }
  function uses_hook_link($teaser) {
    if ($teaser && $this->show_on_teaser || !$teaser && $this->show_on_page) {
      return TRUE;
    }
    return FALSE;
  }
  function flag($action, $content_id, $account = NULL, $skip_permission_check = FALSE) {
    $content_id = $this
      ->get_translation_id($content_id);
    return parent::flag($action, $content_id, $account, $skip_permission_check);
  }

  // Instead of overriding is_flagged() we override get_flagging_record(),
  // which is the underlying method.
  function get_flagging_record($content_id, $uid = NULL, $sid = NULL) {
    $content_id = $this
      ->get_translation_id($content_id);
    return parent::get_flagging_record($content_id, $uid, $sid);
  }
  function replace_tokens($label, $contexts, $options, $content_id) {
    if (is_numeric($content_id) && ($node = $this
      ->fetch_content($content_id))) {
      $contexts['node'] = $node;
    }
    elseif (!empty($content_id) && ($type = node_type_get_type($content_id))) {
      $content_id = NULL;
      $contexts['node'] = (object) array(
        'nid' => NULL,
        'type' => $type->type,
        'title' => '',
      );
    }
    return parent::replace_tokens($label, $contexts, $options, $content_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
flag_entity::applies_to_content_object function Checks whether the flag applies for the current entity bundle.
flag_entity::get_content_id function Returns the entity id, if it already exists. 1
flag_entity::get_flag_action function Returns a 'flag action' object. 2
flag_entity::get_labels_token_types function Returns token types for the current entity type. 1
flag_entity::get_relevant_action_objects function Returns objects the action may possible need. 2
flag_entity::get_views_info function Returns information for the Views integration. 1
flag_entity::_load_content function Loads the entity object.
flag_node::flag function
flag_node::get_flagging_record function
flag_node::get_translation_id function Adjust the Content ID to find the translation parent if i18n-enabled.
flag_node::options function Adds additional options that are common for all entity types. Overrides flag_entity::options
flag_node::options_form function Options form extras for node flags. Overrides flag_entity::options_form
flag_node::replace_tokens function Replaces tokens. Overrides flag_entity::replace_tokens
flag_node::type_access_multiple function
flag_node::uses_hook_link function Returns TRUE if the link should be displayed. Overrides flag_entity::uses_hook_link