Problem/Motivation

Current token support is handled by Token module. Provides basic field level and field component tokens.

This can be extended to fairly easily support name format tokens (I think).

i.e. something like this (orange boxes show the extended tokens)

Due to the nature of the implementation of the Token module, trying to implement multi-value lists could be difficult and maybe requires that the logic is duplicated from the Token module to the Name field module to maintain control.

Proposed resolution

To be decided.

Remaining tasks

User interface changes

API changes

Data model changes

Original post

Token API has been updated to consider cacheability metadata.
The change record for more information: Token replacement needs to consider cacheability metadata

Issue fork name-2539278

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

mbovan’s picture

Status: Active » Needs review
StatusFileSize
new1.11 KB

Added $bubbleable_metadata param in _name_tokens().

mbovan’s picture

Just removing caching for $account as it is not needed and removing set for optional parameters. Anyway, code inside _name_tokens() seems broken, calling deprecated methods etc.

alan d.’s picture

Maybe we should fully review the token area...

FILE: name.tokens.inc
--------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
--------------------------------------------------------------------------
 42 | WARNING | Variable $old_tokens is undefined.
 63 | WARNING | Unused variable $known_tokens.
--------------------------------------------------------------------------
alan d.’s picture

alan d.’s picture

lol, or actually port and implement the tokens:

/**
 * Implements hook_token_info().
 */
function _name_token_info() {}

 /**
  * Implements hook_tokens().
  */
function _name_tokens() {}
alan d.’s picture

Issue summary: View changes
StatusFileSize
new53.22 KB
alan d.’s picture

Category: Bug report » Task
Status: Needs review » Active
Parent issue: #2949574: [meta] Name Field Full Release »

Since token module provides most of the functionality and extending this could prove to be a bit of a quagmire, decoupling from #2949574: [meta] Name Field Full Release

The old code isn't actually triggered and support is provided indirectly via the Token module.

Support for using format settings per individual name items wasn't too bad, supporting list support was a nightmare as Token module switches token structure based on the cardinality.

karuna patel’s picture

#5 suggestion will work, create your own token and set name field in the token.
Here is another example of hook_token in d8.

/*
* implement hook_token_info()
*/
 function custom_token_info(){
  $types['name'] = array(
    'name' => t('Token for name'),
    'description' => t('name token')
  );	
  $tokens['name_f'] = array(
    'name' => t('Name'),
    'description' => t('Token for name')
  );
	
  return array(
    'types' => $types,
    'tokens' => array(
      'name' => $tokens
    )
  );	
}

/*
* implement hook_token()
*/
function custom_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata){
  $replacements = array();
  if($type == 'name') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name_f':

        $url = Url::fromRoute('<current>');
        $path = $url->getInternalPath();
       
        $path_args = explode('/', $path);
        if (isset($path_args[1])) {
          $first_argument = $path_args[1];
          $replacements[$original] = $first_argument;
        }
        break;
      }
    }
      return $replacements;
  }
}
alan d.’s picture

lol, yes. But no as per #7

Fairly good support via Token module and NameItem::propertyDefinitions()

https://cgit.drupalcode.org/name/tree/src/Tests/NameNodeTokenReplaceTest...

About 400-500 loc to reimplement all of the features from Token provides for free;

Or make Token module a dependency and call internal private functions that could break at any time;

Or updating the hook weight, a token alter and it was fairly easy to add new additional tokens to the list, albeit these would be limited to single field items:

i.e. field_token_info_alter()

      $field_token_name = $token_type . '-' . $field_name;
      $info['tokens'][$token_type][$field_name] = array(
        'name' => Html::escape($label),
        'description' => $description,
        'module' => 'token',
        // For multivalue fields the field token is a list type.
        'type' => $cardinality > 1 ? "list<$field_token_name>" : $field_token_name,
      );

i.e the FIELD_NAME:PROPERTY and FIELD_NAME:0:PROPERTY type tokens that change with cardinality (when will people stop doing this....)

bluegeek9’s picture

Title: Update Name Field with Token changes » Add formatted Name as Tokens

bluegeek9’s picture

Status: Active » Needs review

bluegeek9 changed the visibility of the branch 8.x-1.x to hidden.

bluegeek9’s picture

StatusFileSize
new84.27 KB
bluegeek9’s picture

Status: Needs review » Fixed
//www.flaticon.com/free-icons/thank-you Thank you for your contribution! Your continued support makes this project sustainable.
There are multiple ways to show appreciation for the work contributed to this project including:

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • bluegeek9 committed 70c656d6 on 8.x-1.x
    feat: #2539278 Add formatted Name as Tokens
    

Status: Fixed » Closed (fixed)

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