You are here

field_label_plurals.module in Field label plurals 8

Same filename and directory in other branches
  1. 7 field_label_plurals.module

File

field_label_plurals.module
View source
<?php

use Drupal\Core\Entity\Entity\EntityViewDisplay;

/**
 * Implements hook_preprocess_field().
 */
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;
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
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,
  ];
}