function entity_get_form_display in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/includes/entity.inc \entity_get_form_display()
Returns the entity form display associated with a bundle and form mode.
The function reads the entity form display object from the current configuration, or returns a ready-to-use empty one if no configuration entry exists yet for this bundle and form mode. This streamlines manipulation of entity form displays by always returning a consistent object that reflects the current state of the configuration.
Example usage:
- Set the 'body' field to be displayed with the 'text_textarea_with_summary' widget and the 'field_image' field to be hidden on article nodes in the 'default' form mode.
entity_get_form_display('node', 'article', 'default')
->setComponent('body', array(
'type' => 'text_textarea_with_summary',
'weight' => 1,
))
->setComponent('field_image', array(
'type' => 'hidden',
))
->save();
\Drupal::entityManager()
->getStorage('entity_form_display')
->load($entity_type . '.' . $bundle . '.' . $form_mode);
When the entity form display is not available in configuration, you can create a new EntityFormDisplay object using:
$values = ('entity_form_display', array(
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
));
\Drupal::entityManager()->getStorage('entity_form_display')->create($values);
Parameters
string $entity_type: The entity type.
string $bundle: The bundle.
string $form_mode: The form mode.
Return value
\Drupal\Core\Entity\Display\EntityFormDisplayInterface The entity form display associated with the given form mode.
Deprecated
as of Drupal 8.0.x, will be removed before Drupal 9.0.0. If the entity form display is available in configuration use:
See also
\Drupal\Core\Entity\EntityStorageInterface::create()
\Drupal\Core\Entity\EntityStorageInterface::load()
16 calls to entity_get_form_display()
- CommentTestBase::setCommentSubject in core/
modules/ comment/ src/ Tests/ CommentTestBase.php - Sets the value governing whether the subject field should be enabled.
- CommentTestTrait::addDefaultCommentField in core/
modules/ comment/ src/ Tests/ CommentTestTrait.php - Adds the default comment field to an entity.
- EntityFormDisplayEditForm::getEntityDisplay in core/
modules/ field_ui/ src/ Form/ EntityFormDisplayEditForm.php - Returns an entity display object to be used by this form.
- EntityFormDisplayTest::testDeleteField in core/
modules/ field_ui/ src/ Tests/ EntityFormDisplayTest.php - Tests deleting field.
- EntityFormDisplayTest::testEntityGetFromDisplay in core/
modules/ field_ui/ src/ Tests/ EntityFormDisplayTest.php - Tests entity_get_form_display().
File
- core/
includes/ entity.inc, line 538 - Entity API for handling entities like nodes or users.
Code
function entity_get_form_display($entity_type, $bundle, $form_mode) {
// Try loading the entity from configuration.
$entity_form_display = entity_load('entity_form_display', $entity_type . '.' . $bundle . '.' . $form_mode);
// If not found, create a fresh entity object. We do not preemptively create
// new entity form display configuration entries for each existing entity type
// and bundle whenever a new form mode becomes available. Instead,
// configuration entries are only created when an entity form display is
// explicitly configured and saved.
if (!$entity_form_display) {
$entity_form_display = entity_create('entity_form_display', array(
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
));
}
return $entity_form_display;
}