Commit 95e0d27a authored by Jonathan Smith's avatar Jonathan Smith Committed by Jonathan Smith
Browse files

Issue #3024715 by jonathan1055: Simplify options_without_none and do not remove _none

parent f40c6862
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
@@ -140,21 +140,14 @@ function scheduler_content_moderation_integration_form_node_form_alter(&$form, F
 */
function _scheduler_content_moderation_integration_hide_empty_state_options(array $element, FormStateInterface $form_state) {
  foreach (Element::getVisibleChildren($element) as $key) {
    // Hide the element if there are no options.
    if (empty($element[$key]['#options'])) {
      $element[$key]['#access'] = FALSE;
    }
    else {
      $options_without_none = array_filter($element[$key]['#options'], function ($option) {
        return $option != '_none';
      }, ARRAY_FILTER_USE_KEY);
      // Hide the element if the only option is 'none'.
      $options_without_none = array_diff_key($element[$key]['#options'], ['_none' => '']);
      $element[$key]['#access'] = (bool) count($options_without_none);

      // If there is only one option apart from 'none' then remove 'none'.
      if (count($options_without_none) === 1) {
        $element[$key]['#value'] = key($options_without_none);
        $element[$key]['#options'] = $options_without_none;
      }
    }
  }
  return $element;