Jump to content
kongondo

Create Field with Tags Enabled

Recommended Posts

Hi,

Could someone please show me how to create a new file or image field using the API and enable tags as well? It used to work a while back but I am not sure what changed. It seems I can only enable tags using the GUI and only once the field has been added to a template. In addition, it seems one has to do it twice; First enable tag, then disable them then re-enable them! I am talking about normal tags (user enters tags by text input). 

<?php namespace ProcessWire;

$f = new Field();
// ... more code here
$f->useTags = 1;// does not work, even if I do this after adding this field to a template
$f->save();

 

Relevant method in FieldtypeFile.module: updateDatabaseSchema()

Edit: Just updating this. 

The above code will look like it works. The field will save the setting use tags. However, the field's table will not be altered, i.e. the column 'tags' will not be created. Editing a page that uses that field will throw a PDO error: unknown  column field_your_field_name.tags in field list.

Thanks.

Share this post


Link to post
Share on other sites

I didn't try creating a new field, but I altered an existing one by visiting the field settings and using this in the Tracy console:

$field->useTags = 1;
$field->save();

and it worked as expected. Also, just in case you didn't notice (I am sure you did), the options are 0, 1, 8, 9

Share this post


Link to post
Share on other sites
11 minutes ago, adrian said:

and it worked as expected.

Thanks to testing. I forgot to mention, it will look like it works but it doesn't. The setting is saved for the field, however, the database is not altered. A new column 'tags' is not created. In fact, if you edit a page using your field, you should get a PDO error about unknown column field_your_field_name.tags in field list 🙂.

Share this post


Link to post
Share on other sites

New field (created via GUI) without tags:

image.png.7466ea4cb1ca1028d82c2b1a442ecad9.png

Then apply:

$field->useTags = 1;
$field->save();

but nothing happens yet.

But then reload the field settings page in the admin and now it looks correct:

image.png.7cb977225e652d6d741226fc39f13eaf.png

so it seems like the updating of the DB schema doesn't happen on save(), but rather when the field is loaded. My guess would be that this behavior was broken when Ryan added the new custom image fields a while back.

 

  • Like 1

Share this post


Link to post
Share on other sites
1 minute ago, adrian said:

when Ryan added the new custom image fields a while back.

That has been my guess as well.

2 minutes ago, adrian said:

But then reload the field settings page in the admin and now it looks correct:

That was my observation as well, only difference being I was saving (like twice) before it would catch. 

Share this post


Link to post
Share on other sites
4 minutes ago, kongondo said:

Can call this a bug then?

Yeah, I think so for sure.

Share this post


Link to post
Share on other sites

The multiple save issue that you brought up reminds me of the way ProfieldsTable works - you need to save the field settings twice in order for a field order change to stick.

Share this post


Link to post
Share on other sites
1 hour ago, adrian said:

The multiple save issue that you brought up reminds me of the way ProfieldsTable works - you need to save the field settings twice in order for a field order change to stick.

Interesting. Today even saving twice didn't work for me. I had to do it about 7 times!

Share this post


Link to post
Share on other sites

@adrian

I think I have managed to resolve the issue in my case. I was using setImportData() to create the file/image fields and my import data contained a fileSchema declaration (set at 7). Removing that entry resolved the issue. 

I tested on a new install, creating the field using the GUI, using the API (non setImportData() usual method) as well as using setImportData(). I was only able to reproduce the issue as I mention above.

I have closed the bug report but please let us know if you are still seeing the issue. Thanks.

Share this post


Link to post
Share on other sites

@kongondo - not sure what happened here. I actually just had to reset my sandbox install (mysql issue when upgrading to PHP 8) and now everything seems to be working fine with the example I posted above. I get the feeling this issue might appear again in certain circumstances but not sure how to reproduce at the moment.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...