I have a content type. One of the fields is a list that I want to populate from a database. The issue I'm running into is submitting the form. It doesn't submit. I can't put a name on the select that I'm creating. If I take the name out of the element, it submits but the select option isn't saved.

global $user;
	
  dpm($form);


$result = db_query("SELECT nid, title FROM {node} WHERE uid = :uid AND type = 'location'", array('uid' => $user->uid));
$locations = array();
	
foreach ($result as $record) {
	$locations[$record->nid] = t($record->title);
}
	
 $form['field_my_locations'] = array(
       '#type' => 'select',
       '#name' => 'field_my_locations[und]',
       '#title' => t('Locations'),
       '#options' => $locations,
       '#default_value' => $category['selected'],
       '#description' => t('If needed, you can <a href="/node/add/location">create locations</a>.'),
   );
   
?>
<?php print drupal_render($form['field_my_locations']); ?>

<?php print drupal_render_children($form); ?>

If I take the name out, it works but doesn't save. If I leave that in, the form doesn't submit. Please help me so I can try to like drupal

Comments

nevets’s picture

Why not use an entity reference field populated with a view?

dadixon’s picture

The module. Got it.

Jaypan’s picture

Is your problem solved?

taslett’s picture

Hi dadixon,
The #name attribute is not valid for selects form API reference #name

The field name is usually taken from the form array key, so adding #name may be causing a conflict.

nevets’s picture

There is a bigger problem of trying to add a form element outside the form API.

Jaypan’s picture

That's why I was asking if the problem has been solved or not. Not going to explain it if the user went with an entirely different solution.

dadixon’s picture

Problem not 100% solved. I want users to all see their locations. The module gives me the option for a specific user. Would must rather code something since i'm a programmer.

nevets’s picture

This is something you can do without code, given locations are nodes, you can use a entity reference field to allow them to select nodes of type location. By using a view, you can restrict the choices to only their locations.

dadixon’s picture

"By using a view, you can restrict the choices to only their locations"

How do you do that part? I've added a filter for author uid and it seems good but I can only choose one user. And it's the user I hard code. Is there a way to use whatever user is logged in?

nevets’s picture

Create a view that lists content of type location.

Add a relationship on content author.

Now you can add a filter (not a contextual filter) on the current user.

dadixon’s picture

Ah ok. That worked. Thanks

dadixon’s picture

Is there a way to connect to older data Tables? I migrated from Drupal 6 to Drupal 7 (nightmare) so all my older data is in a legacy data tables. Can I some way connect to those?