Set allowed value of a list field depending on the URL In the list field php code I want to set the value of a field:


first do a call to parse the URL.
take the [3] element (nid of the organic group) and use it to do a call to the db to get the node type for that nid. 
set the value of the field depending on the node type.


I had all this working in D6, now have to redo for D7.  Not having any luck getting it working yet.



function custom_pathnoder() {
    $pathnoder = array();
    $pathnoder = check_plain(drupal_get_path_alias($_GET['q']));
    $pathnoder = explode('/', $pathnoder);
  return $pathnoder;
}

$node = menu_get_object();
$pathnoder = custom_pathnoder();

$group_id = $pathnoder[3];


$query = db_select('node');
 $query-&gt;condition('nid', '$group_id', '=')
              -&gt;fields('type');
$og_type = $query-&gt;execute();



if ($og_type) {
if ($og_type == 'square_group')  {
$def_value = array( 0 =&gt; array('value' =&gt; square) );
}

elseif ($og_type == 'west_group')  {
$def_value = array( 0 =&gt; array('value' =&gt; west) );
}
elseif ($og_type == 'east_group')  {
$def_value = array( 0 =&gt; array('value' =&gt; east) );
}
elseif ($og_type == 'circle_group')  {
$def_value = array( 0 =&gt; array('value' =&gt; circle) );
}
elseif ($og_type == 'north_group')  {
$def_value = array( 0 =&gt; array('value' =&gt; north) );
}
elseif ($og_type == 'south_group')  {
$def_value = array( 0 =&gt; array('value' =&gt; south) );
}
}

if (isset($def_value)) {
  return $def_value;
}
else {
  return array(0 =&gt; array('value' =&gt; null));
}
}


@Shawn Conn

I changed the code to reflect the suggestions above but it's still not setting the value.  Can you see the reason?  

def_value = array();
$pathnoder = array();

$pathnoder = check_plain(drupal_get_path_alias($_GET['q']));
$pathnoder = explode('/', $pathnoder);


$og_type = db_select('node','n') 
    -&gt;fields('n', array('type'))
    -&gt;condition('nid', arg(3), '=')
    -&gt;execute()-&gt;fetchField();

switch ($og_type) {
    case square_group:
         $def_value = array( 0 =&gt; array('value' =&gt; square) );
         break;
    case west_group:
         $def_value = array( 0 =&gt; array('value' =&gt; west) );
         break;
    case east_group:
       $def_value = array( 0 =&gt; array('value' =&gt; east) );
       break;
    case circle_group:
       $def_value = array( 0 =&gt; array('value' =&gt; circle) );
       break;
    case north_group:
       $def_value = array( 0 =&gt; array('value' =&gt; north) );
       break;
    case south_group:
       $def_value = array( 0 =&gt; array('value' =&gt; south) );
       break;
}         
if (isset($def_value)) {
  return $def_value;
}
else {
  return array(0 =&gt; array('value' =&gt; null));
}
You're making this a little more convoluted than it needs be; the core arg() function can parse out the path and get the 4th element; the query is also broke (condition() is passed a string literal instead of variable &amp; it's missing aliasing argument); lastly you could help readability with a switch control. 

  //fields() needs an alias argument $group_id can be pulled from arg()
  $og_type = db_select('node','n') 
    -&gt;fields('n', array('type'))
    -&gt;condition('nid', arg(3), '=')
    -&gt;execute()-&gt;fetchField();