You are here

aet.module in Advanced Entity Tokens 7

Same filename and directory in other branches
  1. 2.x aet.module

The AET main module file.

This file contains all of hooks and main programming logic of AET.

File

aet.module
View source
<?php

/**
 * @file
 * The AET main module file.
 *
 * This file contains all of hooks and main programming logic of AET.
 */

/******************************* DEFENITIONS **********************************/

// Checks if the uuid module exists only once.
define('AET_UUID', module_exists('uuid'));

/********************************** HOOKS *************************************/

/**
 * Clears unreplaced tokens.
 *
 * @param array $tokens
 *   The tokens array.
 * @param array $replacements
 *   The replacements array
 *
 * @param bool $clear
 *   The clear flag is used to set if unreplaced tags should be cleared.
 *   defaults to TRUE.
 */
function aet_clear_tokens($tokens, &$replacements, $clear = TRUE) {

  // Checks if the clear option has been flagged.
  if ($clear) {

    // Loop threw all the tokens.
    foreach ($tokens as $original) {

      // Check if the token original value exists in the replacements array.
      if (!isset($replacements[$original])) {

        // Replace the token in the replacements array to an empty string.
        $replacements[$original] = '';
      }
    }
  }
}

/**
 * Checks if an entity is being viewed.
 *
 * @return bool
 *   TRUE if the entity with $id as its id and of type $entity_type is being
 *   viewed.
 */
function aet_entity_is_viewed($entity_type, $id_key, $id) {

  // Get the function backtrace to check if the the entity view function is
  // part of the chain.
  $backtrace = debug_backtrace();
  foreach ($backtrace as $caller) {
    if ($caller['function'] === $entity_type . '_view') {

      // Move the $entity object to a new variable to increase readability.
      $entity = $caller['args'][0];

      // If the received id matches the viewed entity id (or uuid) than
      // return TRUE, which means the entity of the received id is being viewed.
      if ($id == $entity->{$id_key} || AET_UUID && $id == $entity->uuid) {
        return TRUE;
      }
    }
    elseif ($caller['function'] == '_drupal_bootstrap_full' || $caller == 'menu_execute_active_handler') {

      // There is no point in going back this far, so just stop.
      break;
    }
  }
  return FALSE;
}

/**
 * Checks if the current page is the full page view of the passed-in entity.
 *
 * @param int $entity_id
 *   An entity id.
 * @param string $id_key
 *   The entity id key (e.g. nid for nodes, fid for files).
 */
function aet_entity_is_page($entity_id, $id_key) {

  // Returns the object of the entity being viewed.
  $page_entity = menu_get_object();

  // Checks if no entity is being viewed or if it doesn't contain the a property
  // with $id_key as it's key.
  if (empty($page_entity) || !isset($page_entity->{$id_key})) {

    // If the above check is true than the passed in entity is not being viewed
    // directly.
    return FALSE;
  }

  // Returns true if the id of the passed in entity is equal to the id of the
  // entity being viewed.
  return $page_entity->{$id_key} === $entity_id;
}
function _aet_is_valid($id) {
  return is_numeric($id) || AET_UUID && uuid_is_valid($id);
}

Functions

Namesort descending Description
aet_clear_tokens Clears unreplaced tokens.
aet_entity_is_page Checks if the current page is the full page view of the passed-in entity.
aet_entity_is_viewed Checks if an entity is being viewed.
_aet_is_valid

Constants

Namesort descending Description
AET_UUID