How to Get the Path ID (pid) for a Node’s Path Alias in Drupal 7

After searching the internet I found nothing satisfactory for simply finding the Path ID for a node’s alias.

I need this this so that I could programmatic update a node alias in a way that keeps the Drupal site installation updated system wide without conflicts. So, anyway here is a simple function to find the alias path id for a given node.

By the way, if you got to   Home » Administration » Configuration » Search and metadata  and then hover over the edit link on the row of the alias in question, you will see the path is in the url.

<?php

/**
*
* Returns the path alias id for a given alias.
*
* $nid
* int    The node id
*
*/
function pathdata_get_pid( $nid ) {
if ( isset($nid)  && is_numeric($nid) ) {

    $source  = ‘node/’. $nid ;
$query = db_select(‘url_alias’, ‘ua’);
$result = $query->fields(‘ua’ )
->condition( ‘source’, $source , ‘=’)
->execute();
$data = $result->fetchAssoc();
return $data[‘pid’];
}
else {
return 0;
}
}

?>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *