function synonyms_select_entity_options in Synonyms 7
Construct options array for entity reference synonyms friendly select list.
Parameters
array $field: Field definition array of entityreference type for which to construct the options
array $instance: Field instance definition array that corresponds to $field
object $entity: If entity is known for which the options should be generated, provide it here. It is safe to omit this parameter. Frankly, I do not understand well why or how it is used. It is just directly passed into entityreference selection handler class
Return value
array Options array that can be plugged in directly into any #select form element
2 calls to synonyms_select_entity_options()
- synonyms_field_widget_form in ./
synonyms.module - Implements hook_field_widget_form().
- synonyms_views_handler_filter_entityreference_synonyms::value_form in views/
synonyms_views_handler_filter_entityreference_synonyms.inc - Provide a simple textfield for equality
File
- ./
synonyms.module, line 1324 - Provide synonyms feature for Drupal entities.
Code
function synonyms_select_entity_options($field, $instance, $entity = NULL) {
$options = entityreference_get_selection_handler($field, $instance, $instance['entity_type'], $entity)
->getReferencableEntities();
$synonyms_options = array();
$target_entity_info = entity_get_info($field['settings']['target_type']);
$entity_ids = array();
foreach ($options as $bundle_entity_ids) {
$entity_ids = array_merge($entity_ids, array_keys($bundle_entity_ids));
}
$entities = entity_load($field['settings']['target_type'], $entity_ids);
foreach ($options as $bundle => $bundle_entity_ids) {
$synonyms_options[$target_entity_info['bundles'][$bundle]['label']] = array();
$behavior_implementations = synonyms_behavior_get('select', $field['settings']['target_type'], $bundle, TRUE);
foreach ($bundle_entity_ids as $entity_id => $v) {
$entity = $entities[$entity_id];
$synonyms_options[$target_entity_info['bundles'][$bundle]['label']][] = synonyms_select_option_entity($entity, $field['settings']['target_type']);
foreach ($behavior_implementations as $behavior_implementation) {
foreach ($behavior_implementation['object']
->extractSynonyms($entity) as $synonym) {
$synonyms_options[$target_entity_info['bundles'][$bundle]['label']][] = synonyms_select_option_entity($entity, $field['settings']['target_type'], $synonym, $behavior_implementation);
}
}
}
usort($synonyms_options[$target_entity_info['bundles'][$bundle]['label']], 'synonyms_select_sort_name');
}
if (count($synonyms_options) == 1) {
$synonyms_options = reset($synonyms_options);
}
return $synonyms_options;
}