Skip to content

Instantly share code, notes, and snippets.

@clrockwell
Last active February 13, 2017 15:28
Show Gist options
  • Save clrockwell/1b70611fae3c38e893681b22aff4adfb to your computer and use it in GitHub Desktop.
Save clrockwell/1b70611fae3c38e893681b22aff4adfb to your computer and use it in GitHub Desktop.
<?php
/**
* snippets for dealing with custom entities
* Use dependency injection in actual code
*/
function d8_custom_entity_new_entity_form() {
$entity = \Drupal::entityTypeManager()
->getStorage('the_entity_type')
->create()
->setOwnerId(\Drupal::currentUser()->id());
// If you need to set some other default values on new entities:
$entity->set('field_name', 'field_value');
$form = \Drupal::service('entity.form_builder')->getForm($entity);
return $form
}
function d8_custom_entity_edit_entity_form(Entity $entity) {
$form = \Drupal::service('entity.form_builder')->getForm($entity);
return $form;
}
/**
* If you need to have multiple forms for the same entity type
* on the same page, override EntityFormInterface::getFormId()
*/
class customEntity extends ContentEntityForm {
public function getFormId() {
// This isn't a good solution for new entities, I'll work on that next.
return 'custom_form_name_' . $this->entity->id();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment