Adding elements to a field widget 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, \Drupal\Core\Form\FormStateInterface $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, \Drupal\Core\Form\FormStateInterface $form_state, $context) {

}


I guess I have to load the field configuration entity, but I don't know how.
Make sure you include the following:

use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;


I'm assuming your function mymodule_form_field_config_edit_form_builder() looks something like this:

/**
 * My module form builder to map values to third party settings.
 */
function mymodule_form_field_config_edit_form_builder($entity_type, FieldConfig $field, &amp;$form, FormStateInterface $form_state) {
  $field-&gt;setThirdPartySetting('mymodule', 'help_page_id', $form_state-&gt;getValue('help_page_id'));
}


In your widget form alter you'd access the settings with with FieldConfig. This would add it to all field types, you can check $field_name if you want to filter it a bit.

function mymodule_field_widget_form_alter(&amp;$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  $field_name = $context['items']-&gt;getName();
  $field = FieldConfig::loadByName('node', 'myentity', $field_name);
  $help_page_id = $field-&gt;getThirdPartySetting('mymodule', 'help_page_id');
}