Attached patch fixes the following todo in Drupal\eck\Entity\EckEntity:

    // The following base fields are set only if the user selected them. In the
    // future we need to find a better solution for defining this base fields.
    // @todo: Find a dynamic way to add base fields.

It does this by making the created, changed, uid and title field optional. The langcode field has been left mandatory because everything in core should be translatable, the same should be true for ECK entities. The patch covers parts of the same code as #2603522: Cannot import Eck Type Definition so one of the two will need to be rerolled after commit of the other.

The patch also contains some small unrelated changes which I will explain below.

@@ -47,7 +47,7 @@ class EckEntityListBuilder extends EntityListBuilder {
     /* @var $entity \Drupal\eck\Entity\EckEntity */
     $row['id'] = $entity->id();
     $row['title'] = \Drupal::l(
-      $this->getLabel($entity),
+      $entity->label(),
       Url::fromRoute(
         'entity.' . $this->entityTypeId . '.canonical',
         array(

getLabel() is deprecated and the advised resolution is to call label() on the entity. Since I've also had to create EckEntity::label() function to prevent missing label fields from causing trouble this also improves readability, which makes it an acceptable change in my opinion.

@@ -97,89 +103,94 @@ class EckEntity extends ContentEntityBase implements EckEntityInterface {
     $fields['langcode'] = BaseFieldDefinition::create('language')
       ->setLabel(t('Language'))
       ->setDescription(t('The language code of the entity.'))
       ->setTranslatable(TRUE)
-      ->setDisplayOptions('view', array(
-        'type' => 'hidden',
-      ))
-      ->setDisplayOptions('form', array(
+      ->setDisplayOptions('view', ['type' => 'hidden',])
+      ->setDisplayOptions('form', [
         'type' => 'language_select',
         'weight' => 2,
-      ))
+      ])
       ->setDisplayConfigurable('view', TRUE);

This is a pure code-style change to make the entire class match the same code style using short-hand array syntax as per Drupal's coding standards.

--- a/src/Form/EntityType/EckEntityTypeFormBase.php
+++ b/src/Form/EntityType/EckEntityTypeFormBase.php
         ['%label' => $this->entity->label(), 'link' => $edit_link]
       );
     }
     else {
-      // Clear the cache to get the new defined entities.
-      // @todo: Find better solution for this.
-      drupal_flush_all_caches();
+      // Clear the cached definitions to get the newly defined entities.
+      $this->entityTypeManager->clearCachedDefinitions();
+      // Clear the routing cache because new entity types define new routes.
+      \Drupal::service("router.builder")->rebuild();
       // Get the entity manager and check for entity definitions.
       // For some reason it is not working with this->entityManager.
       // @todo: find out what is wrong with that.

This change is not necessary to fix the issue, but since I had to solve the cache-clearing issue for the if part of this if--else block, I might as well implement it in the else part.

CommentFileSizeAuthor
allow-dynamic-base-fields.patch13.01 KBlegolasbo
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

legolasbo created an issue. See original summary.

norman_pixelkings’s picture

Patch works great! But you have to re-enable the title fields on the entities you have used. Better is to enable the field which are used by default.

legolasbo’s picture

Status: Needs review » Closed (duplicate)

Closing this issue as a duplicate and uploading revised patch to #2558143: Opt-out from optional base fields.