Added TermForm::getParentIds for allowing to override in contrib

Created on 12 January 2023, over 1 year ago
Updated 16 March 2023, over 1 year ago

Problem/Motivation

The #2898903: Terms lose 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 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

πŸ› Bug report
Status

Fixed

Version

9.5

Component
TaxonomyΒ  β†’

Last updated 1 day ago

  • Maintained by
  • πŸ‡ΊπŸ‡ΈUnited States @xjm
  • πŸ‡¬πŸ‡§United Kingdom @catch
Created by

πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

Live updates comments and jobs are added and updated live.
  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    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.

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    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. Once that's added, this can be committed to 10.1.x.

    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.

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

    Thanks for working on this!

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    Trying that one more time.

  • πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

    @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 minor change with no impact to API. We're creating a new protected method and is not API addition.

  • πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

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

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    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.

  • πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

    @alexpott, thank you, it makes sense.

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

  • Status changed to RTBC over 1 year ago
  • πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄
    • 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 explanation for these tags.
    • As I have doubts on tags and 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.
  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

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

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    FWIW my opinion here is that 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.

  • πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

    @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 some risks, true. But since 2017 in production this is the 1st time it become inoperable with core.

  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    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.

  • Status changed to RTBC over 1 year ago
  • πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄
    • 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...
  • Status changed to Fixed over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom catch

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

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

Production build 0.71.5 2024