You are here

function _scheduler_translation_validate in Scheduler 2.x

Same name and namespace in other branches
  1. 8 scheduler.module \_scheduler_translation_validate()

Validation handler for language_content_settings_form.

For each entity type, if it is translatable and also enabled for Scheduler, but the translation setting for the publish_on / unpublish_on field does not match the 'published status' field setting then throw a validation error.

See also

https://www.drupal.org/project/scheduler/issues/2871164

1 string reference to '_scheduler_translation_validate'
scheduler_form_language_content_settings_form_alter in ./scheduler.module
Implements hook_form_FORM_ID_alter() for language_content_settings_form.

File

./scheduler.module, line 568
Scheduler publishes and unpublishes entities on dates specified by the user.

Code

function _scheduler_translation_validate($form, FormStateInterface $form_state) {
  $settings = $form_state
    ->getValues()['settings'];

  /** @var \Drupal\scheduler\SchedulerManager $scheduler_manager */
  $scheduler_manager = \Drupal::service('scheduler.manager');
  foreach ($settings as $entity_type => $content_types) {
    $publishing_enabled_types = $scheduler_manager
      ->getEnabledTypes($entity_type, 'publish');
    $unpublishing_enabled_types = $scheduler_manager
      ->getEnabledTypes($entity_type, 'unpublish');
    if (empty($publishing_enabled_types) && empty($publishing_enabled_types)) {
      continue;
    }
    $enabled = [];
    foreach ($content_types as $name => $options) {
      $enabled['publish_on'] = in_array($name, $publishing_enabled_types);
      $enabled['unpublish_on'] = in_array($name, $unpublishing_enabled_types);
      if ($options['translatable'] && ($enabled['publish_on'] || $enabled['unpublish_on'])) {
        $params = [
          '@entity' => $form['settings'][$entity_type]['#bundle_label'],
          '@type' => $form['settings'][$entity_type][$name]['settings']['#label'],
          '%status' => $form['settings'][$entity_type][$name]['fields']['status']['#label'],
        ];
        foreach ([
          'publish_on',
          'unpublish_on',
        ] as $var) {
          $mismatch = $enabled[$var] && $options['fields'][$var] != $options['fields']['status'];
          if ($mismatch) {
            $params['%scheduler_field'] = $form['settings'][$entity_type][$name]['fields'][$var]['#label'];
            $message = t("There is a problem with @entity '@type' - The translatable settings for status field '%status' and Scheduler field '%scheduler_field' should match, either both on or both off", $params);
            $form_state
              ->setErrorByName("settings][{$entity_type}][{$name}][fields][status", $message);
            $form_state
              ->setErrorByName("settings][{$entity_type}][{$name}][fields][{$var}", $message);
          }
        }
      }
    }
  }
}