0

I have a content type where we reference Taxonomy terms in one of the fields. When the field was created it was set up to Unlimited entries for that field.

The issue is that we need to change this field from Unlimted to Limited to 1.

There are a few nodes where more than one term was referenced. Which means that we can no longer change this fields storage settings until the nodes that have more than one record are changed to 1 term only.

My questions is how to set up a view that will show only nodes that have two or more terms referenced.

Error: There is data for this field in the database. The field settings can no longer be changed.

What needs to be changed: There are 65 entities with 2 or more values in this field.

Solution: If we can change those 65 entries, I know we will be able to limit this filed to 1 term.

How? The question is how do we find those nodes? I can't set up a view that will exclude nodes with 1 taxonomy term but show all with 2 or more.

  • This is not really an answer, but if I were in your shoes, I'd just run a few simple queries to find the nodes if I had access to the database, without having to touch Views at all. Failing that, a simple script that bootstraps drupal and runs an entity query will also work. Views would be the last thing I'd go for to be perfectly honest. – Beebee May 11 '18 at 15:35
2

I haven't tried to lower the settings in a situation where no extra data would prevent it, but it seems it is possible: Changing cardinality lower than highest existing delta causes data loss upon save.

But assuming it can be done, you can also create Views to list content with more than one term in a multi-valued field.

Fields with multiple values track the values with Delta starting from 0. Field with one value has Delta 0. Field with 2 values, has Delta of 0 for the first value, and Delta of 1 for the second value etc.

  • Field
    • Value 1 (Delta: 0)
    • Value 2 (Delta: 1)
    • Value 3 (Delta: 2)

Delta is available in Views. This means you can Filter content with regard to it, which is what you want.

  1. Create Views of type Content with Filters and Fields appropriate for your initial situation, show Title and the Taxonomy field in question so you can check the results (you probably already have this covered)
  2. Add a Filter of your field Delta. It will be called something like MyTags (field_mytags:delta). If you search for your field name it will list both your field and the Delta, choose the Delta option.
  3. Set the Operator of the Delta Filter to Is equal to and enter 1

This will list all the nodes (provided your other filters are set appropriately) where the second position in the Taxonomy field isn't empty. Meaning there are at least 2 terms recorded in that field. The nodes will only be listed once.

If you set the Delta Filter Operator to Is greater than 0, as you may be tempted, it will also list the nodes that have more than one term in that field, but it will list them as many times as there are terms on that node minus 1. So if a node has 3 tags, it will be listed twice, because it will list one result for each Delta above 0.

With this result, you can go on to use Views Bulk operations to edit the listed "offenders", by coding an operation that deletes all but the first value from that field. Or even manually edit each node if coding is not an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.