Let ECA models provide tokens by implementing hook_token_info

Created on 17 April 2024, 8 months ago
Updated 1 June 2024, 7 months ago

Problem/Motivation

Let site builders provide tokens from within ECA models.

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

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

Merge Requests

Comments & Activities

  • Issue created by @jurgenhaas
  • πŸ‡©πŸ‡ͺGermany mxh Offenburg

    Maybe (or maybe not) related project: https://www.drupal.org/project/token_eca_alter β†’

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    Nice, thanks for linking this @mxh as I din't know about that module yet. Looks like an interesting feature set, which is in some way related to this issue. But I guess this one could provide some additional functionality.

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    The ECA module seems like it could provide some basic events to inject into the token build and token render context and allow dynamic generation of tokens for use around the site. Token alter seems interesting but that only seems to be for existing tokens, not generating all new ones.

    These tokens are the traditional Drupal tokens, like [node:title].

    It would be an incredible boon to site builders if they could create these tokens with the same interface ECA has. Right now custom tokens have to be developed in code. In general, most custom tokens come about for a need to 'show this if not that', conditionally, which ECA shines at building out.

    Here is an example. I had an ask recently to 'change a string of text in content if the URL has a query parameter of X'.

    /**
     * Implements hook_tokens().
     */
    function demo_tokens_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
      $replacements = [];
    
      if ($type == 'mytokengroup') {
        $request = \Drupal::request();
        $filter = $request->get('filter', []);
        $cid = !empty($filter['cid']) ? (int) $filter['cid'] : 0;
    
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'signup_headline':
              $bubbleable_metadata->addCacheableDependency('url.query_args:filter');
    
              // basic logic example
              $text = match($cid) {
                1 => 'CID 1 specific text here',
                2 => 'CID 2 specific text here',
                3 => 'CID 3 specific text here',
                default => 'Unknown or unspecified CID - will default to content set in configuration.'
              };
    
              $replacements[$original] = $text;
              break;
    
            default:
              break;
          }
        }
      }
    
      return $replacements;
    }
    

    It was not hard to do, necessarily, but its just more time spent by developers and something still out of reach of site builders. ECA might be able to fulfill this need.

  • πŸ‡©πŸ‡ͺGermany mxh Offenburg

    I created a PoC in !420 and played around with that for a bit. Not sure whether that's a good solution path. Had to add a recursion level flag and implemented without a hook inside ECA's token service, as hook_tokens are a bit late and might require more implementation logic when using hook_tokens instead. At least it just needs one additional event, then you can use already existing actions of eca_base like "Token: set value". Attached a small example model to demonstrate how it could work.

  • Pipeline finished with Failed
    8 months ago
    Total: 396s
    #150367
  • Status changed to RTBC 8 months ago
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    This is looking good to me, and I've fixed the test failures.

    Now setting it to RTBC but would certainly be interested, what @kevinquillen thinks about it and if this approach satisfied his original proposal/request.

  • Pipeline finished with Success
    8 months ago
    Total: 503s
    #151041
  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    I can't seem to install or test this at the moment because it is on the 2.x branch which requires Drupal 10.3 and having trouble requiring it:

    Problem 1
        - Root composer.json requires drupal/eca dev-2.0.x -> satisfiable by drupal/eca[dev-2.0.x].
        - drupal/eca dev-2.0.x requires drupal/core ^10.3 || ^11 -> found drupal/core[10.3.x-dev, 11.x-dev] but these were not loaded, likely because it conflicts with another require.
    
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    This is a new feature and will most likely not land in ECA 1.1, so for testing, a fresh Drupal test site with 10.3 and ECA 2.0 would be beneficial. Maybe worth having such a test installation around, as there will be plenty of things to test in the coming weeks and months ;-) Drupal 10.3 is just around the corner anyways.

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    Yeah I have a small install for this but I think there is a conflict elsewhere. I'll have to do a whole new 10.3 stack.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen
  • πŸ‡»πŸ‡³Vietnam zipme_hkt

    Dear all,
    This new feature is interesting.

    i am the author of https://www.drupal.org/project/token_eca_alter β†’

    I discovered ECA and it is a great module for Drupal Site Builder and developers.
    I also created some other ECA for my projects. It will interesting if i can help for distribute to ECA core:

    My others ECA modules:
    https://www.drupal.org/project/views_extender β†’
    https://www.drupal.org/project/eca_helper β†’
    https://www.drupal.org/project/eca_condition β†’

  • Pipeline finished with Failed
    7 months ago
    Total: 212s
    #173285
  • Pipeline finished with Failed
    7 months ago
    Total: 213s
    #174067
  • Pipeline finished with Success
    7 months ago
    Total: 620s
    #175851
  • Pipeline finished with Skipped
    7 months ago
    #175860
  • Status changed to Fixed 7 months ago
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    I've tested this again and decided to merge, as the ECA 2.0 beta release is coming in the next couple of days. The solution path is a good one, and if we find any follow-up tasks, they can be addressed in separate issues.

  • Pipeline finished with Success
    7 months ago
    Total: 248s
    #176291
  • Pipeline finished with Failed
    7 months ago
    Total: 299s
    #176523
  • Pipeline finished with Failed
    7 months ago
    #176528
  • Pipeline finished with Failed
    7 months ago
    Total: 305s
    #176536
  • Pipeline finished with Failed
    7 months ago
    #176542
  • Pipeline finished with Failed
    7 months ago
    Total: 328s
    #179178
  • Pipeline finished with Failed
    7 months ago
    Total: 284s
    #180566
  • Pipeline finished with Failed
    7 months ago
    Total: 214s
    #180596
  • Pipeline finished with Failed
    7 months ago
    Total: 214s
    #180636
  • Pipeline finished with Failed
    7 months ago
    Total: 212s
    #180640
  • Pipeline finished with Failed
    7 months ago
    Total: 243s
    #181024
  • Pipeline finished with Failed
    7 months ago
    Total: 298s
    #181623
  • Pipeline finished with Failed
    7 months ago
    Total: 209s
    #181631
  • Pipeline finished with Failed
    7 months ago
    Total: 210s
    #181928
  • Pipeline finished with Failed
    7 months ago
    Total: 180s
    #181952
  • Pipeline finished with Failed
    7 months ago
    Total: 183s
    #181961
  • Pipeline finished with Failed
    7 months ago
    Total: 185s
    #181964
  • Pipeline finished with Failed
    7 months ago
    Total: 270s
    #182185
  • Pipeline finished with Failed
    7 months ago
    Total: 335s
    #182189
  • Pipeline finished with Success
    7 months ago
    Total: 247s
    #183555
  • Pipeline finished with Failed
    7 months ago
    Total: 362s
    #183658
  • Pipeline finished with Success
    7 months ago
    Total: 247s
    #183792
  • Pipeline finished with Failed
    7 months ago
    Total: 214s
    #183853
  • Pipeline finished with Failed
    7 months ago
    Total: 204s
    #185921
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Pipeline finished with Failed
    7 months ago
    Total: 205s
    #193157
  • Pipeline finished with Success
    7 months ago
    Total: 248s
    #194469
  • Pipeline finished with Success
    7 months ago
    Total: 241s
    #194517
  • Pipeline finished with Success
    7 months ago
    Total: 308s
    #194518
  • Pipeline finished with Success
    7 months ago
    #194870
  • Pipeline finished with Success
    7 months ago
    Total: 215s
    #194999
  • Pipeline finished with Success
    7 months ago
    Total: 277s
    #195052
  • Pipeline finished with Success
    7 months ago
    Total: 215s
    #195333
  • Pipeline finished with Success
    7 months ago
    Total: 219s
    #195802
  • Pipeline finished with Success
    7 months ago
    Total: 217s
    #195808
  • Pipeline finished with Success
    7 months ago
    Total: 219s
    #195809
  • Pipeline finished with Success
    7 months ago
    Total: 184s
    #196221
  • Pipeline finished with Success
    6 months ago
    Total: 336s
    #197617
  • Pipeline finished with Success
    6 months ago
    Total: 308s
    #197642
  • Pipeline finished with Success
    6 months ago
    Total: 372s
    #198376
  • Pipeline finished with Success
    6 months ago
    Total: 212s
    #198621
  • Pipeline finished with Canceled
    about 1 month ago
    Total: 72s
    #348435
  • Pipeline finished with Skipped
    about 1 month ago
    #348438
  • Pipeline finished with Failed
    about 1 month ago
    Total: 516s
    #348436
  • Pipeline finished with Failed
    28 days ago
    Total: 582s
    #351049
  • Pipeline finished with Failed
    28 days ago
    Total: 272s
    #351325
  • Pipeline finished with Success
    28 days ago
    Total: 678s
    #351329
  • Pipeline finished with Success
    27 days ago
    #352051
  • Pipeline finished with Success
    27 days ago
    Total: 604s
    #352107
  • Pipeline finished with Failed
    26 days ago
    Total: 751s
    #352874
  • Pipeline finished with Success
    20 days ago
    Total: 578s
    #358888
  • Pipeline finished with Success
    16 days ago
    Total: 610s
    #362540
  • Pipeline finished with Failed
    7 days ago
    Total: 754s
    #371514
  • Pipeline finished with Failed
    1 day ago
    Total: 679s
    #376792
  • Pipeline finished with Failed
    1 day ago
    Total: 771s
    #376859
  • Pipeline finished with Success
    about 20 hours ago
    Total: 2424s
    #377417
  • Pipeline finished with Failed
    about 16 hours ago
    Total: 942s
    #377510
Production build 0.71.5 2024