Comments

joachim’s picture

Version: » 7.x-1.x-dev
Priority: Normal » Major

Upping this. Would be good to have for OOTB hats and party demo site.

joachim’s picture

Issue tags: +crm get involved

Tagging.

This is a fairly easy one:

- add a field to hook_schema()
- update function
- add form element for it to the hat form

rerooting’s picture

Working on this. This is a really amateur question - what does the update function need to accomplish? I have the other things taken care of. I am assuming it's a function that would add the field column to the table when you run update.php, but I'm too green to know where to look to accomplish this embarassingly simple task.

joachim’s picture

You can look at the patch for adding a parent field to hats. Or most core and contrib modules will have at least one update function that adds a field to a table.

rerooting’s picture

StatusFileSize
new1.21 KB

Ok here it is so far. I'm still looking around for a simple 'added a new field to this entity so I need this simple update hook example'.

rerooting’s picture

StatusFileSize
new1.65 KB

Maybe this is it? Borrowed from party label update function in party.install .

rerooting’s picture

On a secondary note, I should be describing these as party-hat-description-field or something of the like.

rerooting’s picture

oops! naming it 7005 would conflict with the hat parent field, it should be changed to 7006 or something thereafter. my bad.

joachim’s picture

Not change field, as we want to add one.

Be sure to check the API docs for functions you're not familiar with: http://api.drupal.org/api/drupal/includes!database!database.inc/function...

You want: http://api.drupal.org/api/drupal/includes!database!database.inc/function...

> oops! naming it 7005 would conflict with the hat parent field, it should be changed to 7006 or something thereafter. my bad.

Yup. But it doesn't matter for testing. I'll make sure the names are all ok when I commit.

But let's say this gets 7006 because it's simpler than #1613440: Give hats a parent hat which is optionally empty and therefore will probably land first.

A few other things in your patch:

- you don't need #required = FALSE
- a textfield should be given a max length (which is possible IIRC) so users don't enter more characters that can be saved
- check your indentation :)

rerooting’s picture

StatusFileSize
new1.58 KB

Ok, fixed, alongside the issue of using the new 'lenth' property in the schema ;)

Thanks for the friendly feedback

rlmumford’s picture

Do people think this should be a text-area rather than a text field? Or do we want to force these descriptions to be really short?

joachim’s picture

> Do people think this should be a text-area rather than a text field

Could be, I suppose. Taxonomy descriptions are text areas IIRC. But those are meant for public consumption.

But then again, you might want to give a complex description to a hat? If so, we could have a textarea but then it can't be a varchar 255.

But then if they're really long, they'll break the admin UI. What I had in mind here is a brief description. If you want longer, maybe add a textarea field in the Field admin?

rlmumford’s picture

Sweet that makes sense. Maybe there's a better name for this then? Like 'tagline' or something?

rerooting’s picture

I think 'description' works fine as a field name for a short description (see: Views UI). For more detail, you might want to use 'help' or 'summary'. This should be fine for now.

Also, with this length, we could potentially fit it into the 'Manage Hats' UI as an additional column? What about that? Might get a little scrunched on smaller displays. We could hide the machine name displays if we wanted to do that.

rlmumford’s picture

In the Content Types system it's a text area, and they fit it in in small grey writing under the Content Type name. Wouldn't it be better to follow this convention?

BTW, thanks for all the work you're putting in rerooting! It's good to have you on board!

joachim’s picture

> we could potentially fit it into the 'Manage Hats' UI as an additional column?

That's the plan :)

Rob's suggestion makes sense too.

rerooting’s picture

How about 'brief description'. If folks want a more detailed description they can add a field via the UI (which we are keeping, correct?)

joachim’s picture

Let's just call it 'description' in the schema and the UI too for now -- we can bikeshed over the UI string some more later....

rlmumford’s picture

Here's a patch with the description as a text area. I don't know which one we want to use.

Either way, I'd like this committed quick so we can get #1613440: Give hats a parent hat which is optionally empty in.

rerooting’s picture

I think we should keep it as a normal text field, since the plan is to put it into admin UI for hats. If we make it that long, it won't fit.

