RouteNotFoundException when trying to delete a custom entity

I am building a custom component in Drupal 8 that has an entity with child entities i.e., TeamEntity has one or more MeetEntity objects.

When I navigate to the “Delete” link for a given MeetEntity, I get the following error:

The website encountered an unexpected error. Please try again later.  Symfony\Component\Routing\Exception\RouteNotFoundException: Route "entity.meet_entity.collection" does not exist. in Drupal\Core\Routing\RouteProvider->getRouteByName() (line 191 of core/lib/Drupal/Core/Routing/RouteProvider.php). Drupal\Core\Routing\UrlGenerator->getRoute('entity.meet_entity.collection') (Line: 285) Drupal\Core\Routing\UrlGenerator->generateFromRoute('entity.meet_entity.collection', Array, Array, 1) (Line: 110) Drupal\Core\Render\MetadataBubblingUrlGenerator->generateFromRoute('entity.meet_entity.collection', Array, Array, 1) (Line: 754) Drupal\Core\Url->toString(1) (Line: 161) Drupal\Core\Utility\LinkGenerator->generate(Object, Object) (Line: 99) Drupal\Core\Render\Element\Link::preRenderLink(Array) call_user_func(Array, Array) (Line: 384) Drupal\Core\Render\Renderer->doRender(Array) (Line: 451) Drupal\Core\Render\Renderer->doRender(Array) (Line: 451) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200) Drupal\Core\Render\Renderer->render(Array) (Line: 471) Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 43) __TwigTemplate_4a2ed85f2bf4ceab0412cdb1168c80023104264819c7f1db9b016550543c33e9->doDisplay(Array, Array) (Line: 381) Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355) Twig_Template->display(Array) (Line: 366) Twig_Template->render(Array) (Line: 64) twig_render_template('core/themes/classy/templates/form/confirm-form.html.twig', Array) (Line: 389) Drupal\Core\Theme\ThemeManager->render('confirm_form', Array) (Line: 438) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200) Drupal\Core\Render\Renderer->render(Array, ) (Line: 231) Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 577) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 232) Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 122) Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 95) Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 116) Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 144) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 62) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 53) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 103) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 82) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 51) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 55) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 632) Drupal\Core\DrupalKernel->handle(Object) (Line: 19) 

I have a “collection” link defined in my MeetEntity class like this:

 *   links = {  *     "canonical" = "/meet/{meet_entity}",  *     "add-form" = "/team/{team_entity}/meet/add",  *     "edit-form" = "/meet/{meet_entity}/edit",  *     "delete-form" = "/meet/{meet_entity}/delete",  *     "collection" = "/team/{team_entity}/meets",  *   }, 

I don’t have that route defined in my routing.yml file because that “collection” path defined in the links in my Entity class points to a view called “views.view.meets.yml”. However, when I do add a route for that in my routing.yml file, I get a MissingMandatoryParametersException on the {team_entity}

I imagine I need to override some method somewhere but I can’t figure out where. Can someone please help?

What is an entity (with specific examples)?

Which of these classes after their creation will be called – entity?

  1. PointStaticUtils – static class
  2. Point – class with public const field and public method for calc coords by using PointStaticUtils
  3. DogBone – value object (VO) class with fields: fron\back left\right dog paw
  4. DogPaw – class accepts parameters in constructor instance DogBone and implements methods: moveShoulder(int angle), moveElbow(int angle), moveWrist(int angle)
  5. GoService, StopService, SitService – classes implements method run in which calculated is angle
  6. MoveController – class accepts parameters in constructor Map and that class receives di instances of service classes. implements methods: go(), stop(), sit() – using services and moving paws
  7. Dog – class encapsulates creation and initialization of class MoveController and implements method: go(), stop(), sit() encapsulating work with the controller MoveController
  8. DogEvent – class event sent by class Dog

Clean Architecture and Entity Framework

I faced few problems when generating database schema from business objects with EF Core:

  • No support for struct properties
  • No support for interface properties
  • I need additional columns that should belong to Data layer only.

