Commit f40c6862 authored by Jonathan Smith's avatar Jonathan Smith
Browse files

Issue #3024715 by jonathan1055, xaqrox, xavier.masson, daniel.bosen, Leksat,...

Issue #3024715 by jonathan1055, xaqrox, xavier.masson, daniel.bosen, Leksat, PCate, timwood, mrshowerman, Berdir, TwiiK, mglaman: Hide scheduling state options if there are no valid transitions
parent d432dfa7
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\workflows\Transition;

@@ -121,6 +122,9 @@ function scheduler_content_moderation_integration_form_node_form_alter(&$form, F
  $form['publish_state']['#group'] = 'scheduler_settings';
  $form['unpublish_state']['#group'] = 'scheduler_settings';

  $form['publish_state']['widget']['#after_build'][] = '_scheduler_content_moderation_integration_hide_empty_state_options';
  $form['unpublish_state']['widget']['#after_build'][] = '_scheduler_content_moderation_integration_hide_empty_state_options';

  $config = \Drupal::config('scheduler.settings');
  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state->getFormObject()->getEntity()->type->entity;
@@ -131,6 +135,31 @@ function scheduler_content_moderation_integration_form_node_form_alter(&$form, F
  $form['unpublish_state']['#access'] = $type->getThirdPartySetting('scheduler', 'unpublish_enable', $config->get('default_unpublish_enable'));
}

/**
 * Form API callback: Hide state element if there are no options to select.
 */
function _scheduler_content_moderation_integration_hide_empty_state_options(array $element, FormStateInterface $form_state) {
  foreach (Element::getVisibleChildren($element) as $key) {
    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'.
      $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;
}

/**
 * Helper function for the scheduler moderation widget.
 *
+0 −2
Original line number Diff line number Diff line
@@ -110,7 +110,6 @@ class TransitionAccessTest extends SchedulerContentModerationBrowserTestBase {
      'moderation_state[0][state]' => 'draft',
      'publish_on[0][value][date]' => NULL,
      'publish_on[0][value][time]' => NULL,
      'publish_state[0]' => '_none',
    ];
    $this->drupalGet("$entityTypeId/{$entity->id()}/edit");
    $this->submitForm($edit, 'Save');
@@ -149,7 +148,6 @@ class TransitionAccessTest extends SchedulerContentModerationBrowserTestBase {
    $edit = [
      'unpublish_on[0][value][date]' => NULL,
      'unpublish_on[0][value][time]' => NULL,
      'unpublish_state[0]' => '_none',
    ];
    $this->drupalGet("$entityTypeId/{$entity->id()}/edit");
    $this->submitForm($edit, 'Save');