You are here

custom_add_another.module in Custom add another 8

Same filename and directory in other branches
  1. 7 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.
 */
use Drupal\Core\Render\Element;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\field\FieldConfigInterface;

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function custom_add_another_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  foreach ($fields as $field_type => &$field) {
    if ($field instanceof FieldConfigInterface && empty($field
      ->getThirdPartySettings('custom_add_another'))) {
      $field
        ->setThirdPartySetting('custom_add_another', 'custom_add_another', '')
        ->setThirdPartySetting('custom_add_another', 'custom_remove', '')
        ->save();
    }
  }
}

/**
 * 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_storage_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\field\FieldConfigInterface $field */
  $field = $form_state
    ->getStorage()['field_config'];

  /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
  $field_storage = $form_state
    ->getFormObject()
    ->getEntity();
  if (!$field_storage
    ->isLocked()) {
    $form['custom_add_another'] = [
      '#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."),
      '#default_value' => $field
        ->getThirdPartySetting('custom_add_another', 'custom_add_another'),
      // Hidden when the 'Number of values' is not unlimited.
      '#states' => [
        'visible' => [
          ':input[name="cardinality"]' => [
            'value' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
          ],
        ],
      ],
    ];
    $form['custom_remove'] = [
      '#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' => $field
        ->getThirdPartySetting('custom_add_another', 'custom_remove'),
      // Hidden when the 'Number of values' is not unlimited.
      '#states' => [
        'visible' => [
          ':input[name="cardinality"]' => [
            'value' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
          ],
        ],
      ],
    ];
    array_unshift($form['actions']['submit']['#submit'], 'custom_add_another_third_party_settings_submit');
  }
}

/**
 * Submit callback that saves custom labels to field Third Party Settings.
 */
function custom_add_another_third_party_settings_submit($form, FormStateInterface $form_state) {

  /** @var \Drupal\field\FieldConfigInterface $field */
  $field = $form_state
    ->getStorage()['field_config'];
  $field
    ->setThirdPartySetting('custom_add_another', 'custom_add_another', $form_state
    ->getValue('custom_add_another'))
    ->setThirdPartySetting('custom_add_another', 'custom_remove', $form_state
    ->getValue('custom_remove'))
    ->save();
}

/**
 * Implements hook_field_widget_form_alter().
 */
function custom_add_another_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $field = $context['items']
    ->getFieldDefinition();
  if ($field instanceof FieldConfigInterface) {

    /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
    $field_storage = $field
      ->getFieldStorageDefinition();
    if ($field_storage
      ->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      if ($add_another_text = $field
        ->getThirdPartySetting('custom_add_another', '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'] = $add_another_text;
      }
      if ($remove_text = $field
        ->getThirdPartySetting('custom_add_another', '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'] = $remove_text;
      }
      if (!empty($element['#type']) && $element['#type'] == 'managed_file') {
        $element['#process'][] = 'custom_add_another_process_file_buttons_labels';
      }
    }
  }
}

/**
 * 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) {
    $child_element =& $variables['element'][$child];
    if (isset($child_element['#custom_add_another_value']) || isset($child_element['#custom_remove'])) {
      if (isset($child_element['#custom_add_another_value']) && isset($variables['element']['add_more']['#value']) && $variables['element']['add_more']['#value'] != t($child_element['#custom_add_another_value'])) {
        $variables['element']['add_more']['#value'] = t($child_element['#custom_add_another_value']);
      }
      if (isset($child_element['#custom_add_another_value']) && isset($variables['button']['#value']) && $variables['button']['#value'] != t($child_element['#custom_add_another_value'])) {
        $variables['button']['#value'] = t($child_element['#custom_add_another_value']);
      }
      if (isset($child_element['#custom_remove']) && isset($child_element['remove_button']['#value'])) {
        $child_element['remove_button']['#value'] = t($child_element['#custom_remove']);
      }
    }
  }
}

/**
 * Process element callback for multiple Managed file widgets.
 */
function custom_add_another_process_file_buttons_labels($element, FormStateInterface $form_state, $form) {
  if (isset($element['#custom_add_another_value']) || isset($element['#custom_remove'])) {
    if (isset($element['#custom_add_another_value']) && isset($element['upload_button']['#value']) && $element['upload_button']['#value'] != t($element['#custom_add_another_value'])) {
      $element['upload_button']['#value'] = t($element['#custom_add_another_value']);
    }
    if (isset($element['#custom_remove']) && isset($element['remove_button']['#value'])) {
      $element['remove_button']['#value'] = t($element['#custom_remove']);
    }
  }
  return $element;
}

Functions