This patch allows taxonomy terms (as fields) to be automatically added via Devel Generate.

It has a couple TODOs mentioned in the code, but seems to work fine otherwise. Without this patch, I wasn't able to correctly create forum topics using Devel Generate, but with it I can.

With forums, it also has the interesting property that it often assigns a generated forum topic to more than one forum (which Drupal treats as the topic having been "moved" from one forum to another). Not an actual bug, just interesting - we could perhaps find a way to remove that behavior if we really wanted to.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

Status: Needs review » Active

I committed this, and removed the checkbox on the add content form since terms are now added to new nodes unconditionally.

Note that I am seeing errors like below when creating Article nodes. Note that those have a Tags vocab by default. Help wanted.

Notice: Trying to get property of non-object in taxonomy_implode_tags() (line 944 of /Users/mw/htd/prfr/modules/taxonomy/taxonomy.module).

asimmonds’s picture

Status: Active » Needs review
FileSize
918 bytes

If the tags term list is empty, then a invalid entry of tid = 0 is assigned to the node in the taxonomy_index table.

I tried just removing $object_field['tid'] = 0; from _taxonomy_cck_generate(), but that just resulted in taxonomy_field_insert() looking for a 'tid' key, a array of multiple empty arrays was being passed.

The attached patch gets the article node creation working again, with and without tags. It needs some more testing against the other fields that support multiple values.

moshe weitzman’s picture

Title: Taxonomy terms and forum topics cannot be created » Taxonomy terms can duplicate on same node
Status: Needs review » Active

This seems to be working fine right now. The only buglet I noticed is that the same term can be added multiple times to same node. Was using a free tag vocab when i saw this. Arguably, this should be solved in core. Does field api have a notion of uniqueness within a multiple value field?

skirpichev’s picture

subscribe

salvis’s picture

Marked #1215326: Generated nodes taxonomy field contains duplicate terms as a duplicate of this.

If core does not prohibit duplicate tags, then users can enter duplicate tags, right? We may not like it when they do that, but if users can misbehave, then why should devel_generate not misbehave occasionally, too?

This is WF imo, or even WAD.

jamiecuthill’s picture

Whilst I agree the core code accepts duplicates, the input types the user is presented with don't. Try adding the same term twice with any of the field widgets. If core properly accepted the same term twice then saving the node form would not remove the duplicates, but it does.

salvis’s picture

Ok, so what needs fixing, #2 or #3? And what does the patch in #2 try to fix?

Can we get a patch that passes?

dhruvbaldawa’s picture

subscribe

dddave’s picture

#1063326: Duplicates of term fields a question in the core queue wether this is a bug or a feature.

yareckon’s picture

The above issue was closed as a duplicate of this one #1050466: The taxonomy index should be maintained in a node hook, not a field hook

On one hand it is awesome to make devel_generate create exactly what core allows, multiples of the same term on a node if that's a possible data point. On the other hand, in terms of modelling real content for prototyping, that is pretty useless, and just gets in the way of using that generated data for anything. Could we implement something to suppress multiple of the same terms on an a node entity for the sanity of folks making views and filters with generated content?

yareckon’s picture

This issue is made much more annoying by the not so random selection of terms, which I've tried to fix here #1328380: Patch: Fixing fact that certain tax terms are nearly always selected by devel_generate

attheshow’s picture

Just ran into this myself for the first time. Not sure how a user could go about purposefully sending the same term multiple times, but my devel generated nodes definitely do it. Screenshot attached.

salvis’s picture

I don't doubt that this happens, I just haven't seen any convincing patch yet.

dkingofpa’s picture

Status: Active » Needs review
FileSize
984 bytes

Attached patch removes duplicates from the array of terms prior to them being returned. Created from HEAD of 7.x-1.x branch.

dkingofpa’s picture

Coincidentally, the patch should also apply cleanly to the 8.x-1.x branch.

salvis’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Status: Needs review » Needs work
+++ b/devel_generate/taxonomy.devel_generate.inc
@@ -2,7 +2,19 @@
+    // Remove duplicates
+    $tmp = array ();
+    for ($i = 0, $size = sizeof($terms); $i < $size; $i++) {
+      if (in_array($terms[$i]['tid'], $tmp)) {
+        unset($terms[$i]);
+      } else {
+        $tmp[] = $terms[$i]['tid'];
+      }
+    }
+
+    return $terms;

This is a bit long-winded, and I think indexes should start at 0 and be consecutive. Maybe something like (untested!)

    // Remove duplicates.
    $result = array();
    foreach ($terms as $term) {
      $result[$term['tid']] = $term;
    }
    return array_values($result);
dkingofpa’s picture

Status: Needs work » Needs review
FileSize
872 bytes

Re-rolled a new patch with suggestions from #16. Code works for me on D7.

segx’s picture

I was experiencing this problem and the patch worked for me. Thank you!

Manuel Garcia’s picture

Issue tags: +develcontribute

Tagging for this saturday's sprint.

pcambra’s picture

Status: Needs review » Needs work
Issue tags: -develcontribute

I've found a problem with the taxonomy widget in Drupal 8, marking this as need work as I'm waiting some clarifications from the fieldapi folks :)

The issue is that we're checking the widget behavior:
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {

And that function is supposed to return one of the constants of behavior.

function field_behaviors_widget($op, $instance) {
  $info = field_info_widget_types($instance['widget']['type']);
  return isset($info[$op]) ? $info[$op] : FIELD_BEHAVIOR_DEFAULT;
}

But in the taxonomy autocomplete widget:
* multiple_values = TRUE

Let's keep this on hold for the moment

clemens.tolboom’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
787 bytes

I ran into this one when generating for D7 too. I've rerolled #17

clemens.tolboom’s picture

And now with some documentation. Sorry for the `pings`

moshe weitzman’s picture

Status: Needs review » Needs work

This needs a new approach for D8 as the picking of terms is now done in core. See \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::generateSampleValue

js’s picture

I might have missed where someone has already run into this, but I needed a minimum for random.

The problem was that lowest tid for my vid was over 2,000, and the range was only about 20, so the random was selecting between 1 and 2020 for example and then always selecting my lowest tid; 2000 in this example.

  if ($max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data} WHERE vid = :vid', array(':vid' => $vocabulary->vid))->fetchField()) {
    $min = db_query('SELECT MIN(tid) FROM {taxonomy_term_data} WHERE vid = :vid', array(':vid' => $vocabulary->vid))->fetchField();
    $candidate = mt_rand($min, $max);
ann b’s picture

This occurs with node reference fields also.

Drupal 7.32
Devel 7.x-1.x-dev - 2015-Aug-13

willzyx’s picture

Status: Needs work » Closed (outdated)

Closing for lack of activity. Feel free to reopen if the issue still exists