FormatWordingTrait.php in Synonyms 2.0.x
File
src/ProviderInterface/FormatWordingTrait.php
View source
<?php
namespace Drupal\synonyms\ProviderInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\synonyms\SynonymInterface;
trait FormatWordingTrait {
public function synonymFormatWording($synonym, ContentEntityInterface $entity, SynonymInterface $synonym_config, $service_id) {
$wording_type = \Drupal::config('synonyms.settings')
->get('wording_type');
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'];
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;
}
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;
}
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;
}
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);
}
}
public function formatWordingAvailableTokens() {
return [
'@synonym' => $this
->t('The synonym value'),
'@entity_label' => $this
->t('The label of the entity this synonym belongs to'),
'@field_label' => $this
->t('The lowercase label of the provider field'),
'@FIELD_LABEL' => $this
->t('The uppercase label of the provider field'),
];
}
}