3

I have installed custom module.

Then created entity: content type with drupal generate:entity:content

Now, I'm trying to update the module, so that the entity is installed, but no clue.

Since code is looking for entity table, I cannot even uninstall the module.

I also checked in http://domain.com/admin/modules/update but says no update available.

I even tried changing module version in .info.yml but that seems not working.

What is the best way to let drupal know that my module needs update?

UPDATE 1

My RewardNotification.php

<?php

namespace Drupal\reward\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;

/**
 * Defines the Reward notification entity.
 *
 * @ingroup reward
 *
 * @ContentEntityType(
 *   id = "reward_notification",
 *   label = @Translation("Reward notification"),
 *   bundle_label = @Translation("Reward notification type"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\reward\RewardNotificationListBuilder",
 *     "views_data" = "Drupal\reward\Entity\RewardNotificationViewsData",
 *
 *     "form" = {
 *       "default" = "Drupal\reward\Form\RewardNotificationForm",
 *       "add" = "Drupal\reward\Form\RewardNotificationForm",
 *       "edit" = "Drupal\reward\Form\RewardNotificationForm",
 *       "delete" = "Drupal\reward\Form\RewardNotificationDeleteForm",
 *     },
 *     "access" = "Drupal\reward\RewardNotificationAccessControlHandler",
 *     "route_provider" = {
 *       "html" = "Drupal\reward\RewardNotificationHtmlRouteProvider",
 *     },
 *   },
 *   base_table = "reward_notification",
 *   admin_permission = "administer reward notification entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "bundle" = "type",
 *     "label" = "entity_type",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "read" = "read",
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/reward_notification/{reward_notification}",
 *     "add-page" = "/admin/structure/reward_notification/add",
 *     "add-form" = "/admin/structure/reward_notification/add/{reward_notification_type}",
 *     "edit-form" = "/admin/structure/reward_notification/{reward_notification}/edit",
 *     "delete-form" = "/admin/structure/reward_notification/{reward_notification}/delete",
 *     "collection" = "/admin/structure/reward_notification",
 *   },
 *   bundle_entity_type = "reward_notification_type",
 *   field_ui_base_route = "entity.reward_notification_type.edit_form"
 * )
 */
class RewardNotification extends ContentEntityBase implements RewardNotificationInterface {

  use EntityChangedTrait;

