diff --git a/src/Tests/TokenEntityTest.php b/src/Tests/TokenEntityTest.php index b0146b4..7045866 100644 --- a/src/Tests/TokenEntityTest.php +++ b/src/Tests/TokenEntityTest.php @@ -169,7 +169,7 @@ class TokenEntityTest extends TokenKernelTestBase { $context = new RenderContext(); $renderer->executeInRenderContext($context, function() use ($entity, $format) { $this->assertTokens('node', ['node' => $entity], [ - 'test_field' => new Markup('foo'), + 'test_field' => Markup::create('foo'), 'test_field:value' => 'foo', 'test_field:format' => $format->id(), ]); diff --git a/src/Tests/TokenTestTrait.php b/src/Tests/TokenTestTrait.php index e621c93..7eacb5c 100644 --- a/src/Tests/TokenTestTrait.php +++ b/src/Tests/TokenTestTrait.php @@ -36,7 +36,7 @@ trait TokenTestTrait { $this->assertTrue(preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected))); } else { - $this->assertIdentical($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected))); + $this->assertEqual($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected))); } } diff --git a/token.tokens.inc b/token.tokens.inc index e956526..a8fcb4f 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -1210,8 +1210,8 @@ function field_token_info_alter(&$info) { // Property tokens. foreach ($field->getPropertyDefinitions() as $property => $property_definition) { - $info['tokens'][$token_type][$field_name . ':' . $property] = [ - 'name' => new HtmlEscapedText($label), + $info['tokens']['entity_field__' . $field_name][$property] = [ + 'name' => (string) new HtmlEscapedText($label), 'description' => $description, 'module' => 'token', ]; @@ -1271,6 +1271,7 @@ function field_tokens($type, $tokens, array $data = array(), array $options = ar // inside field_attach_view(). unset($entity->_field_view_prepared); + $field_names = []; foreach ($tokens as $name => $original) { $split = explode(':', $name); $property_name = NULL; @@ -1283,6 +1284,7 @@ function field_tokens($type, $tokens, array $data = array(), array $options = ar // Replace the [entity:field-name] token only if token module added this // token. if ($entity->hasField($name) && _token_module($data['token_type'], $name) == 'token') { + $field_names[$name] = $name; // Do not continue if the field is empty or not a configurable field. if ($entity->get($name)->isEmpty() || !($entity->getFieldDefinition($name) instanceof FieldConfigInterface)) { @@ -1295,16 +1297,29 @@ function field_tokens($type, $tokens, array $data = array(), array $options = ar $field_output['#pre_render'][] = 'token_pre_render_field_token'; $replacements[$original] = drupal_render($field_output); } - else { - $replacements[$original] = $entity->get($name)->first()->{$property_name}; - } } } + foreach ($field_names as $field_name) { + $field_tokens = \Drupal::token()->findWithPrefix($tokens, $field_name); + $replacements += \Drupal::token()->generate('entity_field__' . $field_name, $field_tokens, ['field_item' => $entity->get($field_name)], $options, $bubbleable_metadata); + } + // Remove the cloned object from memory. unset($entity); } + // Field tokens. + if (strpos($type, 'entity_field__') === 0) { + list(, $field_name) = explode('entity_field__', $type); + + $field_item = $data['field_item']; + + foreach ($tokens as $name => $original) { + $replacements[$original] = $field_item->first()->{$name}; + } + } + return $replacements; }