Lets create a flag field type in core, this will be hugely beneficial as often we want to flag content for many reasons and we can also convert existing flags such as sticky, promote and the user 'blocked' flag into fields.

Comments

michelle’s picture

Just a note to not forget that forum.module makes use of sticky.

Michelle

damien tournoud’s picture

I don't believe we want to make Drupal slower then it already is. A flag field type in core: why not. Converting sticky and promote to it: no way ;)

catch’s picture

So for this not to be a performance drain, we need the field module to hook_schema_alter() the flag columns onto the table for the fieldable entity (so {node}.sticky is owned by flag_field.module's hook_schema() - and when a new field / field instance is created, we do db_add_field() to put it there - and the same for removal.

Then the field module's hook_field_load() hook_field_insert() hook_field_save() handle updating the sticky/promoted etc. statuses on the entity table. There may be another way to do this in field API which completely skips field storage for this stuff, which would save a duplicate insert/update - but I don't know how that's supposed to be done.

The other option would be materialized views in core, but that's not in CVS yet so I don't have high hopes.

This would be great though - you could rename the core 'flags' to something else, and add extra ones very easily, and it'd allow for things like a 'featured' column for users via the CCK/Field UI.

Forum queries wouldn't need to change - but we'd have to either prevent the 'sticky' flag from being removed altogether, or somehow integrate the flag field and forum module to sync up (which seems impossible without Views, so probably forcing sticky/promoted etc. to always exist is the best bet for now - even if you can rename them / remove them from the UI).

mikey_p’s picture

Also $node->status any number of things that other modules add.

catch’s picture

DamZ pointed out in irc we need indexes as well.

So would need to be db_add_field() and db_add_index() (and keep any composite indexes for the 'core' flags).

naheemsays’s picture

one again, flogging an issue I created, but #470580: Allow multiple content types for user blogs would benefit from such a thing (and also has something that can probably easily be expanded into a simple flag module).

For that issue, I was dissuaded to add more columns to the node table, but I am not really even a developler, so people who know better will know better.

catch’s picture

We can't use foreign keys in core, because MyISAM doesn't support them, and we need to add conditions on that other table, not just load it into the node object, so there's a real performance impact there. So I think if we have a generic way to add these flags to the node table via the field, then your blog patch could add a 'blog' field instance as a flag, and that would work (and this could be defined per content type, which bangpound's use case would allow for) - also you'd only get the extra field if blog.module was enabled - so sites without that need wouldn't suffer the extra column and index to maintain.

Having used flag module in contrib with views, I can confirm the queries you end up generating can be very, very ugly indeed, so I'm not sure why Crell thinks it's not a problem.

chx’s picture

Note that MySQL can not index across tables so if you have a big table you already denormalize anyways. I do not really care where I pull the flags out from, whether it's node or something else, I need to denormalize from several tables anyways.

josh waihi’s picture

There are two scopes to consider, global and per content type (for node), naturally a global flag can be added as a column to the node or node_revision table but per content type would require another table to join, slowing things down a bit. For example, I never use sticky and don't see the use for it, but as Michelle pointed out, forums still use it, so I would want only my forum nodes to have a sticky flag on them.

In terms of performance however, only aggregates would be slowed down if they were sorted on non-global flags. And things like ALTER TABLE ADD COLUMN on 500K of nodes, is only gonna happen once, if its really going to be an issue, then recommend switching to maintenance mode for content type changes.

josh waihi’s picture

There are two scopes to consider, global and per content type (for node), naturally a global flag can be added as a column to the node or node_revision table but per content type would require another table to join, slowing things down a bit. For example, I never use sticky and don't see the use for it, but as Michelle pointed out, forums still use it, so I would want only my forum nodes to have a sticky flag on them.

In terms of performance however, only aggregates would be slowed down if they were sorted on non-global flags. And things like ALTER TABLE ADD COLUMN on 500K of nodes, is only gonna happen once, if its really going to be an issue, then recommend switching to maintenance mode for content type changes.

yched’s picture

hook_field_storage_pre_*() hooks can be used to bypass regular storage. So technically having fields 'status', 'sticky' etc... stored in {node} is possible.

Such a move would notably mean that
- $node->status becomes $node->status[0]['value']
- those flags become revisioned [edit: er, ok, probably not if stored on {node}]

I'm not sure I see what this would really buy us, though...

josh waihi’s picture

Issue tags: +fields

I was gonna write up a patch to get things rolling but seen as I've never written a cck field before and field.api.inc has too many TODOs in it to be able to figure out how to implement this, I'm just gonna describe the spec for flag field instead.

The flag field will be an effort to solve several issues surrounding flag type facets within objects like $node and $user. These issues include, removing sticky and promoted from the node schema as they are not always needed. Adding status to revisions to show which are published and which are still in 'draft' (node.vid would still indicate which revision to show). As for users.blocked - I don't really see what benefit it would have as a flag field. Likewise, even though node.status is a yes/no field, it wouldn't make any sense turn it into a flag field.

In terms of database and performance, the flag field could create flags in 1 of two ways. Firstly, store flags in its own table which would require joining - this would be used when flag field didn't know how to do the second option that is to alter a table such as node, node_revision or users and add the column - this is rather useful as many flags are used as a means to filter data, eg status,promote,blocked,abuse. So naturally, a flag field would also be indexed by default (UI could offer an opt-out option). Because there is potential that a user could add a flag field to a node table that has a billion rows, we could add a check along to lines of if (count > scary number) suggest maintenance mode.

Keeping in mind that sticky is used by forums, the forum install hook could create the sticky flag if not already installed. This also leads to ideas like having default fields that are disabled by default when drupal installs but can be turned on by a user or a module such as forum.

I think that covers everything Catch and I discussed. I leave you with this http://www.youtube.com/watch?v=hYeFcSq7Mxg

yched’s picture

re Josh Waihi #12: "I was gonna write up a patch but field.api.inc has too many TODOs in it to be able to figure out how to implement this"
heh, I cannot leave that unanswered ;-). Working on the missing doc bits, stay tuned.

yched’s picture

patch for Field type API docs in #528182: Field type API docs :-p

sun.core’s picture

Version: 7.x-dev » 8.x-dev
jhedstrom’s picture

Version: 8.0.x-dev » 8.1.x-dev
Issue summary: View changes
Status: Active » Postponed (maintainer needs more info)

Feature -> 8.1.

This thread seems to have lost momentum, and much has changed in the interim. Marking postponed for now.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pasqualle’s picture

longwave’s picture

All three of these are boolean base fields now, I think that is the closest to a "flag" field that we have in core - I suggest this can be closed as outdated.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)