Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Extract function for re-use #21778

Merged
merged 1 commit into from
Oct 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 55 additions & 43 deletions CRM/Core/EntityTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,49 +85,7 @@ protected function getTokenMetadata(): array {
else {
$this->tokensMetadata = $this->getBespokeTokens();
foreach ($this->getFieldMetadata() as $field) {
$field['audience'] = 'user';
if ($field['name'] === 'contact_id') {
// Since {contact.id} is almost always present don't confuse users
// by also adding (e.g {participant.contact_id)
$field['audience'] = 'sysadmin';
}
if (!empty($this->getTokenMetadataOverrides()[$field['name']])) {
$field = array_merge($field, $this->getTokenMetadataOverrides()[$field['name']]);
}
if ($field['type'] === 'Custom') {
// Convert to apiv3 style for now. Later we can add v4 with
// portable naming & support for labels/ dates etc so let's leave
// the space open for that.
// Not the existing quickform widget has handling for the custom field
// format based on the title using this syntax.
$parts = explode(': ', $field['label']);
$field['title'] = "{$parts[1]} :: {$parts[0]}";
$tokenName = 'custom_' . $field['custom_field_id'];
$this->tokensMetadata[$tokenName] = $field;
continue;
}
if (in_array($field['name'], $this->getExposedFields(), TRUE)) {
if (
($field['options'] || !empty($field['suffixes']))
// At the time of writing currency didn't have a label option - this may have changed.
&& !in_array($field['name'], $this->getCurrencyFieldName(), TRUE)
) {
$this->tokensMetadata[$field['name'] . ':label'] = $this->tokensMetadata[$field['name'] . ':name'] = $field;
$fieldLabel = $field['input_attrs']['label'] ?? $field['label'];
$this->tokensMetadata[$field['name'] . ':label']['name'] = $field['name'] . ':label';
$this->tokensMetadata[$field['name'] . ':name']['name'] = $field['name'] . ':name';
$this->tokensMetadata[$field['name'] . ':name']['audience'] = 'sysadmin';
$this->tokensMetadata[$field['name'] . ':label']['title'] = $fieldLabel;
$this->tokensMetadata[$field['name'] . ':name']['title'] = ts('Machine name') . ': ' . $fieldLabel;
$field['audience'] = 'sysadmin';
}
if ($field['data_type'] === 'Boolean') {
$this->tokensMetadata[$field['name'] . ':label'] = $field;
$this->tokensMetadata[$field['name'] . ':label']['name'] = $field['name'] . ':label';
$field['audience'] = 'sysadmin';
}
$this->tokensMetadata[$field['name']] = $field;
}
$this->addFieldToTokenMetadata($field, $this->getExposedFields());
}
foreach ($this->getHiddenTokens() as $name) {
$this->tokensMetadata[$name]['audience'] = 'hidden';
Expand Down Expand Up @@ -625,4 +583,58 @@ public function getActiveTokens(TokenValueEvent $e) {
return array_intersect($messageTokens[$this->entity], array_keys($this->getTokenMetadata()));
}

/**
* Add the token to the metadata based on the field spec.
*
* @param array $field
* @param array $exposedFields
* @param string $prefix
*/
protected function addFieldToTokenMetadata(array $field, array $exposedFields, $prefix = ''): void {
$field['audience'] = 'user';
if ($field['name'] === 'contact_id') {
// Since {contact.id} is almost always present don't confuse users
// by also adding (e.g {participant.contact_id)
$field['audience'] = 'sysadmin';
}
if (!empty($this->getTokenMetadataOverrides()[$field['name']])) {
$field = array_merge($field, $this->getTokenMetadataOverrides()[$field['name']]);
}
if ($field['type'] === 'Custom') {
// Convert to apiv3 style for now. Later we can add v4 with
// portable naming & support for labels/ dates etc so let's leave
// the space open for that.
// Not the existing quickform widget has handling for the custom field
// format based on the title using this syntax.
$parts = explode(': ', $field['label']);
$field['title'] = "{$parts[1]} :: {$parts[0]}";
$tokenName = 'custom_' . $field['custom_field_id'];
$this->tokensMetadata[$tokenName] = $field;
return;
}
$tokenName = $prefix ? ($prefix . '.' . $field['name']) : $field['name'];
if (in_array($field['name'], $exposedFields, TRUE)) {
if (
($field['options'] || !empty($field['suffixes']))
// At the time of writing currency didn't have a label option - this may have changed.
&& !in_array($field['name'], $this->getCurrencyFieldName(), TRUE)
) {
$this->tokensMetadata[$tokenName . ':label'] = $this->tokensMetadata[$field['name'] . ':name'] = $field;
$fieldLabel = $field['input_attrs']['label'] ?? $field['label'];
$this->tokensMetadata[$tokenName . ':label']['name'] = $field['name'] . ':label';
$this->tokensMetadata[$tokenName . ':name']['name'] = $field['name'] . ':name';
$this->tokensMetadata[$tokenName . ':name']['audience'] = 'sysadmin';
$this->tokensMetadata[$tokenName . ':label']['title'] = $fieldLabel;
$this->tokensMetadata[$tokenName . ':name']['title'] = ts('Machine name') . ': ' . $fieldLabel;
$field['audience'] = 'sysadmin';
}
if ($field['data_type'] === 'Boolean') {
$this->tokensMetadata[$tokenName . ':label'] = $field;
$this->tokensMetadata[$tokenName . ':label']['name'] = $field['name'] . ':label';
$field['audience'] = 'sysadmin';
}
$this->tokensMetadata[$tokenName] = $field;
}
}

}