public function TwigExtension::drupalEntityForm in Twig Tweak 8.2
Gets the built and processed entity form for the given entity type.
Parameters
string $entity_type: The entity type.
mixed $id: (optional) The ID of the entity to build. If empty then new entity will be created.
string $form_mode: (optional) The mode identifying the form variation to be returned.
array $values: (optional) An array of values to set, keyed by property name.
bool $check_access: (optional) Indicates that access check is required.
Return value
array The processed form for the given entity type and form mode.
File
- src/
TwigExtension.php, line 625
Class
- TwigExtension
- Twig extension with some useful functions and filters.
Namespace
Drupal\twig_tweakCode
public function drupalEntityForm($entity_type, $id = NULL, $form_mode = 'default', array $values = [], $check_access = TRUE) {
$entity_storage = \Drupal::entityTypeManager()
->getStorage($entity_type);
if ($id) {
$entity = $entity_storage
->load($id);
$operation = 'update';
}
else {
$entity = $entity_storage
->create($values);
$operation = 'create';
}
if ($entity) {
$access = $check_access ? $entity
->access($operation, NULL, TRUE) : AccessResult::allowed();
if ($access
->isAllowed()) {
$build = \Drupal::service('entity.form_builder')
->getForm($entity, $form_mode);
CacheableMetadata::createFromRenderArray($build)
->merge(CacheableMetadata::createFromObject($entity))
->merge(CacheableMetadata::createFromObject($access))
->applyTo($build);
return $build;
}
}
}