  /**
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
    $values += array(
      'user_id' => \Drupal::currentUser()->id(),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getType() {
    return $this->bundle();
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityType() {
    return $this->get('entity_type')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setEntityType($entity_type) {
    $this->set('entity_type', $entity_type);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityId() {
    return $this->get('entity_id')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setEntityId($entity_id) {
    $this->set('entity_id', $entity_id);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  function getValue() {
    return $this->get('value')->value;
  }

  /**
   * {@inheritdoc}
   */
  function setValue($value) {
    return $this->set('value', $value);
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwner() {
    return $this->get('user_id')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwnerId() {
    return $this->get('user_id')->target_id;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $this->set('user_id', $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this->set('user_id', $account->id());
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getRead() {
    return (bool) $this->getEntityKey('read');
  }

  /**
   * {@inheritdoc}
   */
  public function setRead($read) {
    $this->set('read', $read ? TRUE : FALSE);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Authored by'))
      ->setDescription(t('The user ID of author of the Reward notification entity.'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setTranslatable(TRUE)
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'author',
        'weight' => 0,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'entity_reference_autocomplete',
        'weight' => 5,
        'settings' => array(
          'match_operator' => 'CONTAINS',
          'size' => '60',
          'autocomplete_type' => 'tags',
          'placeholder' => '',
        ),
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

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

    $fields['type'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Type'))
      ->setDescription(t('The notification type.'))
      ->setSetting('target_type', 'reward_notification_type')
      ->setReadOnly(TRUE);

    $fields['entity_type'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Entity Type'))
      ->setDescription(t('The type from the notified entity.'))
      ->setDefaultValue('node')
      ->setSettings(array(
        'max_length' => 64
      ))
      ->setRequired(TRUE);

    $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Notified entity'))
      ->setDescription(t('The ID from the notified entity'))
      ->setDefaultValue(0)
      ->setRequired(TRUE);

    $fields['value'] = BaseFieldDefinition::create('float')
      ->setLabel(t('Value'))
      ->setDescription(t('The numeric value of the notification.'))
      ->setDefaultValue(0)
      ->setRequired(FALSE);

      $fields['read'] = BaseFieldDefinition::create('boolean')
          ->setLabel(t('Has user viewed notification'))
          ->setDescription(t('A boolean indicating whether the Notification is visited.'))
          ->setDefaultValue(FALSE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the entity was created.'));

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the entity was last edited.'));

    return $fields;
  }

}
  • Do you try my answer? I don't know why you want install a new module and then create in this module a new entity and wait to drupal know about this. – Adrian Cid Almaguer May 3 '17 at 13:48
  • I just see your scenario when I'm trying to create a new module, I have installed the module but I forget to create one or more entities, then I create the entities and run drush updatedb --entity-updates and that its. – Adrian Cid Almaguer May 3 '17 at 13:53
2

There are a few different ways to do this and since I'm not completely sure of your setup I'm listing all of them here for you but #3 below is probably what you want for your situation. There are so many ways to create entities now that I figured listing the different ways may help you or someone in the future.

  1. This is probably not what you want but I'm adding it here because it's fairly easy to write simple updates with this method. I'm not entirely sure if I understand you correctly, but if you are trying to or need to run an update in a custom module, you need to have an update hook present with the needed updates for your module. So you can use hook_update_N(). It is a common Drupal task to have updates present in modules and if building custom modules for your application/site a good way to ensure changes are applied to each environment (Dev, Test, Prod, Local) as needed. hook_update_N() integrates with the update system and the 'N' represents the minor version changes, so that when the update command is ran, Drupal looks for these hooks and runs the version that it needs to run at that time. Usually you would start with MODULENAME_update_8001() or something similar and future changes you will increment the number each time and the update will be picked up each update command that is issued. So that will give you a basic reason primer in making basic updates, you can run just about any db updates this way but like I said, this is probably just extra information.

  2. You could use the configuration system to make entity field changes and run config updates. This is a very basic way and will not be very usable for a module that needs to run on multiple sites.

  3. If you are registering your entity in your module and you have your schema declared in src/Entity/ENTITYNAME.php this is the file that controls the schema and upon initial install of the module the tables are setup. If you enabled the module before you added your entity schema file fret not, you can use drush updatedb --entity-updates or drupal update:entities to get the new entity or any updates that you have made to the entity throughout development or while deploying changes. An example in the documentation can be found on Creating a content entity type in Drupal 8. Just read through that example and it will give you any other information you need regarding this issue.

Also, you said you created the entity. I'm guessing you did that through the command line, but you want to automate that task. I gave you a link that guides you in adding the entity information to your module in the third point. Specifically, look at the src/Entity/Contact.php section and you will need to have that present, then run the entity update command to register the entity. You may have to rebuild the cache during that process so that Drupal will register the new Entity Class. This is a basic entity manager task; once you do this, you will never go back to any other way. See also Entity Cheat Sheet.

|improve this answer|||||
  • On drupal update:entities I received this error: Error on Entity Updates Array. Any reason? – Adarsh Khatri May 3 '17 at 10:35
  • Can we see your src/Entity/ENTITY_NAME.php file? It would probably help solve this issue – burnsjeremy May 3 '17 at 11:00
  • updated. Please check. – Adarsh Khatri May 3 '17 at 11:02
  • Finally it ran with drush updatedb --entity-updates. Not sure about drupal update:entities. I guess drush is more powerful than drupal. – Adarsh Khatri May 3 '17 at 11:15
  • I've had an issue similar to that where I have to do drush cr, drush updatedb and then drush entup before the change would take affect so that sounds about right, glad it ran for you! – burnsjeremy May 3 '17 at 11:18
3

You can use:

drush updatedb --entity-updates

--entity-updates

Run automatic entity schema updates at the end of any update hooks.

If you have changed any entity/field definitions in your custom module you can quickly apply this to update.

|improve this answer|||||

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.