You are here

scheduled_publish.module in Scheduled Publish 8

Same filename and directory in other branches
  1. 8.3 scheduled_publish.module
  2. 8.2 scheduled_publish.module

File

scheduled_publish.module
View source
<?php

/**
 * @file
 * Contains scheduled_publish.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\scheduled_publish\Service\ScheduledPublishCron;

/**
 * Implements hook_help().
 */
function scheduled_publish_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the scheduled_publish module.
    case 'help.page.scheduled_publish':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module allows the user to create a scheduler for content moderation') . '</p>';
      return $output;
    default:
  }
}
function scheduled_publish_is_node_edit_form() : bool {
  $currentPath = \Drupal::service('path.current')
    ->getPath();
  if (is_numeric(strpos($currentPath, '/node/')) && (is_numeric(strpos($currentPath, '/edit')) || is_numeric(strpos($currentPath, '/add')))) {
    return TRUE;
  }
  return FALSE;
}
function scheduled_publish_get_fields($key) : array {
  $fields = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('node', $key);
  $scheduledFields = [];
  foreach ($fields as $fieldName => $field) {
    if (strpos($fieldName, 'field_') !== FALSE) {
      if ($field
        ->getType() === 'scheduled_publish') {
        $scheduledFields[] = $fieldName;
      }
    }
  }
  return $scheduledFields;
}

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

/**
 * Implements hook_cron().
 */
function scheduled_publish_cron() {

  /**
   * @var ScheduledPublishCron $scheduledPublishUpdate
   */
  $scheduledPublishUpdate = \Drupal::service('scheduled_publish.update');
  $scheduledPublishUpdate
    ->doUpdate();
}