public function FormatWordingTrait::synonymFormatWording in Synonyms 2.0.x
Format a synonym into wording as requested by configuration.
Parameters
string $synonym: Synonym that should be formatted.
\Drupal\Core\Entity\ContentEntityInterface $entity: Entity to which this synonym belongs.
\Drupal\synonyms\SynonymInterface $synonym_config: Synonym config entity in the context of which it all happens.
string $service_id: The behavior service (widget) ID.
Return value
string Formatted wording
File
- src/
ProviderInterface/ FormatWordingTrait.php, line 28
Class
- FormatWordingTrait
- Trait to format wording of a synonym.
Namespace
Drupal\synonyms\ProviderInterfaceCode
public function synonymFormatWording($synonym, ContentEntityInterface $entity, SynonymInterface $synonym_config, $service_id) {
// @todo Maybe we should use tokens replacement here? But then it would mean
// an extra dependency on the tokens module. Is it worth it? For now let's
// use stupid str_replace() and incorporate tokens only if user base really
// asks for it.
$wording_type = \Drupal::config('synonyms.settings')
->get('wording_type');
// If the wording type is 'No wording' it's simple.
if ($wording_type == 'none') {
return $synonym;
}
else {
$wording = '';
$plugin_definition = $synonym_config
->getProviderPluginInstance()
->getPluginDefinition();
$entity_type = $plugin_definition['controlled_entity_type'];
$bundle = $plugin_definition['controlled_bundle'];
// Try widget's wording per entity type and provider.
if ($wording_type == 'provider') {
$provider_configuration = $synonym_config
->getProviderConfiguration();
if (isset($provider_configuration['wording'])) {
$get_wording = $provider_configuration['wording'];
}
$wording = !empty($get_wording) ? $get_wording : $wording;
}
// Try widget's wording per entity type.
if (empty($wording) && ($wording_type == 'provider' || $wording_type == 'entity')) {
$get_wording = \Drupal::config('synonyms_' . $service_id . '.behavior.' . $entity_type . '.' . $bundle)
->get('wording');
$wording = !empty($get_wording) ? $get_wording : $wording;
}
// Try default widget's wording and if it is empty as well
// fallback to basic '@synonym' wording.
if (empty($wording) && ($wording_type == 'provider' || $wording_type == 'entity' || $wording_type == 'default')) {
$get_wording = \Drupal::config('synonyms_' . $service_id . '.settings')
->get('default_wording');
$wording = !empty($get_wording) ? $get_wording : $wording;
}
// Ultimate fallback if all other wordings are empty.
if (empty($wording)) {
$wording = '@synonym';
}
$definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $bundle);
$field_label = $definitions[$plugin_definition['field']]
->getLabel();
$map = [
'@synonym' => $synonym,
'@entity_label' => $entity
->label(),
'@field_label' => strtolower($field_label),
'@FIELD_LABEL' => strtoupper($field_label),
];
return str_replace(array_keys($map), array_values($map), $wording);
}
}