openfed-8.x-8.5/modules/openfed_features/openfed_administration/openfed_administration.module
modules/openfed_features/openfed_administration/openfed_administration.module
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* @file
* Contains openfed_administration.module.
*/
/**
* Implements hook_form_alter().
*
* This will add a checkbox to the add content type form. When checked, Content
* Moderation, default language settings and permissions are added when
* creating the new Content Type.
*/
function openfed_administration_form_alter(array &$form, FormStateInterface $form_state, string $form_id) {
if ($form_id === 'node_type_add_form') {
$form['name'] += [
'#weight' => 0,
];
$form['type'] += [
'#weight' => 1,
];
$form['description'] += [
'#weight' => 2,
];
$form['copy_default_config'] = [
'#type' => 'checkbox',
'#title' => t('Apply the default config to the content type.'),
'#description' => t('Content moderation settings and permissions will be applied to the content type.'),
'#weight' => 3,
];
$form['additional_settings'] += [
'#weight' => 10,
];
// Disable preview by default.
$form['submission']['preview_mode']['#default_value'] = DRUPAL_DISABLED;
// Disable "Display author and date information" by default.
$form['display']['display_submitted']['#default_value'] = FALSE;
// Disable the promote option by default.
if (isset($form['workflow']['options']['#default_value']['promote'])) {
unset($form['workflow']['options']['#default_value']['promote']);
}
// Disable menus by default.
if (isset($form['menu']['menu_options'])) {
$form['menu']['menu_options']['#default_value'] = [];
}
if ($form['language']) {
$form['language'] += [
'#description' => "<i>" . t('Field(s) will be/are disabled when the "Apply default config" checkbox is set.') . "</i>",
'#states' => [
'disabled' => [
':input[name="copy_default_config"]' => ['checked' => TRUE],
],
],
];
}
array_unshift($form['actions']['save_continue']['#submit'], 'openfed_administration_node_type_add_form_submit');
}
}
/**
* Custom submit callback for node_type_add_form forms.
*/
function openfed_administration_node_type_add_form_submit(array &$form, FormStateInterface $form_state) {
if (!$form_state->getValue('copy_default_config')) {
return;
}
$bundle = $form_state->getFormObject()->getEntity();
// Create the content type for the installed languages.
$form_state->setValue([
'language_configuration',
'langcode',
], 'current_interface');
$form_state->setValue([
'language_configuration',
'content_translation',
], TRUE);
$bundle->save();
// Add content moderation to the new content type.
\Drupal::service('openfed_administration.content_type_manager')->addWorkflowToBundle($bundle->id());
// Enable translation for the new content type.
\Drupal::service('openfed_administration.content_type_manager')->enableTranslation($bundle->id());
// Add Permissions to the roles.
\Drupal::service('openfed_administration.content_type_manager')->assignPermissionsToRoles($bundle->id());
}