rlmumford’s picture

255 chars seems really short though, I was just basing it off of the Content types system that has a text area and can cope with longer descriptions. I'm happy to go with a text field for now - especially as for longer descriptions we can add a field. Once it's in we can always change it later.

I'll commit this now.

joachim’s picture

Now we're aiming towards stable releases, could we be a bit less hasty in committing when we're not yet reached what feels like a decision? :)

'description' => array(
        'description' => 'A brief description of this type.', 
        'type' => 'text', 
        'not null' => TRUE, 
        'size' => 'medium', 
        'translatable' => TRUE,
      ), 

I'd be happy to follow the example of node types.

rlmumford’s picture

Status: Active » Fixed
StatusFileSize
new1.66 KB

Cool! We're in! I made a couple of final tweaks, so the final patch is here. But we're all committed now!

Thanks rerooting! that's gone in under your name.

rlmumford’s picture

Oh man, I was committing this as you posted. We can roll back? Or make another issue?

I've got two big hunks of work waiting on hat parents though.

rlmumford’s picture

Majorly fast decision made on IRC to change this to following the node-types convention.

@rerooting sorry to mess this about a bit. We want to do it quick so we don't have to faff with more update functions. I tried to find you on IRC but couldn't - me and joachim normally hang out on #drupal-crm

rlmumford’s picture

Here's the final patch

rerooting’s picture

oh hey! we should hang out on #drupal-crm, im ususally available as rerooting on #drupal-contrib when I am on IRC.

I was away from the drupal world working on a node.js/titanium project for the past few hours. I'd be glad to get myself and scresante on #drupal-crm regularly since we are becoming more involved with party.

Looks good though, want me to re-roll OOTB hats patch for this new field?

joachim’s picture

Yes please!

joachim’s picture

Category: feature » bug
Priority: Major » Critical
Status: Fixed » Active

The update totally killed my site:

Error message
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base.description' in 'field list': SELECT base.hid AS hid, base.name AS name, base.label AS label, base.description AS description, base.data AS data, base.required AS required, base.status AS status, base.module AS module FROM {party_hat} base WHERE (base.status IN (:db_condition_placeholder_0, :db_condition_placeholder_1, :db_condition_placeholder_2)) ; Array ( [:db_condition_placeholder_0] => 3 [:db_condition_placeholder_1] => 2 [:db_condition_placeholder_2] => 6 ) in EntityAPIController->query() (line 152 of /Users/joachim/Sites/7-drupal/sites/all/modules/contrib/entity/includes/entity.controller.inc).

Admittedly I've had to roll a few updates back and stuff, so I'll try it again on a clean install

rlmumford’s picture

Hmmm, I had that issue with the patch in number 10 - i assumed it was down to me having rolled back updates and stuff and the hook schema caching being really aggressive.

joachim’s picture

Slightly different error on a clean install:

Failed: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1101 BLOB/TEXT column 'description' can't have a default value: ALTER TABLE {party_hat} ADD `description` MEDIUMTEXT NOT NULL DEFAULT '' COMMENT 'Some text describing the purpose of the hat'; Array ( ) in db_add_field() (line 2812 of /Users/joachim/Sites/7-drupal/includes/database/database.inc).

rlmumford’s picture

Is that a SQL thing? Can TEXT columns not have defaults? If not, how can they be 'not null'?

rerooting’s picture

I think that the issue has to do with defaults.

http://drupalcode.org/project/drupal.git/blob/refs/heads/7.x:/modules/no... for example

joachim’s picture

> Note that type 'text' and 'blob' fields cannot have default values.

http://drupal.org/node/146939.

joachim’s picture

Status: Active » Fixed

Should be fixed.

Can someone test it?

rlmumford’s picture

Status: Fixed » Needs work

