The taxonomy term entity schema is missing the langcode property (present in nodes, users, comments, etc). To add support to language later, we need the taxonomy terms to support the langcode property just like other entities do. We do not need/want to add a UI here since the node module has UI for this which we want to move up to entity module eventually to be shared among entities. So this is about providing the low level data storage for the langcode field.

Comments

csg’s picture

Status: Active » Needs review
StatusFileSize
new1.18 KB

This patch adds a language field to the schema, and adds the field to the db in the update hook.

gábor hojtsy’s picture

Status: Needs review » Needs work
+++ b/core/modules/taxonomy/taxonomy.installundefined
@@ -39,6 +39,13 @@ function taxonomy_schema() {
+      'language' => array(

@@ -245,4 +252,13 @@ function taxonomy_field_schema($field) {
+  $language = array(
+    'description' => 'The {language}.langcode of this term.',
...
+  db_add_field('taxonomy_term_data', 'language', $language, $keys_new = array());

'language' in Drupal 8 should be 'langcode' in schema.

Also the $language in the update function would better be $langcode_field or something like that.

+++ b/core/modules/taxonomy/taxonomy.installundefined
@@ -39,6 +39,13 @@ function taxonomy_schema() {
+        'default' => LANGUAGE_NONE,

I don't think we ever do this. It is not a bad idea per say, but we usually default to '' and then init to LANGUAGE_NONE in save functions (but we don't give a meaning to the empty langcode value).

+++ b/core/modules/taxonomy/taxonomy.installundefined
@@ -245,4 +252,13 @@ function taxonomy_field_schema($field) {
 function taxonomy_update_8000() {
   db_drop_field('taxonomy_vocabulary', 'module');

Let's not modify an existing update function, create a new taxonomy_update_8001().

+++ b/core/modules/taxonomy/taxonomy.installundefined
@@ -245,4 +252,13 @@ function taxonomy_field_schema($field) {
\ No newline at end of file

Speaks for itself :)

csg’s picture

StatusFileSize
new1.26 KB

Thanks for the review! Here is the new patch.

csg’s picture

Status: Needs work » Needs review
pp’s picture

Status: Needs review » Needs work
+++ b/core/modules/taxonomy/taxonomy.install
@@ -245,4 +252,19 @@ function taxonomy_field_schema($field) {
-}
\ No newline at end of file

hmm... why is the end of the file at middle?

gábor hojtsy’s picture

@pp: well, looks like it is being removed there. Looks good in that regard.

csg’s picture

Assigned: Unassigned » csg
Status: Needs work » Needs review
StatusFileSize
new2.4 KB

This patch also sets the langcode to LANGUAGE_NONE when saving a new term which doesn't have a langcode.

fubhy’s picture

Status: Needs review » Needs work
+++ b/core/modules/taxonomy/taxonomy.installundefined
@@ -39,6 +39,13 @@ function taxonomy_schema() {
+      'langcode' => array(
+      'description' => 'The {language}.langcode of this term.',
+      'type' => 'varchar',
+      'length' => 12,
+      'not null' => TRUE,
+      'default' => '',

The indentation is not right here.

Other than that it looks okay.

fubhy’s picture

+++ b/core/modules/taxonomy/taxonomy.installundefined
@@ -245,4 +252,20 @@ function taxonomy_field_schema($field) {
+  );
+  db_add_field('taxonomy_term_data', 'language', $langcode_field, $keys_new = array());

We probably want to remove the $key_new = array() part there too.

csg’s picture

Status: Needs work » Needs review
StatusFileSize
new2.39 KB

Thanks! I updated the patch.

csg’s picture

StatusFileSize
new2.39 KB

I corrected the field name in the update hook.

kalman.hosszu’s picture

Status: Needs review » Reviewed & tested by the community

I checked the code and the tests run well, so I change the status to RTBC.

xjm’s picture

Is it possible to add test coverage for this?

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

@xjm: right, that we don't have a UI for it yet should not stop us from adding tests to the API. Ie. saving a taxonomy term as default and one with a specific language and then loading them back in checking if that succeeded.

xjm’s picture

Cool, there's an API-only test class that should work for this. It's named TaxonomyTermUnitTest currently (despite that it actually extends the web test class). (See also: #1446366: [meta] Multiple web test classes mislabeled as unit tests. simpletest ([Web|Unit|DrupalUnit]TestBase))

gábor hojtsy’s picture

Also, as pointed out in #1444992: Add langcode property in file schema if the schema did have a langcode property, leave that alone (assume it is in the form we need) - ie. skip the update. To support people previously running modules that might have added that in D7 to support taxonomy translation.

effulgentsia’s picture

+++ b/core/modules/taxonomy/taxonomy.install
@@ -245,4 +252,20 @@ function taxonomy_field_schema($field) {
+/**
+ * Add langcode field to the database.
+ */

Clarify which table the field is being added to.

+++ b/core/modules/taxonomy/taxonomy.install
@@ -245,4 +252,20 @@ function taxonomy_field_schema($field) {
+    // Set all existing terms' language to LANGUAGE_NONE.
+    'initial' => LANGUAGE_NONE,

Unnecessary comment.

+++ b/core/modules/taxonomy/taxonomy.module
@@ -620,6 +620,7 @@ function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) {
+ *   - langcode: (optional) The language of the term.

s/language/language code/

+++ b/core/modules/taxonomy/taxonomy.module
@@ -657,6 +658,9 @@ function taxonomy_term_save($term) {
+    if (empty($term->langcode)) {
+      $term->langcode = LANGUAGE_NONE;
+    }

I think this would be better earlier in the function, before the presave hooks are invoked on other modules.

6 days to next Drupal core point release.

effulgentsia’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new4.62 KB

This patch addresses #16 and #17. It also adds langcode to {taxonomy_vocabulary}, since vocabularies are also entities, according to taxonomy_entity_info().

I disagree with #14. I added the assertions in #1444992-22: Add langcode property in file schema because there was already a test for file_save() to add them to. However, none of our other entity types have low-level unit tests for saving via the API and verifying that each property got saved correctly. I don't think this is the issue in which to add such low-level unit tests. We may want to do so for all entity types, especially in relation to the Web Services initiative (where all of our entity CRUD needs to be fully decoupled from forms), but I don't think it's needed here. That does mean we don't have test coverage for now for this property, but I'm assuming after this lands, there will be a follow-up issue for moving node.module's UI for langcode to all entity forms, and we can incorporate functional tests there. In the meantime, this issue just lays the groundwork for that work. So, I'm removing the "Needs tests" tag, but please add it back if you disagree with me.

gábor hojtsy’s picture

@effulgentsia: great improvements, thanks! On the unnecessary comment, honestly I was not aware of the initial property and ot made Dave Reid puzzled in the file langcode issue, so that is why it seemed logical IMHO. I agree it is not necessary if we assume people know about it :)

Any other concerns from people?

gábor hojtsy’s picture

Btw we likely need upgrade tests, no? Even though the change itself is pretty darn simple.

Also, do we want to defalt to the site default language in the upgrade instead?

xjm’s picture

Aye, I'd agree that upgrade path tests are a really good idea any time there's a hook_update_N() in a patch, really.

gábor hojtsy’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Adding back Needs tests at least for the upgrade tests.

effulgentsia’s picture

Status: Needs work » Closed (duplicate)
gábor hojtsy’s picture

Issue tags: -Needs tests, -sprint

Removing outdated tags.