Problem/Motivation

There is a lot of code in helper functions to build token trees which makes more sense in a service. Let's move it to a service.

Proposed resolution

Add a service.

Remaining tasks

User interface changes

None

API changes

The functions may still remain or can be removed depending on what is required.

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hussainweb created an issue. See original summary.

hussainweb’s picture

Here is the patch for testing. The changes relevant to the issue are in the .txt file which needs to be reviewed. It has to be applied over the patch in #2640612-11: Move token information functions to a service as it makes use of the token service. I have attached a combined patch so that the tests work.

hussainweb’s picture

Berdir’s picture

Status: Needs review » Needs work

Looks like a good start. Some specific feedback below.

AFAIK, we currently have 0 test coverage for the token tree. Would be great to start adding some UI tests there, could be a pretty simple start, e.g. asserting that a bunch of tokens and nested tokens show up on the page. Then we can extend it with regression tests if we ever have to fix tests or add new features there. Want to open a follow-up for that?

  1. +++ b/src/TreeBuilder.php
    @@ -0,0 +1,179 @@
    +
    +  public function __construct(ContainerInterface $container) {
    +    $this->container = $container;
    +  }
    

    Please inject the services that we actually need.

  2. +++ b/src/TreeBuilder.php
    @@ -0,0 +1,179 @@
    +  public function buildTree($token_type, array $options = []) {
    +    $trees = &$this->builtTrees;
    

    weird drupal_static() leftover.

  3. +++ b/src/TreeBuilder.php
    @@ -0,0 +1,179 @@
    +      if (isset($token_info['children']) && is_array($token_info['children'])) {
    +        $result += static::flattenTree($token_info['children']);
    

    should be $this->flattenTree()

  4. +++ b/token.module
    @@ -625,143 +625,20 @@ function token_form_user_admin_settings_alter(&$form, FormStateInterface $form_s
     function token_flatten_tree($tree) {
    

    pretty sure those functions are just internal helper functions for the theme functions, I don't think we need to keep them around.

hussainweb’s picture

Status: Needs work » Needs review
FileSize
5.35 KB
12.36 KB

Addressing comments in #4. I am still manually testing this but putting this up for review.

hussainweb’s picture

Okay, I tested this manually across various pages (including other contrib modules). It works great on all pages.

hussainweb’s picture

  • Berdir committed bde07c5 on 8.x-1.x authored by hussainweb
    Issue #2641076 by hussainweb: Move token builder functions to a service
    
Berdir’s picture

Status: Needs review » Fixed

Committed. Nice, one step closer to unit testable code.

I think you're already working on converting the remaining tree theme code there to twig where you will probably convert these calls. But lets also do a follow-up to convert all other calls to this function, including properly injecting the service into controllers and so on.

hussainweb’s picture

Status: Fixed » Closed (fixed)

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