Update Entity Reference field programmatically I'm kind of new to Drupal development and I'm a little lost on something I'm trying to do:

My company has several locations and I am developing a method whereby each location can have their own page on my company's website.

I want location managers to be able to log in and post news articles about their respective locations and have those articles appear on the appropriate location page.  

I have content types set up for 'Location' and 'Site Articles'.  'Site Articles' has a 'Location' field that is a reference type back to Location.  

In an effort to restrict the ability of various site managers to select a location that is not theirs, I am using hook_form_alter() to adjust the 'Location' entity reference in the 'Site Article' content type that reads what Site a manager belongs to in their User Account Settings, and adjusts the form so that ONLY the managers site shows up.  That code looks like this:

function smg_site_articles_form_alter(&amp;$form, &amp;$form_state){
global $user;
$account = user_load($user-&gt;uid);
if(!empty($form['field_site_news_article_location'])){        
     $entity_node = node_load($account-&gt;field_user_location['und'][0]['target_id']);
     $form['field_site_news_article_location'] = array(
      '#title' =&gt; 'location',
      '#type' =&gt; 'select',  
      '#options' =&gt; array(
          0 =&gt; t($entity_node-&gt;title)
        ),
      '#default_value' =&gt; $entity_node-&gt;title);        


The problem that I'm running into is when I save the Article, the "Location" entity reference does not get populated and I can't add Views filters to it the way that I want.  

I'm assuming the problem lies with how I'm altering the 'Location' entity refernce field in hook_form_alter(), but I'm a little lost as to how I get the data updated correctly.  

Can anyone help??
Fields in forms are fun, there's some info about those here

To summarise, the element you're looking to alter will be something like

$form['field_site_news_article_location'][LANGUAGE_NONE][0]['target_id'];


Rather than replace the whole thing it would be wiser just to replace the existing options array, but that'll need a small change as well.

Select options are key => display, so if you use 0 as the key, it won't match up with anything on the backend (namely a node ID in this case), and the form won't save properly. Similarly the #default_value is expecting the key rather than the node title.

Something similar to the following should do the trick:

...
$options = array($entity_node-&gt;nid =&gt; $entity_node-&gt;title);
$form['field_site_news_article_location'][LANGUAGE_NONE][0]['target_id']['#options'] = $options;
$form['field_site_news_article_location'][LANGUAGE_NONE][0]['target_id']['#default_value'] = $entity_node-&gt;nid;