I don’t want to change my business layer (structs/interfaces to classes) just because of EF as this goes against clean architecture principles (framework should serve your application not the other way around). Also adding persistence specific columns to the business layer is out of question.

I suppose the right thing to do is to add persistence objects to persistence layer which would represent entities in the business layer. On top of that some mappers would have to be used by business layer to abstract persistence objects mapping them to the business entities.

Few question about this approach:

  • Is this a correct thing to do? Are there any alternatives? For example: EF configuration capabilities that would help me to deal with the issues listed above without replicating entities in the persistence layer?
  • This feels like a lot of boilerplate code and replication of same properties. Seems like this code could be auto-generated by some tool, does something like this exist?
  • Is there a good example of how such approach should look?

Entity Framework Core – may cause cycles or multiple cascade paths error

I was creating some models and this error appeared while i was configuring fluent API and added migration. I don’t understand why it tells me this when i set DeleteBehaviour.Restrict ?

public class Registo {     public int CelulaId { get; set; }     public Celula Celula { get; set; }      public int ReferenciaId { get; set; }     public Referencia Referencia { get; set; }      public ICollection<ComponenteArranque> Componentes { get; set; }     public ICollection<RegistoGeometria> RegistosGeometria { get; set; } }  [Table("hGeometrias")] public class Geometria {     public int Id { get; set; }     public string Referencia { get; set; }     public int MatrizCelulaReferenciaId { get; set; }     public MatrizCelulaReferencia Matriz { get; set; }      public ICollection<RegistoGeometria> RegistosGeometria { get; set; } }  [Table("hRegistosGeomtrias")] public class RegistoGeometria {     public int Id { get; set; }      public float X1 { get; set; }     public float X2 { get; set; }     public float X3 { get; set; }     public float X4 { get; set; }      public int RegistoId { get; set; }     public Registo Registo { get; set; }      public int GeometriaId { get; set; }     public Geometria Geometria { get; set; } } 

fluet api

   modelBuilder.Entity<RegistoGeometria>()             .HasOne(rg => rg.Registo)             .WithMany(r => r.RegistosGeometria)             .OnDelete(DeleteBehavior.Restrict); 

Introducing FOREIGN KEY constraint ‘FK_hRegistosGeomtrias_hRegistos_RegistoId’ on table ‘hRegistosGeomtrias’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

Entity unique identifier in domain driven design

I’m just learning DDD and a question raised in my mind about unique identifiers in an entity.

