You are here

function scheduled_publish_form_alter in Scheduled Publish 8.2

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

Implements hook_form_alter

File

./scheduled_publish.module, line 59
Contains scheduled_publish.module.

Code

function scheduled_publish_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (scheduled_publish_is_supported_edit_form()) {
    $formObject = $form_state
      ->getFormObject();
    if (!$formObject instanceof EntityFormInterface) {

      // Not an entity form, nothin to alter.
      return;
    }
    $entity = $formObject
      ->getEntity();
    $typeID = $entity
      ->bundle();
    $scheduledPublishFields = scheduled_publish_get_fields($entity
      ->getEntityTypeId(), $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\ModerationInformation $moderationInformationService */
      $moderationInformationService = \Drupal::service('content_moderation.moderation_information');
      if ($moderationInformationService
        ->isModeratedEntity($entity)) {

        /** @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;
        }
      }
    }
  }
}