Vocabulary "tags" is used, but it may not exist

Created on 2 May 2023, almost 2 years ago
Updated 4 May 2023, almost 2 years ago

Problem/Motivation

In the file BaseFieldTrait.php, a reference was made to taxonomy terms with the type 'tags'.

$fields['terms'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Tags'))
  ->setDescription(t('Tags for the entity.'))
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default:taxonomy_term')
  ->setSetting('handler_settings', [
    'target_bundles' => ['tags'],
    'sort' => ['field' => '_none'],
    'auto_create' => TRUE,
  ])
  ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  ->setCustomStorage(FALSE)
  ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => -1,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ])
  ->setDisplayOptions('view', [
    'label' => 'inline',
    'weight' => -1,
    'settings' => [
      'link' => TRUE,
    ],
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

In this line:
'target_bundles' => ['tags'],
But this type may not exist, which is the case for me. This leads to the creation of tags with nonexistent vocabulary, resulting in an error when attempting to edit the tag.

Steps to reproduce

  1. Remove vocabulary "Tags"
  2. Create Core with any tag
  3. Try to edit created tag

Result:

Error: Call to a member function id() on null in Drupal\taxonomy\TermForm->form() (line 33 of core/modules/taxonomy/src/TermForm.php).

Proposed resolution

I'm not sure, but perhaps we need to create a separate vocabulary for DRD tags during module installation.

📌 Task
Status

Fixed

Version

4.1

Component

Code

Created by

🇷🇺Russia qzmenko Novosibirsk

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @qzmenko
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Good catch, thanks for reporting this @qzmenko

    You're right, this is a problem. However, the solution is not that easy. For new installations, we could indeed verify the existence of the taxonomy and if not, create it automatically. However, using a DRD specific taxonomy seems to be difficult because we have to keep the existing installations in mind. AFAIK, we can't easily move existing terms over to the new taxonomy without potentially breaking something else, i.e. some other content could use the same terms.

    That said, I'd suggest we implement the verification of the default taxonomy called tags, and create it during installation if it didn't exist.

    MR welcome.

  • @qzmenko opened merge request.
  • Status changed to Needs review almost 2 years ago
  • 🇷🇺Russia qzmenko Novosibirsk
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Nice one, thanks a lot @qzmenko

  • Status changed to RTBC almost 2 years ago
  • Status changed to Fixed almost 2 years ago
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024