I just got this error again when running the update.

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base.description' in 'field list': SELECT base.hid AS hid, base.name AS name, base.label AS label, base.description AS description, base.data AS data, base.required AS required, base.status AS status, base.module AS module FROM {party_hat} base WHERE (base.status IN (:db_condition_placeholder_0, :db_condition_placeholder_1, :db_condition_placeholder_2)) ; Array ( [:db_condition_placeholder_0] => 3 [:db_condition_placeholder_1] => 2 [:db_condition_placeholder_2] => 6 ) in EntityAPIController->query() (line 152 of /home/deva/public_html/sites/all/modules/entity/includes/entity.controller.inc

rlmumford’s picture

Ok, so you only that error if you try and upgrade 7005 and 7006 at the same time. Because hook_schema has already been updated, the entity_load and party_hat_save methods both look for a description column when loading and saving the Party Hats.

However, only running 7006 gives a different error.

Update #7006
Failed: PDOException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'description' at row 1: ALTER TABLE {party_hat} CHANGE `description` `description` MEDIUMTEXT NOT NULL COMMENT 'Some text describing the purpose of the hat'; Array ( ) in db_add_field() (line 2809 of /home/deva/public_html/includes/database/database.inc).

I guess this is because we have a NOT NULL text field, but then don't put a value in it (for all the hats that already exist in the database) I guess the simple solution is to make description not NOT NULL.

joachim’s picture

Urgh.

> the simple solution is to make description not NOT NULL.

Yup.

> Ok, so you only that error if you try and upgrade 7005 and 7006 at the same time

Let's file a different issue for that.

ARGH party is broken :(

joachim’s picture

Huh?

node_schema:

'description' => array(
        'description' => 'A brief description of this type.', 
        'type' => 'text', 
        'not null' => TRUE, 
        'size' => 'medium', 
        'translatable' => TRUE,

So how does that manage to be NOT NULL and not have a default???

joachim’s picture

Hang on...

> CHANGE `description` `description` MEDIUMTEXT NOT NULL

You sure you weren't re-running the update? Why would it do a CHANGE?
Normally that would be 'ALTER TABLE tablename ADD newfieldname ...'

rlmumford’s picture

> CHANGE `description` `description` MEDIUMTEXT NOT NULL

You sure you weren't re-running the update? Why would it do a CHANGE?
Normally that would be 'ALTER TABLE tablename ADD newfieldname ...'

http://api.drupal.org/api/drupal/includes%21database%21mysql%21schema.in...

When something is 'NOT NULL' drupal seems to always run that as a changeField later. I don't know why. This also explains why the field gets created when the update runs.

So how does that manage to be NOT NULL and not have a default???

I guess cos It's a core module they haven't had to worry about update hooks adding this field to a table that doesn't already exist.

rlmumford’s picture

Also, we just hit something similar when saving a Party Hat. We'll need to add something to the save() method to atleast set description as an empty string, otherwise you'll get errors on save. Alternatively, we can make description not NOT NULL

rerooting’s picture

Yeah, maybe not null => true is what we want.

See what our friends at profile2 did with a text field:

http://drupalcode.org/project/profile2.git/blob/refs/heads/7.x-1.x:/prof...

I don't think we can set a default value such as '' like we can with a varchar.

rlmumford’s picture

Following profile2's pattern seems like a good idea to me.

joachim’s picture

Which profile2 db field do you mean? The only one I see is 'data' and that's serialized.

rlmumford’s picture

Oh yeah, I didn't read that properly

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new541 bytes

Here's a patch that should mean every PartyHat object has a description. this should allow us to keep the NOT NULL on description.

We may also want to check whether the description field is null inside PartyHat->save()

rlmumford’s picture

Status: Needs review » Fixed

Ok, I just pushed this (I fixed the white-space error first). Tested it out and it seemed to fix saving a party.

joachim’s picture

> Here's a patch that should mean every PartyHat object has a description. this should allow us to keep the NOT NULL on description.

You've lost me now.

I thought the problem was with updates.

What does patch 47 do?

rlmumford’s picture

If you programmatically create a Party Hat and saved it without explicitly giving a discription, the save would fail.

rerooting’s picture

This should work as of a while ago, or so it seems:

#392696: Save default values on insert

Looking into this further...

Automatically closed -- issue fixed for 2 weeks with no activity.