You are here

custom_add_another.module in Custom add another 7

Same filename and directory in other branches
  1. 8 custom_add_another.module

Allows the 'Add another item' button text to be customised.

File

custom_add_another.module
View source
<?php

/**
 * @file
 *   Allows the 'Add another item' button text to be customised.
 */

/**
 * Implements hook_field_info_alter().
 */
function custom_add_another_field_info_alter(&$info) {

  // Add the 'custom_add_another' instance setting to all field types.
  foreach ($info as $field_type => &$field_type_info) {
    $field_type_info += array(
      'instance_settings' => array(),
    );
    $field_type_info['instance_settings'] += array(
      'custom_add_another' => '',
      'custom_remove' => '',
    );
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add a textbox for the 'custom_add_another' instance settings on the 'Edit
 * field instance' form.
 */
function custom_add_another_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  $instance = $form['#instance'];
  if (empty($form['#field']['locked'])) {
    $form['instance']['custom_add_another'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom add another item button'),
      '#description' => t("If the number of items in this field is set to 'Unlimited' then you might get a button that allows you to 'Add another item'. You may customise the text for that button here, an empty value will just use the default value for the button text."),
      // Add a hint for the empty value if hint module is around.
      '#hint' => t('Add another item'),
      '#default_value' => isset($instance['custom_add_another']) ? $instance['custom_add_another'] : '',
      // Hidden when the 'Number of values' is not unlimited.
      '#states' => array(
        'visible' => array(
          ':input[name="field[cardinality]"]' => array(
            'value' => (string) FIELD_CARDINALITY_UNLIMITED,
          ),
        ),
      ),
    );
    $form['instance']['custom_remove'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom remove button'),
      '#description' => t("If the number of items in this field is set to 'Unlimited' then you might get a button that allows you to 'Remove'. You may customise the text for that button here, an empty value will just use the default value for the button text."),
      // Add a hint for the empty value if hint module is around.
      '#hint' => t('Remove this item'),
      '#default_value' => isset($instance['custom_remove']) ? $instance['custom_remove'] : '',
      // Hidden when the 'Number of values' is not unlimited.
      '#states' => array(
        'visible' => array(
          ':input[name="field[cardinality]"]' => array(
            'value' => (string) FIELD_CARDINALITY_UNLIMITED,
          ),
        ),
      ),
    );
  }
}

/**
 * Implements hook_field_widget_form_alter().
 */
function custom_add_another_field_widget_form_alter(&$element, &$form_state, $context) {
  $instance = module_exists('i18n_custom_add_another') ? i18n_string_object_translate('field_instance', $context['instance']) : $context['instance'];
  $field = $context['field'];
  if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
    if (!empty($instance['custom_add_another'])) {

      // Pop the add another value to be used on the add more button into the
      // widget here for use later by
      // custom_add_another_preprocess_field_multiple_value_form().
      $element['#custom_add_another_value'] = $instance['custom_add_another'];
    }
    if (!empty($instance['custom_remove'])) {

      // Pop the add another value to be used on the add more button into the
      // widget here for use later by
      // custom_add_another_preprocess_field_multiple_value_form().
      $element['#custom_remove'] = $instance['custom_remove'];
    }
  }
}

/**
 * Implements hook_preprocess_field_multiple_value_form().
 *
 * We look for a value that was placed there earlier by
 * custom_add_another_field_widget_form_alter() and change the add_more button
 * to use that.
 */
function custom_add_another_preprocess_field_multiple_value_form(&$variables) {
  foreach (element_children($variables['element']) as $child) {
    if (isset($variables['element'][$child]['#custom_add_another_value']) || isset($variables['element'][$child]['#custom_remove'])) {
      if (isset($variables['element'][$child]['#custom_add_another_value']) && isset($variables['element']['add_more']['#value']) && $variables['element']['add_more']['#value'] != t($variables['element'][$child]['#custom_add_another_value'])) {
        $variables['element']['add_more']['#value'] = t($variables['element'][$child]['#custom_add_another_value']);
      }
      if (isset($variables['element'][$child]['#custom_remove']) && isset($variables['element'][$child]['remove_button']['#value'])) {
        $variables['element'][$child]['remove_button']['#value'] = t($variables['element'][$child]['#custom_remove']);
      }
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * Support the field_collection_table module.
 */
function custom_add_another_preprocess_field_collection_table_multiple_value_fields(&$variables) {
  custom_add_another_preprocess_field_multiple_value_form($variables);
}