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:

enter image description here

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

  • 2
    You should share the code you have tried. Also, there's always the option of implementing a custom widget that extends from the default. Thanks to OOP, you can simply call the parent and then extend the build render array.
    – Berdir
    Mar 10, 2017 at 19:15
  • Hello there, I still don't understand exactly what you want to achieve. Could you post the code you have tried and rephrase a little bit the description ? @ Mar 12, 2017 at 6:07
  • Hello, I've edited the original message. Hope it's clearer now. Thanks.
    – Bubba Gump
    Mar 12, 2017 at 9:05
  • I might be missing something, but why do you need entity reference in case you just need a simple link that is pointing towards a specific node ? Mar 12, 2017 at 13:26
  • Because I want the admin users to be able to associate a node to a field knowing only the node title. I know I can use html inside the description field, but I'm looking for a simpler solution.
    – Bubba Gump
    Mar 12, 2017 at 13:46

1 Answer 1


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, &$form, FormStateInterface $form_state) {
  $field->setThirdPartySetting('mymodule', 'help_page_id', $form_state->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(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  $field_name = $context['items']->getName();
  $field = FieldConfig::loadByName('node', 'myentity', $field_name);
  $help_page_id = $field->getThirdPartySetting('mymodule', 'help_page_id');

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.