Module Description
This module provides a new field widget which shows a read-only version of a field on a form for Drupal 8.x.

It renders to the field value using one of the available field formatters for that field. The formatter can be selected in the field widget settings.

The widget is available to use for any field type.

NOTE: The field must have a value for this widget to show - this can either be a default value (set while the widget uses a form type widget or programatically) or from the saved value while on an entity edit form.

- Still shows up if the user doesn't have edit access to the field but they do have view access. - The formatter options for the field are available under the widget settings. - Label position and hide/show option is available under the widget settings.

A common scenario where this module is useful is switching the form widget type of an entity reference field conditionally when the referenced entity is known. This makes a prettier UX than a disabled select/autocomplete.

/** * Implements hook_ENTITY_TYPE_prepare_form(). */ function my_module_node_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) { // Set the value of field_my_entity_ref_field if id is known. if ($entity instanceof NodeInterface && $entity->bundle() == 'MY_BUNDLE') { $ref_id = \Drupal::request()->get('ref_id'); $ref_node = \Drupal\node\Entity\Node::load($ref_id); if ($ref_node instanceof NodeInterface && $ref_node->bundle() == 'MY_REF_BUNDLE') { $entity->set('field_my_entity_ref_field', $ref_node); } } } /** * Implements hook_entity_form_display_alter(). */ function my_module_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context) { // Change the field_my_entity_ref_field to a readonly field widget if id is known. if ($context['entity_type'] == 'node' && $context['bundle'] == 'MY_BUNDLE') { $ref_id = \Drupal::request()->get('ref_id'); $ref_node = \Drupal\node\Entity\Node::load($ref_id); if ($ref_node instanceof NodeInterface && $ref_node->bundle() == 'MY_REF_BUNDLE') { $component = $form_display->getComponent('field_my_entity_ref_field'); if ($component) { $component['type'] = 'readonly_field_widget'; $component['settings'] = [ 'label' => 'inline', 'formatter_type' => 'entity_reference_label', 'formatter_settings' => [ 'entity_reference_label' => ['link' => FALSE], ] ]; $form_display->setComponent('field_my_entity_ref_field', $component); } } } }
Project Usage
3327
Creation Date
Changed Date
Security Covered
Covered By Security Advisory
Version Available
Production
Module Summary
This module provides a new field widget for Drupal 8.x that shows a read-only version of a field on a form, rendering the field value using available formatters and allowing selection of formatter options.
Data Name
readonly_field_widget

OPENAI CHATBOT

OPENAI CHATBOT

16:24:48
Generic Chatbot
Hi, I'm a Drupal module expert powered by OpenAI, answering your questions about the Drupal module ecosystem. How can I be helpful today? Please note that we will log your question.