You are here

function _scheduler_translation_validate in Scheduler 8

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

Validation handler for language_content_settings_form.

If the content type is translatable and the field is 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 330
Scheduler publishes and unpublishes nodes on dates specified by the user.

Code

function _scheduler_translation_validate($form, FormStateInterface $form_state) {
  $content_types = $form_state
    ->getValues()['settings']['node'];
  $publishing_enabled_types = array_keys(_scheduler_get_scheduler_enabled_node_types('publish'));
  $unpublishing_enabled_types = array_keys(_scheduler_get_scheduler_enabled_node_types('unpublish'));
  $enabled = [];
  foreach ($content_types as $name => $settings) {
    $enabled['publish_on'] = in_array($name, $publishing_enabled_types);
    $enabled['unpublish_on'] = in_array($name, $unpublishing_enabled_types);
    if ($settings['translatable'] && ($enabled['publish_on'] || $enabled['unpublish_on'])) {
      $params = [
        '@type' => $form['settings']['node'][$name]['settings']['#label'],
        '@status' => $form['settings']['node'][$name]['fields']['status']['#label'],
      ];
      foreach ([
        'publish_on',
        'unpublish_on',
      ] as $var) {
        $mismatch = $enabled[$var] && $settings['fields'][$var] != $settings['fields']['status'];
        if ($mismatch) {
          $params['@scheduler_field'] = $form['settings']['node'][$name]['fields'][$var]['#label'];
          $message = t("Content type '@type' - Translatable settings for status field '@status' and Scheduler field '@scheduler_field' should match, either both on or both off", $params);
          $form_state
            ->setErrorByName("settings][node][{$name}][fields][status", $message);
          $form_state
            ->setErrorByName("settings][node][{$name}][fields][{$var}", $message);
        }
      }
    }
  }
}