You are here

function scheduled_publish_form_alter in Scheduled Publish 8

Same name and namespace in other branches
  1. 8.2 scheduled_publish.module \scheduled_publish_form_alter()

Implements hook_form_alter

File

./scheduled_publish.module, line 55
Contains scheduled_publish.module.

Code

function scheduled_publish_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (scheduled_publish_is_node_edit_form()) {
    $formObject = $form_state
      ->getFormObject();
    $entity = $formObject
      ->getEntity();
    $typeID = $entity
      ->bundle();
    $scheduledPublishFields = scheduled_publish_get_fields($typeID);
    if (!empty($scheduledPublishFields)) {
      $form['scheduled_publish_details'] = [
        '#type' => 'details',
        '#title' => t('Scheduled moderation'),
        '#description' => t('Scheduled change of the moderation state e.g (draft => published)'),
        '#weight' => -5,
        '#group' => 'advanced',
        '#access' => TRUE,
        '#open' => TRUE,
        '#tree' => TRUE,
      ];
      foreach ($scheduledPublishFields as $fieldName) {
        $form[$fieldName]['#group'] = 'scheduled_publish_details';
      }

      /** @var \Drupal\content_moderation\StateTransitionValidation $transitionValidationService */
      $transitionValidationService = \Drupal::service('content_moderation.state_transition_validation');
      $transitions = $transitionValidationService
        ->getValidTransitions($entity, \Drupal::currentUser());
      $states = [];
      foreach ($transitions as $key => $value) {
        $states[$transitions[$key]
          ->to()
          ->id()] = $transitions[$key]
          ->label();
      }
      foreach ($scheduledPublishFields as $scheduledPublishField) {
        $form[$scheduledPublishField]['widget'][0]['moderation_state']['#options'] = $states;
      }
    }
  }
}