0

While enabling my custom module that creates a custom content entity through Drush, I get the following error:

[error] Drush command terminated abnormally due to an unrecoverable error. Error: Declaration of Drupal\staff_profile\Entity\StaffProfile::preCreate() must be compatible with Drupal\Core\Entity\EntityInterface::preCreate(Drupal\Core\Entity\EntityStorageInterface $storage, array &$values) in /Users/user/Sites/drupal8staff/web/modules/custom/staff_profile/src/Entity/StaffProfile.php, line 54

Line 54 corresponds to the class StaffProfile in the following:

<?php
namespace Drupal\staff_profile\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Druapl\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;
use Drupal\staff_profile\StaffProfileInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityPublishedTrait;

/**
 * Defines staff_profile entity class
 *
 *  @ingroup staff_profile
 *  @ContentEntityType(
 *    id = "staff_profile_profile",
 *    label = @Translation("Staff Profile"),
 *    handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\content_entity_example\Entity\Controller\StaffProfileListBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "form" = {
 *       "add" = "Drupal\content_entity_example\Form\StaffProfileForm",
  *       "edit" = "Drupal\content_entity_example\Form\StaffProfileForm",
  *       "delete" = "Drupal\content_entity_example\Form\StaffProfileDeleteForm",
  *     },
  *     "access" = "Drupal\content_entity_example\StaffProfileAccessControlHandler",
  *   },
  *    base_table = "staff_profile_entity",
  *    admin_permission = "administer staff profile entity",
  *    fieldable = TRUE,
  *    links = {
  *      "canonical" = "/people/{staff_profile_profile}",
  *      "add-page" = "/people/add",
  *      "edit-form" = "/people/{staff_profile_profile}/edit",
  *      "delete-form" = "/people/{staff_profile_profile}/delete",
  *      "collection" = "/people/list",
  *    },
  *    entity_keys = {
  *      "id" = "id",
  *      "uuid" = "uuid",
  *      "label" = "usernme",
  *      "published" = "status",
  *    },
  *    field_ui_base_route = "staff_profile.staff_profile_settings",
  *  )
  *
 */
 class StaffProfile extends ContentEntityBase implements StaffProfileInterface, EntityPublishedInterface {
   use EntityChangedTrait;

   /**
   * {@inheritdoc}
   * Set computed fields when creating a new Staff Profile
   */
   public static function preCreate(EntityStorageInterface $storage, array &$values) {
     parent::preCreate($storage, $values);
     $values += array(
       'user_id' => \Drupal::currentUser()->id(),
     );

// Methods omitted for brevity

   }

   /**
   * {@inheritdoc}
   *
   * Creates Fields and properties
   * Defines gui behavior
   */
   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
     $fields['id'] = BaseFieldDefinition::create('integer')
       ->setLabel(t('ID'))
       ->setReadOnly(TRUE);

     $fields['uuid'] = BaseFieldDefinition::create('uuid')
       ->setLabel(t('UUID'))
       ->setReadOnly(TRUE);


 //Fields omitted for brevity


      $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
          ->setLabel(t('User Name'))
          ->setSettings(array(
            'target_type' => 'user',
            'handler' => 'default',
          ))
          ->setDisplayOptions('view', array(
            'label' => 'hidden',
            'type' => 'entity_reference_label',
            'weight' => -3,
          ))
          ->setDisplayOptions('form', array(
            'type' => 'entity_reference_autocomplete',
            'settings' => array(
              'match_operator' => 'CONTAINS',
              'size' => 60,
              'autocomplete_type' => 'tags',
              'placeholder' => '',                 ),
          ))
          ->setDisplayConfigurable('form', TRUE)
          ->setDisplayConfigurable('view', TRUE);
      }
  }

StaffProfileInterface.php contents:

<?php
namespace Drupal\staff_profile;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\Core\Entity\EntityChangedInterface;

/**
 * Provides an interface defining a StaffProfile entity.
 * @ingroup staff_profile
 *
 */

interface StaffProfileInterface extends ContentEntityInterface, EntityOwnerInterface, EntityChangedInterface {
}

The preCreate method is the exact same as what can be found on the example for content entities in the Drupal 8 entity API Docs. I was able to successfully install a copy of the example content entity, but not my entity. The example entity requires the examples module, is there something in that module that I am missing in mine or was this caused by an error in my implementation?

1 Answer 1

0

Your problem is most likely this statement:

use Druapl\Core\Entity\EntityStorageInterface;

Drupal is spelled incorrectly. Thus it's not using the correct class in the type hint and thinking it's an incorrect implementation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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