You are here

class flag_entity in Flag 7.2

Same name and namespace in other branches
  1. 7.3 includes/flag/flag_entity.inc \flag_entity

Base entity flag handler.

Hierarchy

Expanded class hierarchy of flag_entity

2 string references to 'flag_entity'
flag_field_extra_fields in ./flag.module
Implements hook_field_extra_fields().
flag_flag_definitions_alter in ./flag.inc
Implements hook_flag_definitions_alter().

File

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

View source
class flag_entity extends flag_flag {

  /**
   * Adds additional options that are common for all entity types.
   */
  function options() {
    $options = parent::options();
    $options += array(
      // Output the flag in the entity links.
      // @see hook_entity_view().
      'show_on_entity' => TRUE,
      // Add a checkbox for the flag in the entity form.
      // @see hook_field_attach_form().
      'show_on_form' => FALSE,
      'access_author' => '',
    );
    return $options;
  }

  /**
   * Options form extras for the generic entity flag.
   */
  function options_form(&$form) {
    $bundles = array();
    $entity_info = entity_get_info($this->content_type);
    foreach ($entity_info['bundles'] as $bundle_key => $bundle) {
      $bundles[$bundle_key] = check_plain($bundle['label']);
    }
    $form['access']['types'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Bundles'),
      '#options' => $bundles,
      '#description' => t('Select the bundles that this flag may be used on. Leave blank to allow on all bundles for the entity type.'),
      '#default_value' => $this->types,
    );

    // Handlers may want to unset this option if they provide their own more
    // specific ways to show links.
    $form['display']['show_on_entity'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display link on entity'),
      '#default_value' => isset($this->show_on_entity) ? $this->show_on_entity : TRUE,
      '#access' => empty($this->locked['show_on_entity']),
      '#weight' => 0,
    );
    $form['display']['show_on_form'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display checkbox on entity edit form'),
      '#default_value' => $this->show_on_form,
      '#access' => empty($this->locked['show_on_form']),
      '#weight' => 5,
    );
  }

  /**
   * Loads the entity object.
   */
  function _load_content($content_id) {
    if (is_numeric($content_id)) {
      $entity = entity_load($this->content_type, array(
        $content_id,
      ));
      return reset($entity);
    }
    return NULL;
  }

  /**
   * Checks whether the flag applies for the current entity bundle.
   */
  function applies_to_content_object($entity) {
    $entity_info = entity_get_info($this->content_type);

    // The following conditions are applied:
    // - if the types array is empty, the flag applies to all bundles and thus
    //   to this entity.
    // - if the entity has no bundles, the flag applies to the entity.
    // - if the entity's bundle is in the list of types.
    if (empty($this->types) || empty($entity_info['entity keys']['bundle']) || in_array($entity->{$entity_info['entity keys']['bundle']}, $this->types)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Returns the entity id, if it already exists.
   */
  function get_content_id($entity) {
    $entity_info = entity_get_info($this->content_type);
    if ($entity && isset($entity->{$entity_info['entity keys']['id']})) {
      return $entity->{$entity_info['entity keys']['id']};
    }
  }

  /**
   * Returns TRUE if the link should be displayed.
   */
  function uses_hook_link($teaser) {
    if ($this->show_on_entity) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Returns token types for the current entity type.
   */
  function get_labels_token_types() {

    // The token type name might be different to the entity type name. If so,
    // an own flag entity handler can be used for overriding this.
    return array_merge(array(
      $this->content_type,
    ), parent::get_labels_token_types());
  }

  /**
   * Replaces tokens.
   */
  function replace_tokens($label, $contexts, $options, $content_id) {
    if ($content_id && ($entity = $this
      ->fetch_content($content_id))) {
      $contexts[$this->content_type] = $entity;
    }
    return parent::replace_tokens($label, $contexts, $options, $content_id);
  }

  /**
   * Returns a 'flag action' object.
   */
  function get_flag_action($content_id) {
    $flag_action = parent::get_flag_action($content_id);
    $entity = $this
      ->fetch_content($content_id);
    $flag_action->content_title = entity_label($this->content_type, $entity);
    $flag_action->content_url = _flag_url($this->content_type . '/' . $this
      ->get_content_id($entity));
    return $flag_action;
  }

  /**
   * Returns objects the action may possible need.
   */
  function get_relevant_action_objects($content_id) {
    return array(
      $this->content_type => $this
        ->fetch_content($content_id),
    );
  }

  /**
   * Returns information for the Views integration.
   */
  function get_views_info() {
    $entity_info = entity_get_info($this->content_type);
    return array(
      'views table' => $entity_info['base table'],
      'join field' => $entity_info['entity keys']['id'],
      'title field' => isset($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : '',
      'title' => t('@entity_label flag', array(
        '@entity_label' => $entity_info['label'],
      )),
      'help' => t('Limit results to only those entity flagged by a certain flag; Or display information about the flag set on a entity.'),
      'counter title' => t('@entity_label flag counter', array(
        '@entity_label' => $entity_info['label'],
      )),
      'counter help' => t('Include this to gain access to the flag counter field.'),
    );
  }

}

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::options function Adds additional options that are common for all entity types. 3
flag_entity::options_form function Options form extras for the generic entity flag. 3
flag_entity::replace_tokens function Replaces tokens. 2
flag_entity::uses_hook_link function Returns TRUE if the link should be displayed. 2
flag_entity::_load_content function Loads the entity object.