0

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>.

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.