diff --git a/token.tokens.inc b/token.tokens.inc index 45c2316..d6d8a3c 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -1228,18 +1228,6 @@ function field_token_info_alter(&$info) { $description = t('@type field.', $params); } - // Change the token type if it is for an entity reference field and if - // there is a token type defined for the referenced entity. - $entity_reference_field = false; - if ($field->getType() === 'entity_reference') { - $field_property_definition = $field->getPropertyDefinitions(); - $entity_type = $field_property_definition['entity']->getTargetDefinition()->getEntityTypeId(); - $entity_type = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity_type); - if (isset($info['types'][$entity_type])) { - $entity_reference_field = true; - } - } - $cardinality = $field->getCardinality(); $cardinality = ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || $cardinality > 3) ? 3 : $cardinality; $field_token_name = $token_type . '-' . $field_name; @@ -1247,22 +1235,9 @@ function field_token_info_alter(&$info) { 'name' => SafeMarkup::checkPlain($label), 'description' => $description, 'module' => 'token', - ); - - // The field type depends whether it is multivalued and if it's an entity - // reference field or not. - if ($cardinality > 1) { // For multivalue fields the field token is a list type. - $info['tokens'][$token_type][$field_name]['type'] = "list<$field_token_name>"; - } - else { - if ($entity_reference_field) { - // Token type is that of the referenced entity's type. - $info['tokens'][$token_type][$field_name]['type'] = $entity_type; - continue; - } - $info['tokens'][$token_type][$field_name]['type'] = $field_token_name; - } + 'type' => $cardinality > 1 ? "list<$field_token_name>" : $field_token_name, + ); // Field token type. $info['types'][$field_token_name] = [ @@ -1288,14 +1263,31 @@ function field_token_info_alter(&$info) { $info['tokens']["list<$field_token_name>"][$delta] = [ 'name' => t('@type type with delta @delta', ['@type' => SafeMarkup::checkPlain($label), '@delta' => $delta]), 'module' => 'token', - 'type' => $entity_reference_field ? $entity_type : $field_token_name, + 'type' => $field_token_name, + ]; + } + } + + // If the field is an entity reference field, add an intermediary token + // of the referenced entity's type. + $entity_reference_field = false; + if ($field->getType() === 'entity_reference') { + $field_property_definition = $field->getPropertyDefinitions(); + $entity_type = $field_property_definition['entity']->getTargetDefinition()->getEntityTypeId(); + $entity_type = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity_type); + if (isset($info['types'][$entity_type])) { + $entity_reference_field = true; + $info['tokens'][$field_token_name]['referenced'] = [ + 'name' => 'Referenced entity', + 'module' => 'token', + 'type' => $entity_type, ]; } } // Property tokens. foreach ($field->getPropertyDefinitions() as $property => $property_definition) { - if (is_subclass_of($property_definition->getClass(), 'Drupal\Core\TypedData\PrimitiveInterface')) { + if (is_subclass_of($property_definition->getClass(), 'Drupal\Core\TypedData\PrimitiveInterface') || $entity_reference_field) { $info['tokens'][$field_token_name][$property] = [ 'name' => $property_definition->getLabel(), 'description' => $property_definition->getDescription(), @@ -1430,20 +1422,24 @@ function field_tokens($type, $tokens, array $data = array(), array $options = ar $field_property_definition = $field_storage_definition->getPropertyDefinitions(); $referenced_token_type = $field_property_definition['entity']->getTargetDefinition()->getEntityTypeId(); - $parts = explode(':', $name, 3); - if (is_numeric($parts[1])) { - // Multivalued field. Token is [entity:field_name:0:value]. - $delta = $parts[1]; + $parts = explode(':', $name, 4); + if ((count($parts) >= 4) && is_numeric($parts[2])) { + // Multivalued field. Token is [entity:field_name:referenced:0:value]. + $delta = $parts[2]; if (count($field) <= $delta) { // No such delta for this field, abort token replacement. continue; } + $literal_text = $parts[3]; + } + elseif (count($parts) >= 3) { + // Single valued field. Token is [entity:field_name:referenced:value]. + $delta = 0; $literal_text = $parts[2]; } else { - // Single valued field. Token is [entity:field_name:value]. - $delta = 0; - $literal_text = $parts[1]; + // Token not of the form expected. + continue; } $field_name = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($referenced_token_type, TRUE);