field_label_plurals.module in Field label plurals 8
File
field_label_plurals.module
View source
<?php
use Drupal\Core\Entity\Entity\EntityViewDisplay;
function field_label_plurals_preprocess_field(&$variables) {
if (isset($variables['element']['#items']) && method_exists($variables['element']['#items'], 'count') && $variables['element']['#items']
->count() > 1) {
$data = $variables['element']['#items']
->getDataDefinition();
$plural = $data
->getThirdPartySetting('field_label_plurals', 'label_plural');
if ($plural) {
$variables['label'] = $plural;
}
}
}
function field_label_plurals_form_field_config_edit_form_alter(&$form, $form_state, $form_id) {
$entity = $form_state
->getFormObject()
->getEntity();
$form['third_party_settings']['field_label_plurals']['label_plural'] = [
'#type' => 'textfield',
'#title' => t('Label to use for multiple values'),
'#default_value' => $entity
->getThirdPartySetting('field_label_plurals', 'label_plural'),
'#size' => 60,
'#maxlength' => 180,
];
}