3

i have:

/**
 * @ContentEntityType(
 *   id = "my_entity",
 *   label = @Translation("drwywr werwer label"),
 *   bundle_label = @Translation("My entity type"),
 *   handlers = {
 *     bla...
 *     "form" = {
 *       "default" = "Drupal\my_entity\Form\MyEntityForm",
 *       "edit" = "Drupal\my_entity\Form\MyEntityForm",
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
 *     },
 *     bla...
 *   },
 *   translatable = FALSE,
 *   entity_keys = { bla... },
 *   bundle_entity_type = "my_entity_type",
 *   links = {
 *     "collection" = "/admin/test/entity/collection",
 *     "canonical" = "/admin/test/entity/{my_entity}",
 *     "edit-form" = "/admin/test/entity/{my_entity}/edit",
 *     "delete-form" = "/admin/test/entity/{my_entity}/delete"
 *   }
 * )
 */

based on above, how do i load my entity in form below:

<?php

namespace Drupal\my_entity\Form;

use bla...

/**
 * Form controller for the node edit forms.
 */
class MyEntityForm extends ContentEntityForm {
 //how to load my entity type here?
}

i tried a couple way but i keep receiving:

Error: Call to a member function getEntityTypeId() on null

mainly because the entity is not loaded (null value)

this is how i create the route:

in my_entity.routing.yml:

route_callbacks:
  - 'entity.my_entity.routes::routes'

in routes:

public function routes() {
// Build Entity Create Route - CREATE
  $route = new Route(
      (substr($type->getRoute()['path'], -1) == '/' ? $type->getRoute()['path'] . $type->id() : $type->getRoute()['path'] . '/' . $type->id()) . '/create',
      [
        '_form' => '\Drupal\my_entity\Form\MyEntityForm', //TODO : not finish
        'entity_type' => 'my_entity',
        'bundle' => $type->id(),
        '_title' => 'Create ' . $type->getRoute()['label'],
      ],
      ['_permission' => 'entity admin settings']
    );
  $route_collection->add('entity.my_entity.' . $type->id() . '_create', $route);
}

if you see in my content entity type config above, i have:

 *   links = {
 *     "collection" = "/admin/test/entity/collection",
 *     "canonical" = "/admin/test/entity/{my_entity}",
 *     "edit-form" = "/admin/test/entity/{my_entity}/edit",
 *     "delete-form" = "/admin/test/entity/{my_entity}/delete"
 *   }

which i want those: edit, delete to be able to 'link' with my form as well - how to do this?

what i'm trying to achieve is: to generate forms based on field define in my entity type

  • How is your route defined? Please add your definition to the question. – Eyal May 1 '17 at 6:01
  • Also the form class (ArmEntityForm) is not mentioned in the form handlers section of the entity. – Eyal May 1 '17 at 6:01
  • Sorry the class in my php snippet is MyEntityForm it is typo. I have fix it, the route is generated based on links in contententitytype config – AnD May 1 '17 at 6:15
  • Please add the definition for the form route. I cannot help you if i can't see how you defined the route for the form. – Eyal May 1 '17 at 6:46
  • Thanks @Eyal, I have updated my question above to explain how i build the route – AnD May 1 '17 at 11:08
3

Your code currently loads the form with the _form routing definition. For classes extending ContentEntityForm, you have to use the _entity_form notation instead. Here is an example of an 'module_name.routing.yml' file:

kollektivhampel.pdf_erstellen_form:
  path: '/intranet/{my_entity}/something'
  defaults:
    _entity_form: 'my_entity.default'
    _title: 'Your page title'
  options:
    parameters:
      my_entity:
        type: my_entity
  requirements:
    _permission: 'some permission defined in your module_name.permissions.yml file'

The _entity_form is composed of two parts: before the dot, you have the entity type id, in this example my_entity. Behind the dot, you have the name of the form, defined in the handlers section of the @ContentEntityType annotation. In this case, the second part is default, According to your question's code, this refers to \Drupal\my_entity\Form\MyEntityForm. You might not need the route_callbacks code, as a static routing directly in the yml file is probably enough.

For more details, see https://www.drupal.org/docs/8/api/routing-system/structure-of-routes.

1

You can take a look into core/modules/system/tests/modules/entity_test/src/EntityTestForm.php file.

And you can see that in the object that is an instance of ContentEntityForm it possible to get an entity object using $entity property:

$entity = $this->entity;
$entity = $this->getEntity();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.