I have a content type of Location that has a list (text) field with options for Full Service or Quick Stop.

On another content type, I have an entity reference field that pulls in all the locations (using a select list widget).

On the node creation/edit form, I am trying to group those locations with optgroups into Full Service and Quick Stop based on the value of the nodes in the entity reference field (i.e. a location with field_location_type of "Quick Stop" would go into the Quick Stop optgroup).

Right now I am manually sorting them with the following, but it would be nice to do it automatically via my field_location_type field so that new options are automatically added.

$form['field_location']['und']['#options'] = array (
   'Full Service' => array(
      19 => 'Main Street',
      20 => 'Oak Hills',
      21 => 'Cherry Trail',
    'Quick Stop' => array(
      22 => 'Montreal',
      26 => 'Washington Place',
  • Hi, @MrPeanut. You are sorting options in a select list on a node edit form? You mentioned it should be "based on whatever is chosen on the location node". What is chosen where? Can you clarify this? – othermachines Aug 25 '15 at 19:25
  • @othermachines Updated with more info. Yes, node edit form. Based on the list (text) field (field_location_type) of the nodes in the entity reference field. So the entity reference field loads Location nodes. Locations have a list (text) field and therefore has "Quick Stop" or "Full Service" selected. – MrPeanut Aug 25 '15 at 19:31

Use the module Entityreference View Widget and set up a view with grouping based on location type.

Alternatively, using custom code:

// Grab our list of all possible location types.
$location_types = list_allowed_values(field_info_field("field_location_type"));
$options = array();

// Cycle through each location type.
foreach ($location_types as $location_type_key => $location_type) {
  // Find all the nodes that are set to use this location type.  
  // You may need to adjust this query if you need to limit it to only nodes of a specific type in the event you use field_location_type on other content types that you don't want showing up in this list.
  $efq = new EntityFieldQuery();
  $locations = $efq->entityCondition('entity_type', 'node')
    ->fieldCondition('field_location_type', 'value', $location_type_key)
    ->propertyOrderBy('title', 'DESC')

  // If any nodes are found with this location type, add them to the options array under the appropriate location type
  if (!empty($locations['node'])) {
    foreach($locations['node'] as $location) {
      $options[$location_type][$location->nid] = entity_label('node', node_load($location->nid));

$form['field_location']['und']['#options'] = $options;
  • That's sort of my last resort. My site users don't find modal windows very user friendly (nor do I). I was hoping to be able to just use optgroups instead. – MrPeanut Aug 25 '15 at 19:09
  • @MrPeanut Updated my answer with a custom code solution. – Aaron Aug 25 '15 at 19:30
  • That's very close! Awesome, but they're missing labels. The HTML result is <option value="19"></option> without a label. – MrPeanut Aug 25 '15 at 19:37
  • Updated my code. Forgot entity_label needed the whole entity. – Aaron Aug 25 '15 at 19:40
  • That's it! Thank you so much! One last question... Could I somehow put "Full Service Locations" instead of just "Full Service" (i.e. append "Locations")? – MrPeanut Aug 25 '15 at 19:42

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.