1

New Drupal Developer here.

I am currently working on a inventory management module for a friend. And I ran into a bit of a snag with Custom Entities in Drupal 8.

I have a custom "Customer" entity content type which stores various bits of information about the customer, first name, last name, a number which we assign each customer, etc...

When I added a new field post-module-install, after running update.php, cron, etc... The new field was never added to the existing SQL table.

I am wondering if there is something I'm missing, like a way to force update the database without having to manually add the new table.

Here is the new field I added.

$fields['l_number'] = BaseFieldDefinition::create('string')
        ->setLabel(t('L#'))
        ->setDescription(t('The Customer\'s L Number.'))
        ->setSettings(array(
            'default_value' => '',
            'max_length' => 8,
            'text_processing' => 0,
        ))
        ->setDisplayOptions('view', array(
            'label' => 'above',
            'type' => 'string',
            'weight' => -7,
        ))
        ->setDisplayOptions('form', array(
            'type' => 'string',
            'weight' => -7,
        ))
        ->setDisplayConfigurable('form', TRUE)
        ->setDisplayConfigurable('view', TRUE);
0

Using hook_update_N() as linked in @cilefen's answer is way to do it when you have an existing sites with data and want it to make easier for users to run your updates.

If you're doing the initial development on a development environment, then I'd consider just re-installing the module (that's pretty fast with drush pmu -y module; drush en -y module).

You can also try drush entity-updates. If you don't have/know drush yet, you're missing out: https://github.com/drush-ops/drush

| improve this answer | |
  • 1
    Does drush entity-updates apply to Content Types (aka Node Bundles) as well? – David Riccitelli May 5 '16 at 14:23
  • No. config entity have no update system, there are no database tables. If you need to make updates there, either write the code so it works with the old structure too or write an update function that loads them all and updates accordingly. – Berdir May 5 '16 at 14:43
0

There are published examples using hook_update_n().

| 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.