You are here

acquia_contenthub_moderation.module in Acquia Content Hub 8.2

Provides a way to select moderation state during imports from Content Hub.

File

modules/acquia_contenthub_moderation/acquia_contenthub_moderation.module
View source
<?php

/**
 * @file
 * Provides a way to select moderation state during imports from Content Hub.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function acquia_contenthub_moderation_form_workflow_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $config_settings = \Drupal::configFactory()
    ->get('acquia_contenthub_moderation.settings');
  $states = $form['states_container']['states'];
  foreach ($states as $state => $value) {
    if (substr($state, 0, 1) === '#') {
      unset($states[$state]);
      continue;
    }
    $states[$state] = $states[$state]['state']['#markup'];
  }
  $workflow = $form['id']['#default_value'];
  $configured_import_state = $config_settings
    ->get("workflows.{$workflow}.moderation_state");
  $form['acquia_contenthub'] = [
    '#type' => 'details',
    '#title' => t("Acquia Content Hub: Import Moderation State"),
    '#open' => TRUE,
    '#collapsible' => 'FALSE',
  ];
  $form['acquia_contenthub']['import_moderation_state'] = [
    '#type' => 'select',
    '#title' => t('Import Moderation State'),
    '#options' => $states,
  ];
  if (!empty($configured_import_state)) {
    $form['acquia_contenthub']['import_moderation_state']['#default_value'] = $configured_import_state;
  }
  if (isset($form['actions']['submit'])) {
    $form['actions']['submit']['#submit'][] = 'acquia_contenthub_moderation_import_moderation_state_submit';
  }
}

/**
 * Submit handler for dealing with import moderation state.
 *
 * @param array $form
 *   The form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current form state.
 *
 * @throws \Exception
 */
function acquia_contenthub_moderation_import_moderation_state_submit(array &$form, FormStateInterface $form_state) {
  if (!$form_state
    ->hasValue('import_moderation_state')) {
    return;
  }
  $workflow = $form_state
    ->getValue('id');
  $import_state = $form_state
    ->getValue('import_moderation_state');
  $config_settings = \Drupal::configFactory()
    ->getEditable('acquia_contenthub_moderation.settings');
  $config_settings
    ->set("workflows.{$workflow}", [
    'moderation_state' => $import_state,
  ]);
  $config_settings
    ->save();
}

Functions