Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
registration
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
registration
Commits
1b790b44
Commit
1b790b44
authored
6 months ago
by
john.oltman
Committed by
john.oltman
6 months ago
Browse files
Options
Downloads
Patches
Plain Diff
#3487942
: Missing tokens
parent
ffc66af6
No related branches found
No related tags found
No related merge requests found
Pipeline
#340996
passed
6 months ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
registration.tokens.inc
+221
-0
221 additions, 0 deletions
registration.tokens.inc
tests/src/Kernel/RegistrationTokensTest.php
+77
-0
77 additions, 0 deletions
tests/src/Kernel/RegistrationTokensTest.php
with
298 additions
and
0 deletions
registration.tokens.inc
0 → 100644
+
221
−
0
View file @
1b790b44
<?php
/**
* @file
* Builds placeholder replacement tokens for registration-related data.
*/
use
Drupal\Core\Datetime\Entity\DateFormat
;
use
Drupal\Core\Language\LanguageInterface
;
use
Drupal\Core\Render\BubbleableMetadata
;
use
Drupal\user\Entity\User
;
/**
* Implements hook_token_info().
*/
function
registration_token_info
()
{
$types
[
'registration'
]
=
[
'name'
=>
t
(
'Registration'
),
'description'
=>
t
(
'Tokens related to registrations.'
),
'needs-data'
=>
'registration'
,
];
$registration
[
'id'
]
=
[
'name'
=>
t
(
'ID'
),
'description'
=>
t
(
'The unique identifier for the registration.'
),
];
$registration
[
'count'
]
=
[
'name'
=>
t
(
'Spaces'
),
'description'
=>
t
(
'The number of spaces reserved by the registration.'
),
];
$registration
[
'label'
]
=
[
'name'
=>
t
(
'Label'
),
'description'
=>
t
(
'The registration label.'
),
];
$registration
[
'mail'
]
=
[
'name'
=>
t
(
'Email'
),
'description'
=>
t
(
'The email address for the registration.'
),
];
$registration
[
'state'
]
=
[
'name'
=>
t
(
'Status'
),
'description'
=>
t
(
'The registration state.'
),
];
$registration
[
'type'
]
=
[
'name'
=>
t
(
'Type'
),
'description'
=>
t
(
'The registration type ID.'
),
];
$registration
[
'type-name'
]
=
[
'name'
=>
t
(
'Type name'
),
'description'
=>
t
(
'The registration type name.'
),
];
// Chained tokens for registrations.
$registration
[
'author'
]
=
[
'name'
=>
t
(
'Author'
),
'type'
=>
'user'
,
];
$node
[
'created'
]
=
[
'name'
=>
t
(
"Date created"
),
'type'
=>
'date'
,
];
$node
[
'changed'
]
=
[
'name'
=>
t
(
"Date changed"
),
'description'
=>
t
(
"The date the registration was most recently updated."
),
'type'
=>
'date'
,
];
$node
[
'completed'
]
=
[
'name'
=>
t
(
"Date completed"
),
'description'
=>
t
(
"The date the registration was completed."
),
'type'
=>
'date'
,
];
$registration
[
'entity'
]
=
[
'name'
=>
t
(
'Host entity'
),
'description'
=>
t
(
'The host entity for the registration.'
),
];
$registration
[
'user'
]
=
[
'name'
=>
t
(
'User'
),
'type'
=>
'user'
,
];
return
[
'types'
=>
$types
,
'tokens'
=>
[
'registration'
=>
$registration
],
];
}
/**
* Implements hook_tokens().
*/
function
registration_tokens
(
$type
,
$tokens
,
array
$data
,
array
$options
,
BubbleableMetadata
$bubbleable_metadata
)
{
$token_service
=
\Drupal
::
token
();
if
(
isset
(
$options
[
'langcode'
]))
{
$url_options
[
'language'
]
=
\Drupal
::
languageManager
()
->
getLanguage
(
$options
[
'langcode'
]);
$langcode
=
$options
[
'langcode'
];
}
else
{
$langcode
=
LanguageInterface
::
LANGCODE_DEFAULT
;
}
$replacements
=
[];
// The registration tokens.
if
(
$type
==
'registration'
&&
!
empty
(
$data
[
'registration'
]))
{
/** @var \Drupal\registration\Entity\RegistrationInterface $registration */
$registration
=
$data
[
'registration'
];
// Simple tokens.
foreach
(
$tokens
as
$name
=>
$original
)
{
switch
(
$name
)
{
case
'id'
:
$replacements
[
$original
]
=
$registration
->
id
();
break
;
case
'count'
:
$replacements
[
$original
]
=
$registration
->
getSpacesReserved
();
break
;
case
'label'
:
$replacements
[
$original
]
=
$registration
->
label
();
break
;
case
'mail'
:
$replacements
[
$original
]
=
$registration
->
getEmail
();
break
;
case
'state'
:
$replacements
[
$original
]
=
$registration
->
getState
()
?->
label
();
break
;
case
'type'
:
$replacements
[
$original
]
=
$registration
->
getType
()
->
id
();
break
;
case
'type-name'
:
$replacements
[
$original
]
=
$registration
->
getType
()
->
label
();
break
;
// Default values for the chained tokens handled below.
case
'author'
:
$account
=
$registration
->
getAuthor
()
?
$registration
->
getAuthor
()
:
User
::
load
(
0
);
$bubbleable_metadata
->
addCacheableDependency
(
$account
);
$replacements
[
$original
]
=
$account
->
label
();
break
;
case
'created'
:
$date_format
=
DateFormat
::
load
(
'medium'
);
$bubbleable_metadata
->
addCacheableDependency
(
$date_format
);
$replacements
[
$original
]
=
\Drupal
::
service
(
'date.formatter'
)
->
format
(
$registration
->
getCreatedTime
(),
'medium'
,
''
,
NULL
,
$langcode
);
break
;
case
'changed'
:
$date_format
=
DateFormat
::
load
(
'medium'
);
$bubbleable_metadata
->
addCacheableDependency
(
$date_format
);
$replacements
[
$original
]
=
\Drupal
::
service
(
'date.formatter'
)
->
format
(
$registration
->
getChangedTime
(),
'medium'
,
''
,
NULL
,
$langcode
);
break
;
case
'completed'
:
if
(
$registration
->
isComplete
())
{
$date_format
=
DateFormat
::
load
(
'medium'
);
$bubbleable_metadata
->
addCacheableDependency
(
$date_format
);
$replacements
[
$original
]
=
\Drupal
::
service
(
'date.formatter'
)
->
format
(
$registration
->
getCompletedTime
(),
'medium'
,
''
,
NULL
,
$langcode
);
}
break
;
case
'entity'
:
if
(
$entity
=
$registration
->
getHostEntity
()
?->
getEntity
())
{
$bubbleable_metadata
->
addCacheableDependency
(
$entity
);
$replacements
[
$original
]
=
$entity
->
label
();
}
break
;
case
'user'
:
$account
=
$registration
->
getUser
()
?
$registration
->
getUser
()
:
User
::
load
(
0
);
$bubbleable_metadata
->
addCacheableDependency
(
$account
);
$replacements
[
$original
]
=
$account
->
label
();
break
;
}
}
// Author tokens at [registration:author:*].
if
(
$author_tokens
=
$token_service
->
findWithPrefix
(
$tokens
,
'author'
))
{
$data
=
[
'user'
=>
$registration
->
getAuthor
()];
$replacements
+=
$token_service
->
generate
(
'user'
,
$author_tokens
,
$data
,
$options
,
$bubbleable_metadata
);
}
// Date created tokens at [registration:created:*].
if
(
$created_tokens
=
$token_service
->
findWithPrefix
(
$tokens
,
'created'
))
{
$data
=
[
'date'
=>
$registration
->
getCreatedTime
()];
$replacements
+=
$token_service
->
generate
(
'date'
,
$created_tokens
,
$data
,
$options
,
$bubbleable_metadata
);
}
// Date changed tokens at [registration:changed:*].
if
(
$changed_tokens
=
$token_service
->
findWithPrefix
(
$tokens
,
'changed'
))
{
$data
=
[
'date'
=>
$registration
->
getChangedTime
()];
$replacements
+=
$token_service
->
generate
(
'date'
,
$changed_tokens
,
$data
,
$options
,
$bubbleable_metadata
);
}
// Date completed tokens at [registration:completed:*].
if
(
$completed_tokens
=
$token_service
->
findWithPrefix
(
$tokens
,
'completed'
))
{
$data
=
[
'date'
=>
$registration
->
getCompletedTime
()];
$replacements
+=
$token_service
->
generate
(
'date'
,
$completed_tokens
,
$data
,
$options
,
$bubbleable_metadata
);
}
// Host entity tokens at [registration:entity:*].
if
(
$entity_tokens
=
$token_service
->
findWithPrefix
(
$tokens
,
'entity'
))
{
if
(
$entity
=
$registration
->
getHostEntity
()
?->
getEntity
())
{
$entity_type_id
=
$entity
->
getEntityTypeId
();
$data
=
[
$entity_type_id
=>
$entity
];
$replacements
+=
$token_service
->
generate
(
$entity_type_id
,
$entity_tokens
,
$data
,
$options
,
$bubbleable_metadata
);
}
}
// User tokens at [registration:user:*].
if
(
$user_tokens
=
$token_service
->
findWithPrefix
(
$tokens
,
'user'
))
{
$data
=
[
'user'
=>
$registration
->
getUser
()];
$replacements
+=
$token_service
->
generate
(
'user'
,
$user_tokens
,
$data
,
$options
,
$bubbleable_metadata
);
}
}
return
$replacements
;
}
This diff is collapsed.
Click to expand it.
tests/src/Kernel/RegistrationTokensTest.php
0 → 100644
+
77
−
0
View file @
1b790b44
<?php
namespace
Drupal\Tests\registration\Kernel
;
use
Drupal\Core\Utility\Token
;
use
Drupal\Tests\registration\Traits\NodeCreationTrait
;
use
Drupal\Tests\registration\Traits\RegistrationCreationTrait
;
/**
* Tests registration tokens.
*
* @group registration
*/
class
RegistrationTokensTest
extends
RegistrationKernelTestBase
{
use
NodeCreationTrait
;
use
RegistrationCreationTrait
;
/**
* The token service.
*/
protected
Token
$tokenService
;
/**
* {@inheritdoc}
*/
protected
function
setUp
():
void
{
parent
::
setUp
();
$this
->
tokenService
=
$this
->
container
->
get
(
'token'
);
}
/**
* Tests token generation and chaining.
*/
public
function
testRegistrationTokens
()
{
$node
=
$this
->
createAndSaveNode
();
$registration
=
$this
->
createRegistration
(
$node
);
$registration
->
set
(
'anon_mail'
,
'test@example.org'
);
$registration
->
save
();
// Simple tokens.
$test_data
=
[
'[registration:id]'
=>
'1'
,
'[registration:count]'
=>
'1'
,
'[registration:label]'
=>
'Registration #1 for My event'
,
'[registration:mail]'
=>
'test@example.org'
,
'[registration:state]'
=>
'Pending'
,
'[registration:type]'
=>
'conference'
,
'[registration:type-name]'
=>
'Conference'
,
];
$token_data
=
[
'registration'
=>
$registration
,
];
foreach
(
$test_data
as
$token
=>
$expected_value
)
{
$token_replaced
=
$this
->
tokenService
->
replace
(
$token
,
$token_data
);
$this
->
assertEquals
(
$token_replaced
,
$expected_value
);
}
// Chained host entity tokens.
$test_data
=
[
'[registration:entity]'
=>
'My event'
,
'[registration:entity:nid]'
=>
'1'
,
'[registration:entity:title]'
=>
'My event'
,
'[registration:entity:type]'
=>
'event'
,
'[registration:entity:type-name]'
=>
'Event'
,
];
foreach
(
$test_data
as
$token
=>
$expected_value
)
{
$token_replaced
=
$this
->
tokenService
->
replace
(
$token
,
$token_data
);
$this
->
assertEquals
(
$token_replaced
,
$expected_value
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment