You are here

function custom_add_another_field_widget_form_alter in Custom add another 8

Same name and namespace in other branches
  1. 7 custom_add_another.module \custom_add_another_field_widget_form_alter()

Implements hook_field_widget_form_alter().

File

./custom_add_another.module, line 96
Allows the 'Add another item' button text to be customised.

Code

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';
      }
    }
  }
}