Problem/Motivation

The #2898903: Terms lose <root> as the parent when editing change broke the rdf_taxonomy sub-module of https://www.drupal.org/project/rdf_entity:

--- a/core/modules/taxonomy/src/TermForm.php
+++ b/core/modules/taxonomy/src/TermForm.php
@@ -23,7 +23,12 @@ public function form(array $form, FormStateInterface $form_state) {
     $taxonomy_storage = $this->entityTypeManager->getStorage('taxonomy_term');
     $vocabulary = $vocab_storage->load($term->bundle());
 
-    $parent = array_keys($taxonomy_storage->loadParents($term->id()));
+    $parent = [];
+    // Get the parent directly from the term as
+    // \Drupal\taxonomy\TermStorageInterface::loadParents() excludes the root.
+    foreach ($term->get('parent') as $item) {
+      $parent[] = (int) $item->target_id;
+    }
     $form_state->set(['taxonomy', 'parent'], $parent);
     $form_state->set(['taxonomy', 'vocabulary'], $vocabulary);

Specifically, https://www.drupal.org/project/rdf_entity is swapping the storage of the taxonomy_term entity in order to store the terms in a triplestore backend. Along with this, the TIDs are strings (URIs), not integers. In #2898903: Terms lose <root> as the parent when editing they were casted to integers, prohibiting https://www.drupal.org/project/rdf_entity to use TermForm as it is.

Of course, https://www.drupal.org/project/rdf_entity can swap the form class handler and add its own but it has to copy/paste the entire TermForm::form() method.

Steps to reproduce

N/A

Proposed resolution

Make it easier to extend TermForm::form() method by moving the parents computing in a protected method.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

Issue fork drupal-3332877

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Issue summary: View changes
claudiu.cristea’s picture

Issue summary: View changes
claudiu.cristea’s picture

Tagging

claudiu.cristea’s picture

  • MR 3232: Drupal 9.5.x
  • MR 3235: Drupal 10.0.x
claudiu.cristea’s picture

Status: Active » Needs review
claudiu.cristea’s picture

StatusFileSize
new1.58 KB

Because of https://drupal.slack.com/archives/C51GNJG91/p1673438297534429 I'm posting also a patch to get testing

claudiu.cristea’s picture

Related issue in affected contrib module.

claudiu.cristea’s picture

StatusFileSize
new1.57 KB

The patch again

dimilias’s picture

Status: Needs review » Reviewed & tested by the community

Seems legit and it helps us with the non numerical IDs. RTBC +1.

penyaskito’s picture

I wasn't sure if this could be considered a bug, but from core.data_types.schema.yml:

field.value.entity_reference:
  type: mapping
  label: 'Default value'
  mapping:
    target_id:
      type: string
      label: 'Value'
    target_uuid:
      type: uuid

So RTBC+1

penyaskito’s picture

Title: Contrib module broken after #2898903 » TermForm should honor target_id defined data type

Adjusting title

xjm made their first commit to this issue’s fork.

xjm’s picture

I wasn't sure if the patch or the MR was the latest here, so I kicked the tires on the two MRs now that the CI outage is over.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record, +Needs release manager review, +Needs tests

Thanks for working on this. This looks like a very sensible fix; I'm signing off on the method addition in my little-used role as Taxonomy subsystem maintainer. 🪄😀

Promoting to major, since it's a contrib blocker and a regression.

I put a couple of nitpick suggestions on the MR. The main thing we still need here though is a change record.

Typically, API additions are only allowed in minor releases, even for bugfixes. However, I think there's a low risk of method name collisions for this backport, and it's more important to fix the affected contrib in the production branches. I'll check with the other release managers to confirm that this is backportable under a policy exception. If we do backport, it will need a release note.

We should also maybe add a regression test and a unit test for the new method?

Thank you!

xjm’s picture

Priority: Normal » Major

Trying that one more time.

claudiu.cristea’s picture

@xjm, Thank you for review. I see you tagged with "Needs tests". Hm, I don't see why, as this is just moving around a piece of code from a method into a new method. Also, "Needs change record" and "Needs release manager review"? This is just a minor change with no impact to API. We're creating a new protected method and that is not an API addition.

claudiu.cristea’s picture

Or maybe the simplest change would be to remove the (int) as per #13

alexpott’s picture

I think it is quite a lot to expect core and contrib to work when the type of the ID field is changing. The code referenced in #13 is for the entity reference field. It has not much to do with the data type as defined by \Drupal\Core\Entity\ContentEntityBase::baseFieldDefinitions() which is what this is changing for taxonomy fields.

This is not just about this bit of code... it's also about things like:

entity.taxonomy_term.canonical:
  path: '/taxonomy/term/{taxonomy_term}'
  defaults:
    _entity_view: 'taxonomy_term.full'
    _title: 'Taxonomy term'
    _title_callback: '\Drupal\taxonomy\Controller\TaxonomyController::termTitle'
  requirements:
    _entity_access: 'taxonomy_term.view'
    taxonomy_term: \d+

Which I guess the rdf_entity is altering. I think we could consider saying if you altering all the other things then you can replace the entire form too. The change here seems okay in the tiny scope of make this work with core and the rdf_entity module but I'm not sure that the approach of the rdf_entity module is actually sustainable.

claudiu.cristea’s picture

@alexpott, thank you, it makes sense.

@xjm, could we move with this w/o tests? It's just moving code around.

claudiu.cristea’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record, -Needs release manager review, -Needs tests
  • Review remarks addressed
  • IMHO, this is not an API change, we're just moving a piece of code in a protected method. So, I don't think this needs tests, change record or release manager review. I'm tentatively untagging to get more explanations for these tags.
  • As I have doubts on tags and on this being an API change, I'm settings back to RTBC to get more feedback from @xjm
  • Given @alexpott comment from the tiny change is acceptable.
penyaskito’s picture

Title: TermForm should honor target_id defined data type » Added TermForm::getParentIds for allowing to override in contrib

Updated issue summary to my best knowledge of what this patch is about now.

alexpott’s picture

FWIW my opinion here is

but I'm not sure that the approach of the rdf_entity module is actually sustainable.

I think that swapping out the storage and changing the underlying type of the ID field is beyond to make it inoperable with some of contrib and probably other parts of core.

claudiu.cristea’s picture

@alexpott

I think that swapping out the storage and changing the underlying type of the ID field is beyond to make it inoperable with some of contrib and probably other parts of core

There are some risks, true. But since 2017 in production this is the 1st time it become inoperable with core.

catch’s picture

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

I agree with #23 that given this is just adding a protected method to a form and moving code around it doesn't need explicit test coverage.

However, to backport it to 10.0.x and 9.5.x, we should add a change record, because it's a method addition and modules doing similar to rdf_entity might want to know about it.

claudiu.cristea’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record

  • catch committed ea0c86bc on 10.0.x
    Issue #3332877 by claudiu.cristea, xjm, penyaskito, alexpott: Added...

  • catch committed 20e21dc4 on 10.1.x
    Issue #3332877 by claudiu.cristea, xjm, penyaskito, alexpott: Added...

  • catch committed 0421a652 on 9.5.x
    Issue #3332877 by claudiu.cristea, xjm, penyaskito, alexpott: Added...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.1.x, cherry-picked to 10.0.x and 9.5.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.