I have Table A who has a foreign key referring to Table B. I want my form associated with Table A to have a select field whose values will come from Table B. I want the select field to have options with key-value pairs coming from Table B.
Both Table A and Table B contain entities of different types.
I'm currently looking for programmatic solutions that use Entity Reference, but without having to go through adding a new field from Administration
> Structure
> <entity type>
.
I went with this solution that uses entity_load
then adding the loaded entities as options to the select field for now:
$foreign_entity_items = entity_load('foreign_entity');
$foreign_entity_options = array();
foreach ($foreign_entity_items as $foreign_entity_item) {
$foreign_entity_options[$foreign_entity_item->some_field] = $foreign_entity_item->some_other_field;
}
$form['some_form_element'] = array(
'#title' => t('Some title'),
'#type' => 'select',
'#required' => TRUE,
'#options' => $foreign_entity_options
);