3

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(&$form, &$form_state){
global $user;
$account = user_load($user->uid);
if(!empty($form['field_site_news_article_location'])){        
     $entity_node = node_load($account->field_user_location['und'][0]['target_id']);
     $form['field_site_news_article_location'] = array(
      '#title' => 'location',
      '#type' => 'select',  
      '#options' => array(
          0 => t($entity_node->title)
        ),
      '#default_value' => $entity_node->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??

2

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->nid => $entity_node->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->nid;
  • Thanks a lot for your reply. I understand how this works a lot better now. I tried plugging your code into my module and it looks like it should work fine, however, my Location select list does not get filtered...it still has ALL of the locations to select from. The funny thing is, if I do a print_r($form['field_site_news_article_location'][LANGUAGE_NONE][0]['target_id']['#options']) it shows up correctly, but it's still not filtering the select box. Any ideas??? – gev125 Oct 29 '13 at 22:52
  • Sometimes (for an added bit of 'fun') you need to use $form['field_site_news_article_location'][LANGUAGE_NONE][0]['#options'] instead of usin – Clive Oct 30 '13 at 0:00
1

I managed to get it working by changing

$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->nid;

to

$form['field_site_news_article_location']['und']['#options'] = $options;
$form['field_site_news_article_location']['und']['#default_value'] = $entity_node->nid;

essentially removing the [0] and ['target_id'] and it sets the values and works perfect!

However, I don't understand why this works and the other way doesn't. Is there anybody that explain how this all works??

Thanks for help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.