0

I have a custom entity, BaseEstablishment which is an abstract base class, implemented by Practice.

abstract class BaseEstablishment extends ContentEntityBase implements EntityOwnerInterface {
    // The necessary functions required for get/set methods
    // The baseFieldDefinitions method
    public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
        $fields = parent::baseFieldDefinitions($entity_type);
        // Standard field, used as unique if primary index.
        $fields['id'] = BaseFieldDefinition::create('integer')
              ->setLabel(t('ID'))
              ->setDescription(t('The ID of the entity.'))
              ->setReadOnly(TRUE);
        // Standard field, unique outside of the scope of the current project.
        $fields['uuid'] = BaseFieldDefinition::create('uuid')
              ->setLabel(t('UUID'))
              ->setDescription(t('The UUID of the entity.'))
              ->setReadOnly(TRUE);
       /*=====================================================*/
       // Name field for the establishment (vc 256)
       $fields['name'] = BaseFieldDefinition::create('string')
          ->setLabel(t('Name'))
          ->setDescription(t('Name of the establishment.'))
          ->setRequired(TRUE)
          ->setSettings([
                  'default_value' => '',
                  'max_length' => 256,
                  'text_processing' => 0,
          ])
          ->setDisplayOptions('view', [
                'label' => 'above',
                'type' => 'string',
                'weight' => -5,
          ])
          ->setDisplayOptions('form', [
                'type' => 'string_textfield',
                'weight' => -5,
          ])
          ->setDisplayConfigurable('form', TRUE)
          ->setDisplayConfigurable('view', TRUE);
        $fields['affiliated_users'] = 

        BaseFieldDefinition::create('entity_reference')
          ->setLabel(t('Affiliated Users'))
          ->setDescription(t('The users allowed to manage this     establishment.'))
          ->setRevisionable(FALSE)
          ->setRequired(FALSE)
          ->setSettings([
                  'target_type' => 'user',
                  'default_value' => 0,
                ]
              )
          ->setCardinality(-1)
          ->setDisplayOptions('view', [
                'label' => 'hidden',
                'type' => 'author',
                'weight' => 0,
          ]);
    // Rest of fields that I need
    return $fields;
    }
}

/**
* Defines the practice entity class.
*
* There are many more properties to be used in an entity type definition. For
* a complete overview, please refer to the '\Drupal\Core\Entity\EntityType'
* class definition.
*
* @ingroup practice
*
* @ContentEntityType(
*   id = "myentity_practice",
*   label = @Translation("Practice"),
*   label_collection = @Translation("Practice"),
*   label_singular = @Translation("Practice"),
*   label_plural = @Translation("Practices"),
*   label_count = @PluralTranslation(
*     singular = "@count practice",
*     plural = "@count practices"
*   ),
*   handlers = {
*     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
*     "list_builder" = "Drupal\myentity\Entity\Controller\PracticeListBuilder",
*     "form" = {
*       "add" = "Drupal\myentity\Entity\Form\PracticeForm",
*       "edit" = "Drupal\myentity\Entity\Form\PracticeForm",
*       "delete" = "Drupal\myentity\Entity\Form\PracticeDeleteForm",
*     },
*   },
*   base_table = "_practice",
*   data_table = "myentity_practice_data",
*   fieldable = FALSE,
*   translatable = FALSE,
*   entity_keys = {
*     "id" = "id",
*     "uuid" = "uuid",
*     "owner" = "uid",
*   },
*   links = {
*     "canonical" = "/practice/{id}",
*     "delete-form" = "/practice/{id}/delete",
*     "edit-form" = "/practice/{id}/edit",
*     "collection" = "/practice/list"
*   },
*   field_ui_base_route = "myentity.admin.entity.practice.main",
* )
*/    
class Practice extends BaseEstablishment implements PracticeInterface {
    // Any changes that are required for the practice entity.
    // The PracticeInterface extends the EstablishmentInterface, 
    // which exposes all the required get/set methods.
    // There aren't many changes that happen here;
    // I've commented out everything in this file
    // and the issue still persists.
}

class PracticeForm extends ContentEntityForm {
    public function buildForm(array $form, FormStateInterface $form_state) {
        $form = parent::buildForm($form, $form_state);
        /**
         * This is always null, 
         * regardless of whether it is accessed via: 
         * /practice/add or /practice/{id}/edit
         */
        $entity = $this->entity;

        if (is_null($entity->id()) {
            $form['#title'] = $this->t("Add Practice");
        } else {
            // $entity->getName() is a getter method defined by the practice interface.
            $form['#title'] = $this->t("Edit " . $entity->getName());
        }

        // rest of customization to the form that are dependent upon the conditional above.

        return $form;
    }
}

The above is the PHP code that drives the entity. I'm not sure if it's necessary, but the routing file is below:

#########################################################
# Practice Admin/User paths
#########################################################
myentity.admin.entity.practice.main:
  path: '/admin/content/practice'
  defaults:
    _entity_list: 'myentity_practice'
    _title: 'Practice List'
  requirements:
    # Checks for permission directly.
    _permission: 'administer site'

myentity.user.entity.practice.form.add:
  path: '/practice/add'
  defaults:
    # Calls the form.add controller, defined in the practice entity.
    _entity_form: 'myentity_practice.add'
    _title: 'Add Practice'
  requirements:
    _permission: 'administer site'

myentity.user.entity.practice.form.edit:
  path: '/practice/{id}/edit'
  defaults:
    # Calls the form.edit controller, defined in the practice entity.
    _entity_form: 'myentity_practice.edit'
    _title: 'Edit Practice'
  requirements:
    _permission: 'administer site'
#    _permission: 'authenticated user'

myentity.user.entity.practice.form.delete:
  path: '/practice/{id}/delete'
  defaults:
    # Calls the form.delete controller, defined in the practice entity.
    _entity_form: 'myentity_practice.delete'
    _title: 'Delete Practice'
  requirements:
    _permission: 'administer site'

The problem is that when I add a practice through the form: /practice/add, it correctly stores it in the database. However, when I go to: practice/1/edit, the entity is always null, I can see it in the database and it is listed on PracticeListBuilder page.

As for trying to figure out what the issue is: I've tried minimizing the total fields added -- removing the EntityOwner and my custom affiliated users field. I don't understand why the entity is always null when trying to edit the entity.

  • The parameter should be the machine name of the entity, then it will be upcast to an entity object. – Kevin Jun 10 at 18:22
  • ...alright then. That's just embarrassing. If you'd be so kind to add that as an answer, I can give you due credit. – lilott8 Jun 11 at 4:23
1

The parameter should be the machine name of the entity, then it will be upcast to an entity object.

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.