Show only one empty form element for a multivalue field; after it is filled show just the add another item button

Sometimes you want to allow multiple items but really only encourage the adding of just one, especially to keep the form clean. Or, in certain cases, this is necessary to prevent the ‘blank’ dates from getting saved or causing validation errors.

In a .module file like this one, put:

<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_BASE_FORM_ID_alter() for node_form.
 *
 *   - Remove open blank daterange form fields after one date is already saved.
 */
function drutopia_findit_program_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $bundle = $form_state->getFormObject()->getEntity()->bundle();
  if (!in_array($bundle, ['findit_program', 'findit_event'])) {
    return;
  }

  // Show an initial blank date, but do not show a blank date after that initial
  // one has been filled in.  Additional dates after the first one require a
  // service provider to press "Add another date".
  foreach(['field_findit_dates', 'field_findit_registration_dates'] as $field_name) {
    if (!empty($form[$field_name])) {
      $max_delta = $form[$field_name]['widget']['#max_delta'];
      if ($max_delta >= 1) {
        unset($form[$field_name]['widget'][$max_delta]);
        $form[$field_name]['widget']['#max_delta'] = $max_delta - 1;
      }
    }
  }

}