0

I am trying to accomplish a task with Drupal and I'm kind of stuck in how to implement this, so I'm wondering if anyone has any ideas or can at least point me in the right direction.

My company has over 60 locations. I am using Views to dynamically create a single "profile" page for each location. What I want is a way for the manager of each location to be able to log in and create articles for a kind of "What's Happening" section for their respective pages.

When the "manager" user accounts are created, I assign them a location based on a "Location" field that I added to the "User" content type.

What I want to do is add a field to the "Article" content type that is automatically populated with the "Location" of the manager that is posting the article. From there I can filter in Views to complete the page generation.

I could easily just add a "location" field to the Article content type that the user could manually select, but I know my users, and people will be creating articles for other sites and cause big mess for me to clean up. I'd like to have the "Location" field in the article automatically populate depending on the users' login.

Possible? If so, how?

0

I dont think you need to assign the location for the user.

When a person adds a content that content is automatically assigned the author. So if manager A creates a content then its author field is populated with Manager A.

Now you can use this information in views.

If you are creating a view with all the information related to the location you could create a view with all the contents added by the particular person.

  • Your answer does not resolve migration. What if manager is moved to manage other locations? All articles he wrote for location X should not suddenly get moved to location Y with him. – Mołot Oct 25 '13 at 14:52
  • yes. That is true. Molot I had not thought about that. – Mohammed Shameem Oct 25 '13 at 15:01
0

A quick solution to do this is implement a custom module that uses hook_form_alter to prepopulate the location field in the node creation form.

Here goes a similar question:

How to pre-populate fields on a node creation form based on fields from Profile2 module?

0

AFAIK there is not a standard way to do this. I recall writing a little module to make it possible to set a default value for a field which is a token string (and therefore dynamic), but I don't think anyone's written a public module along the same lines.

If you are up for a little coding you could write a form_alter hook to achieve it, something like:

function mymodule_form_mycontenttype_node_form_alter(&$form, &$form_state) {
  if(empty($form['field_location'][LANGUAGE_NONE][0]['#default_value'])) {
    global $user;
    $account = user_load($user->uid);
    if(!empty($user->field_location[LANGUAGE_NONE][0]['value'])) {
      $form['field_location'][LANGUAGE_NONE][0]['#default_value'] = $user->field_location[LANGUAGE_NONE][0]['value'];
    }
  }
}

This code is not tested and may also need to some adjustment depending on the field type you have used for location.

0

Thanks for the help everyone. Using Alfred's post I was able to write some code that does the trick:

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);        

} }

This does what I need it to, however, I'm having another problem with the entity references that has cropped up. It can be see here HEREand if anyone can help I certainly appreciate it.

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.