Is there a way to load the current entity object? Is there a way, from anywhere in drupal, to get the current entity object? I'm using the hook hook_filter_FILTER_process() and I'd like to replace things in the $text with data from the entity, but it doesn't look like there is a way to access the entity without doing something like

if (!empty(arg(0)) &amp;&amp; is_numeric(arg(1)) {
    $entity = entity_load(arg(0), array(arg(1)));
}


This works most of the time, but sometimes (like taxonomy terms) it doesn't. On top of that, this is really messy as it makes another call to the database.

Is there a better way to do this?
I think menu_get_object() might be helpful:


  menu_get_object() provides access to objects loaded by the current router item. For example, on the page node/%node, the router loads the %node object, and calling menu_get_object() will return that. Normally, it is necessary to specify the type of object referenced, however node is the default. The following example tests to see whether the node being displayed is of the "story" content type:


&lt;?php
$node = menu_get_object();
$story = $node-&gt;type == 'story';
?&gt;


You can use this with any router-loaded object so it stands to reason that on entity pages (including taxonomy term pages) that this would get you the correct object:

$entity = menu_get_object($entity_type);


UPDATE

On further inspection you still have to pass the position of the ID in the URL as a parameter to menu_get_object() so it's pretty much the same as using arg(), but it's arguably a bit cleaner. For taxonomy terms the path is taxonomy/term/% so you would call:

$term = menu_get_object('taxonomy_term', 2);


It's worth bearing in mind that entity_load will only incur a database call if the entity hasn't previously been loaded in the current page request. If it's already been loaded, it will be served from the static cache.

UPDATE 2

Just to add an example of the static pattern to avoid reloading the entity over and over again if you're getting memory errors:

$loaded_entity = &amp;drupal_static(__FUNCTION__, NULL);
if (!$loaded_entity) {
  $loaded_entity = menu_get_object();
}
return $loaded_entity;