Problem/Motivation

Solve objects in theme definition methods.

Error:

Unable to dump a service container if a parameter is an object without _serviceId.

Steps to reproduce

E.g

final class MyThemeObject {

  #[ThemeDefinition]
  public static function theme(): array {
    return [
      'variables' => [
        'attributes' => new \Drupal\Core\Template\Attribute(),
      ],
    ];
  }

}

Proposed resolution

Unknown

Remaining tasks

Investigate, fix

User interface changes

Nil

API changes

Nil

Data model changes

Nil

Issue fork pinto-3425951

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

dpi created an issue. See original summary.

larowlan’s picture

I wonder if we could add a property attribute e.g #[Theme\Variable] and put that on constructor Params and have the theme hook variables inferred

Between the constructor, a factory method, the theme definition and the build method, there's a lot of boilerplate around the variable names

I also wonder if the method that does the build could auto fill # keys from properties with attributes so if you don't need any logic these get passed through to the render array automatically

I can spin up a new issue (or two) for both those ideas if this isn't the best place for it

dpi’s picture

I wonder if we could add a property attribute e.g #[Theme\Variable] and put that on constructor Params and have the theme hook variables inferred

This sounds like an interesting idea.

New issue, though. This one isn’t quite it.

dpi’s picture

dpi’s picture

This bug was fixed with a workaround as a part of #3482094: Implement Slots. Definitions are now serialised to a string, so objects no longer enter Drupals' container.

A new factory unserialises definitions on request in a factory.

dpi’s picture

Status: Active » Fixed

  • dpi committed 74eeb022 on 1.x
    Issue #3425951 by dpi: Test coverage for objects in theme definitions
    

Status: Fixed » Closed (fixed)

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