Show field on entity form programmatically

How do you update entity display mode to show a newly created field by default? It is easy with configuration import, but what if you need to use hook_update_N?

If you add a field then it could be hidden for whatever reason or you just want to update its widget settings, then there are two options - use config factory to modify configuration or use configuration entity itself. I strongly recommend loading configuration entities and not using config factory on them - you have better API available and many times you need to write less code.

I have an example code for this, you can find it below. The method requires three arguments - field name, widget settings array, and entity type. There is an optional argument for specifying a bundle. The bundle argument is needed when you need to limit to a specific bundle if the field is reused because it will find all instances of that field.

You can modify it and update entity view mode settings as well, the configuration entities are quite similar. It uses the default display mode, but it can be changed.

/** * Enable field on form display. * * In case of reused fields, this will update all form displays. To limit * this, use $bundle argument. * * @param string $field_name * Field name to enable. * @param array $field_display_config * Field display configuration. * @param string $entity_type * Entity type. * @param string|NULL $bundle * Optional bundle option to limit which bundle to update. */ public function showFieldOnEntityForm(string $field_name, array $field_display_config, string $entity_type, string $bundle = NULL): void { // Arguments check. They need to be filled, otherwise things just break. if (empty($field_name) || empty($entity_type) || empty($field_display_config)) { $this->logger->error('Unable to show field on entity form. Missing required parameters.'); return; } try { // Find field config entities by field name. Should be one, // but we should handle reused fields as well. // If needed, bundle argument can limit reused field to update. /** @var \Drupal\field\Entity\FieldConfig[] $field_configs */ $field_configs = $this->entityTypeManager->getStorage('field_config') ->loadByProperties(['field_name' => $field_name, 'entity_type' => $entity_type]); foreach ($field_configs as $field_config) { // Get entity type and bundle from field config. $field_bundle = $field_config->getTargetBundle(); // Check if we need to update only specific bundle. if ($bundle && $bundle !== $field_bundle) { continue; } // Get form display for entity type and bundle. /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form_display */ $form_display = $this->entityTypeManager->getStorage('entity_form_display') ->load($entity_type . '.' . $field_bundle . '.default'); $form_display->setComponent($field_name, $field_display_config); $form_display->save(); } } catch (PluginException | EntityStorageException $e) { $this->logger->error('Unable to show field @field_name on entity form. @exception', [ '@field_name' => $field_name, '@exception' => $e->getMessage(), ]); } }
Buy Me a Coffee at ko-fi.com

Add new comment