I had the impression that tokens for entities in Drupal 8 are provided by the core out of the box dues to the new OOP system of fields but it turned out I was wrong and the situation is the same as in Drupal 7.

I think that this is one of the most important features that should be present for Drupal 8 regarding tokens therefore if core is not taking care of it this module should do it automatically. It should not be node, user, taxonomy and file specific but it should be entity-wide. Fields, no matter if base fields or field config, provide all the data that is needed for making a list of available fields and their descriptions and also meta/data for replacing the tokens. Each field type that needs/has additional options(like date formatting) can be easily derived from the field type/information.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

ivanjaros created an issue. See original summary.

Maouna’s picture

Status: Active » Closed (duplicate)

Hi ivanjaros,
I totally agree with you. It is a pity that nobody else seems to be interested.
I think this issue is a duplicate. Please comment on these issues:

The general question: https://www.drupal.org/node/2469081
and also: https://www.drupal.org/node/2430827
Tokens for base fields: https://www.drupal.org/node/2493567
Chained tokens for field tokens: https://www.drupal.org/node/2493559

Anonymous’s picture

Anonymous’s picture

Status: Closed (duplicate) » Active

I've read the other issue and it is similar but not a duplicate of this one.
This is about providing tokens automatically for all entities by the core whereas the other issue is about recycling fields if they are attached to other entities.

Berdir’s picture

Status: Active » Closed (duplicate)

Maybe not a duplicate of those, but it is a duplicate of this: #2493567: Allow field tokens also for base fields

Anonymous’s picture

Status: Closed (duplicate) » Active

Berdir I fail to see the connection here. If I use dsm(\Drupal::token()->getInfo()); I do not see any of my custom content entities. If I define them in token_info hook then I will see them but no fields either so I have to do it all manually anyway.

This issue is about Core doing this on its own.

Berdir’s picture

Status: Active » Closed (duplicate)

I didn't say the patch there was complete, it's a first step. Still makes this a duplicate.

Yes, currently it only handles actually generating the tokens of course we also need to generate the token info, you're welcome to work on it.

Maouna’s picture

Status: Closed (duplicate) » Needs review
FileSize
982 bytes

@ivanjaros: I saw your patch at https://www.drupal.org/node/2493567#comment-10491838, and now I understand better what you want to achieve.
That's why I am moving back to this issue here.

In fact, in one of my custom modules, I do the same:

/**
 * Implements hook_token_info().
 */
function my_module_token_info() {
  $info = array();

  //Provide tokens for all entities
  $entity_types = \Drupal::entityManager()->getDefinitions();
  foreach ($entity_types as $entity_type_id => $entity_type) {
    if (!$entity_type->isSubclassOf('\Drupal\Core\Entity\ContentEntityInterface')) {
      continue;
    }
    $entity_type_name = $entity_type->getLabel();
    $info['types'][$entity_type_id] = [
      'name' => $entity_type_name,
      'description' => t('Tokens related to the @entity_type_name entity.', array('@entity_type_name' => $entity_type_name)),
      'needs-data' => $entity_type_id,
    ];
    $info['tokens'][$entity_type_id] = [];
  }

  return $info;
}

And your goal is to move this into token_token_info(). Is that correct?

What I still do not understand from your patch, why do you remove the additional tokens defined in token_token_info()? They are still valid.

Because of the field tokens provided by field_token_info_alter(&$info) and field_tokens(), every entity has then at least its fields available as tokens.

So, as far as I understand, this patch should be sufficient:

Status: Needs review » Needs work

The last submitted patch, 8: 2560191-entity_tokens-8.patch, failed testing.

Berdir’s picture

Status: Needs work » Closed (duplicate)

I think that snippet should also be added to your patch in #2493567: Allow field tokens also for base fields. And we should also do it in field_token_info_alter() so that we avoid doing it if that type is already defined.

And yes, agreed. Removing hardcoded tokens when this works would be a separate follow-up and it will not work for everything, not all of those are simple field based tokens.