[PP-1] Allow plurals on bundle labels

Created on 12 July 2016, almost 8 years ago
Updated 19 December 2023, 6 months ago

Problem/Motivation

Entity types can define now plurals for their labels starting with #1850080: Entity type labels lack plurality, cannot generate UI text based on label if plural is needed . It's possible now to have the variants "content item" (singular) and "content items" (plural) for nodes. We want the same for bundles. We want to be able to have "article" (singular) and "articles" (plural).

It's not the same as for entity types. Entity types can define the label plural variants in the annotation while for bundles the site builder should be able to add such variants. Thus we need to store the variants in the bundle config entity and provide UI for site builders.

Proposed resolution

Create the foundation that allows bundle config entities to store plural labels and add this ability to the node_type bundle entity as a start.

  1. Create a new interface EntityBundleWithPluralLabelsInterface that defines 6 new methods:
    • setSingularLabel()
    • getSingularLabel()
    • setPluralLabel()
    • getPluralLabel()
    • setCountLabel()
    • getCountLabel()

    These are similar to those implemented to entity types.

  2. Add a common implementation for the new 6 methods in a new trait EntityBundleWithPluralLabelsTrait, to be reused in most of the bundle config entities.
  3. Create a bundle_entity_with_plural_labels schema to be used by bundles that want to support plural labels. The new schema contains the new storage keys: label_singular, label_plural, label_count.
  4. Allow more than one version for the count label. This is because, in the real world, a site might need different versions depending on the place these count labels are used. Let's see a hypothetical website scenario:
    • On a page the count label could be a simple label as 1 article\x03@count articles.
    • On a search results page we need a different version: 1 article was found\x03@count articles were found. Note that we cannot build this version by deriving from the first one because of the was/were thing.
    • In other place something like <span>1</span> article\x03<span>@count</span> articles.
    • In a 3rd place, because the count is displayed in other <div ...> or even is rendered in a different theme hook, just Article\x03Articles, without the count part.

    Similar with translated strings, were we are able to store contextualised translations and then retrieve them by passing the context identifier, we allow label_count to store multiple versions, identifiable by a context string identifier.

  5. Bundles declared via hook_entity_bundle_info(), are able to declare such labels by using the keys: label_singular, label_plural and label_count. This will be documented in entity.api.php [see #108, #109].
  6. Bundles supporting plurals labels, regardless if they are defined as config entities or via hook_entity_bundle_info(), are able to process the count label, given a count, by using the new service method entity_type.bundle.info::getBundleCountLabel(). This is a new method exposed by EntityTypeBundleInfoInterface [see #108, #109]
  7. Make NodeTypeInterface extend also EntityBundleWithPluralLabelsInterface.
  8. Make NodeType use the EntityBundleWithPluralLabelsTrait trait.
  9. Make node.type.* schema extend bundle_entity_with_plural_labels.
  10. Add plural labels to all node types shipped with Drupal core.
  11. The UI for editing node type plural labels is added in a follow-up: #2938251: Allow edit of bundle plural labels in the node type form .

Remaining tasks

User interface changes

None. The UI changes are split in #2938251: Allow edit of bundle plural labels in the node type form .

API changes

New interface \Drupal\Core\Config\Entity\EntityBundleWithPluralLabelsInterface:

  • setSingularLabel()
  • getSingularLabel()
  • setPluralLabel()
  • getPluralLabel()
  • setCountLabel()
  • getCountLabel()

Data model changes

The node type bundle config entity has new keys/values: label_singular, label_plural, label_count.

Feature request
Status

Needs work

Version

11.0 🔥

Component
Entity 

Last updated 1 day ago

Created by

🇷🇴Romania claudiu.cristea Arad 🇷🇴

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.69.0 2024