ListDataDefinition::toArray is missing the data type

Created on 16 December 2021, over 2 years ago
Updated 21 August 2023, 10 months ago

Problem/Motivation

The ListDataDefinition hardcodes its data type to list. However, it never pushes that to its internal definition that is exported with toArray.

Steps to reproduce

ListDataDefinition::createFromItemType('string')
          ->setRequired(TRUE)

When calling toArray the result is expected to be:

Array (
    'type' => 'list'
    'required' => true
)

But it is

Array (
    'required' => true
)

Proposed resolution

When a ListDataDefinition is created, set the `type` in the definition.

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values = [], DataDefinitionInterface $item_definition = NULL) {
    $this->definition = $values;

Hard code it here or elsewhere. It's usually constructed with an empty array for values

  /**
   * {@inheritdoc}
   */
  public static function createFromItemType($item_type) {
    return new static([], \Drupal::typedDataManager()->createDataDefinition($item_type));
  }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Typed data 

Last updated 18 days ago

  • Maintained by
  • 🇦🇹Austria @fago
Created by

🇺🇸United States mglaman WI, USA

Live updates comments and jobs are added and updated live.
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.

  • 🇦🇺Australia acbramley

    Triaged as part of BSI.

    Is this still an issue? Also, since my knowledge of this system is severely lacking, does this lead to other bugs? Might be good to get a use case of what this issue leads to.

  • Status changed to Active 11 months ago
  • 🇦🇹Austria fago Vienna

    I'd agree that this is unexpected behavior, thus qualifying it as bug seems ok. let's improve this.

  • 🇦🇺Australia acbramley

    @fago are you able to answer any of the questions in #5?

  • 🇦🇹Austria fago Vienna

    let me try:
    well, without the type you cannot re-create the data definition based upon the toArray()
    This works well when working with other classes based upon DataDefinition, but apparently not for lists.

    I don't see it causing subtle other bugs, still it's not working as it should, thus qualifying it as bug seems right to me.

  • First commit to issue fork.
  • 🇺🇸United States TolstoyDotCom L.A.

    I made the indicated changes. I can change the visibility of THIS_TYPE or 'self' to 'static' if necessary. What would be a good way to test this out?

  • Status changed to Needs review 10 months ago
  • 🇬🇧United Kingdom nlisgo

    This steps to reproduce are screaming to be converted into a test.

  • last update 10 months ago
    29,949 pass, 1 fail
  • @nlisgo opened merge request.
  • 🇬🇧United Kingdom nlisgo

    Created a test only MR. I decided the best approach to expose this bug was to add similar extensions of test coverage to the other TypedDataDefinition tests.

  • 🇬🇧United Kingdom nlisgo

    I've added the test to the main MR and I expect that the test will fail in the test-only MR but that the same test will pass in the main MR.

  • last update 10 months ago
    29,953 pass
  • @nlisgo opened merge request.
  • 🇺🇸United States TolstoyDotCom L.A.

    The constant isn't necessary but it might be a good idea to make clear that it won't change and that the uses of 'list' are the same semantically.

  • Status changed to Needs work 10 months ago
  • 🇺🇸United States smustgrave

    Seems have two open threads.

    For the constant would say not sure it's needed either. Looking at how SequenceDataDefinition is done they don't use a constant.

  • 🇺🇸United States TolstoyDotCom L.A.

    I can just hardcode 'list' if necessary, but, IMHO, there's already too much use of hardcoded strings in Drupal.

Production build 0.69.0 2024