What I need to accomplish is adding a link to a node to field widgets (the node contains the formatted instructions to properly compile the field).
The goal is something like this:
“What is this?” is the link to a node, to be opened in a new tab or in a popup. In this example the field is a select, but it could be any type of field.
So far I’ve been able to add the node reference field in the field config form, using Third Party Settings:
function mymodule_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
if ($form_id === 'field_config_edit_form' && $form_state->getFormObject()->getEntity()->get('entity_type') == 'myentity') {
$field = $form_state->getFormObject()->getEntity();
$help_page_id = $field->getThirdPartySetting('mymodule', 'help_page_id');
$form['help_page_id'] = array(
'#type' => 'entity_autocomplete',
'#title' => t('Help page'),
'#target_type' => 'node',
'#selection_handler' => 'default',
'#selection_settings' => array(
'target_bundles' => array('help_page'),
),
'#default_value' => $help_page_id ? Node::load($help_page_id) : NULL,
'#weight' => 100,
);
$form['#entity_builders'][] = 'mymodule_form_field_config_edit_form_builder';
}
}
Now I’m having troubles retrieving this information when showing the form, and altering the field widget in order to display the link.
Using hook_field_widget_form_alter, I cannot get the custom configuration value from the arguments I have:
function mymodule_field_widget_form_alter(&$element, DrupalCoreFormFormStateInterface $form_state, $context) {
}
I guess I have to load the field configuration entity, but I don’t know how.