diff --git a/tests/src/Kernel/FieldTest.php b/tests/src/Kernel/FieldTest.php index 75dd367..51dd369 100644 --- a/tests/src/Kernel/FieldTest.php +++ b/tests/src/Kernel/FieldTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\token\Kernel; use Drupal\Component\Utility\Unicode; +use Drupal\contact\Entity\ContactForm; use Drupal\Core\Entity\Entity\EntityViewMode; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\Markup; @@ -17,6 +18,7 @@ use Drupal\field\Entity\FieldStorageConfig; use Drupal\filter\Entity\FilterFormat; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; +use Drupal\contact\Entity\Message; /** * Tests field tokens. @@ -35,7 +37,7 @@ class FieldTest extends KernelTestBase { * * @var array */ - public static $modules = ['node', 'text', 'field', 'filter']; + public static $modules = ['node', 'text', 'field', 'filter', 'contact']; /** * {@inheritdoc} @@ -201,4 +203,46 @@ class FieldTest extends KernelTestBase { 'test_field' => Markup::create(substr($value, 0, 50)), ]); } + + /** + * Test that tokens are properly created for an entity's base fields. + */ + public function testBaseFieldToken() { + // Create a new contact_message entity and verify that tokens are generated + // for its base fields. + $contact_form = ContactForm::create([ + 'id' => 'form_id', + ] + ); + $contact_form->save(); + + $entity = Message::create([ + 'contact_form' => 'form_id', + 'uuid' => '123', + 'langcode' => 'en', + 'name' => 'Test name', + 'mail' => 'Test mail', + 'subject' => 'Test subject', + 'message' => 'Test message', + 'copy' => FALSE, + ]); + $entity->save(); + + $this->assertTokens('contact_message', ['contact_message' => $entity], [ + 'uuid' => Markup::create('123'), + 'langcode' => Markup::create('English'), + 'name' => Markup::create('Test name'), + 'mail' => Markup::create('Test mail'), + 'subject' => Markup::create('Test subject'), + 'message' => Markup::create('Test message'), + 'copy' => 'Off', + ]); + + // Test the metadata of one of the tokens. + $tokenService = \Drupal::service('token'); + $token_info = $tokenService->getTokenInfo('contact_message', 'subject'); + $this->assertEqual($token_info['name'], 'Subject'); + $this->assertEqual($token_info['description'], 'Text (plain) field.'); + $this->assertEqual($token_info['module'], 'token'); + } } diff --git a/token.tokens.inc b/token.tokens.inc index f4774ed..d45c1c4 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -70,7 +70,13 @@ function token_token_info_alter(&$info) { $token_type = $entity_info->get('token_type'); if (!isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) { - continue; + // Define entity type that doesn't exist yet. + $info['types'][$entity_info->id()] = [ + 'name' => $entity_info->getLabel(), + 'description' => t('Tokens related to %entity entity.', ['%entity' => $entity_info->getLabel()]), + 'needs-data' => $entity_info->id(), + 'new-entity-type' => true, + ]; } // Add [entity:url] tokens if they do not already exist. @@ -373,30 +379,6 @@ function token_token_info() { 'dynamic' => TRUE, ); - // Provide field tokens for content entities. - $entity_manager = \Drupal::entityManager(); - $store_entities = array_filter($entity_manager->getDefinitions(), function($definition) { - return $definition->getGroup() == 'content'; - }); - - foreach ($store_entities as $entity_info) { - $info['types'][$entity_info->id()] = [ - 'name' => $entity_info->getLabel(), - 'description' => t('Tokens related to %entity entity.', ['%entity' => $entity_info->getLabel()]), - 'needs-data' => $entity_info->id() - ]; - $fields = array_filter($entity_manager->getFieldStorageDefinitions($entity_info->id()), function($field) { - return $field instanceof \Drupal\Core\Field\BaseFieldDefinition; - }); - foreach ($fields as $field) { - $info['tokens'][$entity_info->id()][$field->getName()] = array( - 'name' => $field->getLabel(), - 'description' => $field->getDescription() ? $field->getDescription() : '', - 'module' => 'token' - ); - } - } - return $info; } @@ -1189,16 +1171,19 @@ function field_token_info_alter(&$info) { $fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type_id); foreach ($fields as $field_name => $field) { - if (!($field instanceof FieldStorageConfigInterface)) { + // Ensure the tokens exist. + if (!isset($info['types'][$token_type])) { continue; } - // If a token already exists for this field, then don't add it. - if (isset($info['tokens'][$token_type][$field_name])) { + + // Ensure the token implements FieldStorageConfigInterface or was newly + // created. + if (!($field instanceof FieldStorageConfigInterface) && (empty($info['types'][$token_type]['new-entity-type']))) { continue; } - // Ensure the tokens exist. - if (!isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) { + // If a token already exists for this field, then don't add it. + if (isset($info['tokens'][$token_type][$field_name])) { continue; } @@ -1243,7 +1228,7 @@ function _token_field_label($entity_type, $field_name) { $bundle_instances = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle); if (isset($bundle_instances[$field_name])) { $instance = $bundle_instances[$field_name]; - $label = $instance->getLabel(); + $label = (string) $instance->getLabel(); $labels[$label] = isset($labels[$label]) ? ++$labels[$label] : 1; } } @@ -1290,15 +1275,10 @@ function field_tokens($type, $tokens, array $data = array(), array $options = ar // token. if ($entity->hasField($name) && _token_module($data['token_type'], $name) == 'token') { - // Do not continue if the field is empty or not a configurable field. + // Do not continue if the field is empty. if ($entity->get($name)->isEmpty()) { continue; } - $definition = $entity->getFieldDefinition($name); - if ((!$definition instanceof FieldConfigInterface) - && (!$definition instanceof BaseFieldDefinition)) { - continue; - } $display_options = 'token'; if (!$token_view_display) { // We don't have the token view display and should fall back on