field_label_plurals.module in Field label plurals 7
Same filename and directory in other branches
Alter the field settings form to add a textfield for the singular and use those settings when preprocessing fields.
File
field_label_plurals.moduleView source
<?php
/**
* @file
* Alter the field settings form to add a textfield for the singular and use
* those settings when preprocessing fields.
*/
/**
* Implements hook_help().
*/
function field_label_plurals_help($path, $arg) {
switch ($path) {
case 'admin/help#field_label_plurals':
$output = '<h3>' . t('Field label plurals') . '</h3>';
$output .= '<p>' . t('Some fields can have multiple values. See the "Number of values" setting when editing a field. If that is set, then the <em>Field label plurals</em> module shows a "Label to use for a single value" setting right below the "Label" textfield.') . '</p>';
return $output;
}
}
/**
* Implements hook_form_FORM_ID_alter() for field_ui_field_edit_form().
*
* Add a textfield to the field instance settings form.
*/
function field_label_plurals_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
// Get the currently selected cardinality.
$cardinality = isset($form_state['values']) ? $form_state['values']['field']['cardinality'] : $form['#field']['cardinality'];
// Decide whether or not to show the singular field.
$show_singular = $cardinality > 1 || $cardinality == FIELD_CARDINALITY_UNLIMITED;
// Add the textfield if it should be shown.
// Also add it for graceful degradation, if it could be shown in the future.
if ($show_singular || empty($form['field']['cardinality']['disabled'])) {
// Textfield, wrapped in the field_label_plurals_singular div.
$form['instance']['field_label_plurals_singular'] = array(
'#prefix' => '<div id="field-label-plurals-singular">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Label to use for a single value'),
'#description' => t('Useful if a field can have one or more values and you want to change the label accordingly.'),
'#weight' => -15,
'#default_value' => isset($form['#instance']['field_label_plurals_singular']) ? $form['#instance']['field_label_plurals_singular'] : '',
);
// Use js-hide to hide it, if it shouldn't be visible.
if (!$show_singular) {
$form['instance']['field_label_plurals_singular']['#prefix'] = '<div id="field-label-plurals-singular" class="js-hide">';
}
// Add an #ajax callback to the cardinality selectbox to dynamically hide
// and show the singular textfield.
$form['field']['cardinality']['#ajax'] = array(
'callback' => 'field_label_plurals_cardinality_callback',
'wrapper' => 'field_label_plurals_singular',
);
}
}
/**
* Form API #ajax callback.
*
* Returns a fresh field_label_plurals_singular, hidden or shown, depending on
* the selected cardinality.
*/
function field_label_plurals_cardinality_callback($form, $form_state) {
if (isset($form['instance']['field_label_plurals_singular'])) {
return $form['instance']['field_label_plurals_singular'];
}
return array(
'#markup' => '<div id="field-label-plurals-singular"></div>',
);
}
/**
* Implements hook_preprocess_HOOK() for theme_field().
*
* Alter the label before displaying it.
*/
function field_label_plurals_preprocess_field(&$variables) {
$element =& $variables['element'];
if (count($variables['items']) === 1) {
// If the textformatter module is enabled we do another check.
if (module_exists('textformatter') && (!isset($variables['items'][0]['#items']) || count($variables['items'][0]['#items']) > 1)) {
return;
}
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
if (isset($instance['field_label_plurals_singular']) && trim($instance['field_label_plurals_singular']) != '') {
$variables['label'] = check_plain($instance['field_label_plurals_singular']);
$element['#title'] = $instance['field_label_plurals_singular'];
}
}
}
Functions
Name![]() |
Description |
---|---|
field_label_plurals_cardinality_callback | Form API #ajax callback. |
field_label_plurals_form_field_ui_field_edit_form_alter | Implements hook_form_FORM_ID_alter() for field_ui_field_edit_form(). |
field_label_plurals_help | Implements hook_help(). |
field_label_plurals_preprocess_field | Implements hook_preprocess_HOOK() for theme_field(). |