View source
<?php
function pathauto_persist_entity_load($entities, $type) {
static $loadable_types;
if (!isset($loadable_types)) {
$loadable_types =& drupal_static(__FUNCTION__);
if (!isset($loadable_types)) {
$loadable_types = db_query("SELECT DISTINCT entity_type FROM {pathauto_persist}")
->fetchCol();
}
}
if (!in_array($type, $loadable_types)) {
return;
}
$states = pathauto_persist_entity_state_load_multiple($type, array_keys($entities));
foreach ($states as $id => $state) {
if (!isset($entities[$id]->path)) {
$entities[$id]->path = array();
}
if (is_array($entities[$id]->path) && !isset($entities[$id]->path['pathauto'])) {
$entities[$id]->path['pathauto'] = $state;
}
}
}
function pathauto_persist_entity_presave($entity, $type) {
if (isset($entity->path['pathauto']) && is_array($entity->path)) {
$entity->path += array(
'alias' => '',
);
}
}
function pathauto_persist_entity_insert($entity, $type) {
if (isset($entity->path['pathauto'])) {
pathauto_persist_entity_state_save($type, $entity, $entity->path['pathauto']);
}
}
function pathauto_persist_entity_update($entity, $type) {
if (isset($entity->path['pathauto'])) {
pathauto_persist_entity_state_save($type, $entity, $entity->path['pathauto']);
}
}
function pathauto_persist_entity_state_load($entity_type, $entity_id) {
$pathauto_state = pathauto_persist_entity_state_load_multiple($entity_type, array(
$entity_id,
));
return !empty($pathauto_state) ? reset($pathauto_state) : FALSE;
}
function pathauto_persist_entity_state_load_multiple($entity_type, $entity_ids) {
$pathauto_state = db_query("SELECT entity_id, pathauto FROM {pathauto_persist} WHERE entity_type = :entity_type AND entity_id IN (:entity_ids)", array(
':entity_type' => $entity_type,
':entity_ids' => $entity_ids,
))
->fetchAllKeyed();
return $pathauto_state;
}
function pathauto_persist_entity_state_save($entity_type, $entity, $pathauto_state) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
db_merge('pathauto_persist')
->key(array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
))
->fields(array(
'pathauto' => $pathauto_state ? 1 : 0,
))
->execute();
drupal_static_reset('pathauto_persist_entity_load');
}