Consider this simple method that checks the uniqueness of an entity to prevent duplication:

    private bool IsDuplicate(BoardGame boardGame)     {         //It's either this         if (Games.Any(x => x.Id == boardGame.Id)) return true;          //Or this         if (Games.Any(x => x.Name == boardGame.Name && x.Price == boardGame.Price)) return true;          return false;     } 

The first method is very clean and readable. The problem is we usually don’t have the value of Id before the item is persisted somehow because it is handled by the ORM.

The second method is the way how an object is considered unique from a domain point of view. We can evaluate this uniqueness before persisting the object But the downside is it gets messy when an entity’s uniqueness becomes more complicated.

Now the question is, isn’t that Id field redundant? don’t we have to check this uniqueness with domain perspective anyway? why not having, say, a composite/compound key in ORM/Persistence side?

Of course this is an example. The name or price could change, but remember, I said that in the domain perspective, the name should be unique.

Consider having a Member entity, with his/her SSN as a unique identifier.

How to customize entity print pdf

I’ve installed Entity Print in order to create PDF files. I’ve read the documentation to enable a twig template to customize the pdf output, but I don’t know how to do simple things, in the twig file, like add a page header and footer, add a front page, etc. Are there any docs about how to customize it?

Why do I get “Type does not correspond to an entity on this site” when POSTing to REST api behind an Apache Reverse proxy?

I have the REST api configured to allow POSTing new users to /entity/user. On my local machine, it works just fine. Once I push up to my remote host, it stops working.

The key difference is that on my local machine, my site is running in a docker container bound to port 6050, so all rest calls go to localhost:6050.

On my remote host, I have docker configured behind an Apache 2.4 reverse proxy. So the docker container is still bound to 127.0.0.1:6050, but all requests should go to https://sub.example.tld/.

To summarize, posting a new user to http://localhost:6050/entity/user?_format=hal_json, with the appropriate X-CSRF-Token and Content-Type headers set, works. It returns 200.

Posting to https://sub.example.tld/entity/user?_format=hal_json, with the appropriate X-CSRF-Token and Content-Type headers set, does not. It returns 422 Unprocessable Entity.

{"message":"Type https:\/\/sub.example.tld\/rest\/type\/user\/user does not correspond to an entity on this site."} 

I do manually set the user’s “_links” array:

"_links": {         "type": {           "href": "https://sub.example.tld/rest/type/user/user"         }       }, 

I know that authentication isn’t the issue. My code is successfully accessing views that require authentication.

Since I think it might be something to do with these settings, I do have the following set:

  • $ settings['trusted_host_patterns'] = ["$ sub\.example\.tld$ "]
  • $ settings['reverse_proxy'] = TRUE;
  • $ settings['reverse_proxy_addresses'] = ["internal network ip","172.18.0.1","127.0.0.1"];

I’m running Drupal 8.7.3.

My docker image is based on php:7.1-apache.

Any ideas what I’m doing wrong?

Mismatched entity and/or field definitions for comment field

I have been receiving this message

ENTITY/FIELD DEFINITIONS Mismatched entity and/or field definitions The following changes were detected in the entity type and field definitions. Comment The Entity type field needs to be updated. The Comment field name field needs to be updated.

I have tried quite a lot of things including running entup (apparently depreciated) and adding some lines on entity update to index.php. Nothing has worked, I don’t really use comments on my site, but would like to resolve it, any ideas?

How can I read config schema from a config entity?

I need to read the schema definition from a config entity, which is located in config/schema. For example the schema from a node type defined in node.schema.yml:

node.type.*:   type: config_entity   label: 'Content type'   mapping:     name:       type: label       label: 'Name'     type:       type: string       label: 'Machine-readable name'     description:       type: text       label: 'Description'     help:       type: text       label: 'Explanation or submission guidelines'     new_revision:       type: boolean       label: 'Whether a new revision should be created by default'     preview_mode:       type: integer       label: 'Preview before submitting'     display_submitted:       type: boolean       label: 'Display setting for author and date Submitted by post information' 

How can I do this?

Clean code for multiple entity representations

I’m wondering what is a clean solution for representing an entity in many different format. Let’s suppose I have the following entity:

Person {     Long id;     String title;     String firstName;     String lastName;     Date birthDate;     List contacts;     Address address;     ...     Date joinDate;     Date lastLoginDate;     Status status; } 

And I have a web application where I need to display different data from the Person entity depending on where it is needed. For example:

  • Public GUI header: firstName, city(from address), email(from contacts)
  • Public GUI profile: title, firsName, lastName, birthDate, contacts, address
  • Admin persons list: id, fullName, birthDate, joinDate, lastLoginDate, status
  • Admin person edit: all the fields
  • REST API: id, title, firstName, lastName, age(as int and not birthDate), address
  • Report: id, fullName, fullAddress, birthDate, login attempts (not part of the main entity, get from other service), audit logs (not part of the main entity, get from other service)

In general we know the person’s id and can get the entity like personRepository.getById()

I can just send the Person entity to the various representation layers and get what it needs, but in this case I have to repeat formatting like fullName concatenation, I can accidentally expose data in case of the REST API, in case of list pages it can cause performance degradation, and so on.

Or, I can create unique representation for each case so in this example:

  • PersonPublicHeaderViewData
  • PersonPublicProfileViewData
  • PersonAdminListViewData
  • PersonAdminEditViewData
  • PersonRestViewData
  • PersonReportViewData

In this case I also need some factories that can build the views from the original entity:

  • PersonPublicViewFactory: creates header and profile views
  • PersonAdminViewFactory: creates admin list and edit views
  • PersonRestViewFactory: creates the REST view
  • PersonReportViewFactory: create the report view with the additional external data

The second solution seems to be cleaner but requires a lot of extra classes.

What do you think? Any other idea?