function gutenberg_form_alter in Gutenberg 8.2
Same name and namespace in other branches
- 8 gutenberg.module \gutenberg_form_alter()
Implements hook_form_alter().
File
- ./
gutenberg.module, line 125 - Provides integration with the Gutenberg editor.
Code
function gutenberg_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/* TODO: Simplify large alter functions like this using specific classes:
* https://drupal.stackexchange.com/questions/238341
* @see content_moderation_form_alter()
*/
$form_object = $form_state
->getFormObject();
if ($form_object instanceof MediaForm && $form_object
->getOperation() === 'edit') {
_gutenberg_media_form_alter($form, $form_state, $form_id);
}
if ($form_id == 'node_type_edit_form' || $form_id == 'node_type_add_form') {
$config = \Drupal::service('config.factory')
->getEditable('gutenberg.settings');
$form['gutenberg'] = [
'#type' => 'details',
'#title' => t('Gutenberg experience'),
'#description' => '',
'#group' => 'additional_settings',
'#weight' => 999,
'enable_gutenberg_experience' => [
'#type' => 'checkbox',
'#title' => t('Enable Gutenberg experience'),
'#description' => t('Turn the node edit form into a full Gutenberg UI experience. At least one field of long text type is necessary.'),
'#default_value' => $config
->get($form['type']['#default_value'] . '_enable_full'),
],
];
$form['gutenberg']['categories'] = [
'#type' => 'hidden',
'#default_value' => [],
];
$example_code = '<code>
<pre>[
["core/heading", {}],
["core/paragraph", {"placeholder": "Insert text"}]
]</pre></code>';
$form['gutenberg']['gutenberg_template'] = [
'#type' => 'textarea',
'#description' => t('JSON structure of blocks. Example: @example_code', [
'@example_code' => Markup::create($example_code),
]),
'#title' => t('Template'),
'#default_value' => $config
->get($form['type']['#default_value'] . '_template'),
'#states' => [
'visible' => [
'input[name="enable_gutenberg_experience"]' => [
'checked' => TRUE,
],
],
],
];
/*
* TODO add validation on this.
* Enforcing the "None" lock if the template is empty.
*/
$form['gutenberg']['gutenberg_template_lock'] = [
'#type' => 'select',
'#title' => t('Template lock'),
'#description' => t('<code>All</code> will fully lock the page template, not able to delete, create or move blocks just edit the content.<br/><code>Insert</code> will allow moving blocks.'),
'#default_value' => $config
->get($form['type']['#default_value'] . '_template_lock'),
'#options' => [
'none' => t('None'),
'insert' => t('Insert'),
'all' => t('All'),
],
'#states' => [
'visible' => [
'input[name="enable_gutenberg_experience"]' => [
'checked' => TRUE,
],
],
],
];
$form['gutenberg']['allowed_blocks_details'] = [
'#type' => 'details',
'#title' => t('Allowed Gutenberg blocks'),
'#states' => [
'visible' => [
'input[name="enable_gutenberg_experience"]' => [
'checked' => TRUE,
],
],
],
];
$settings = UtilsController::getAllowedBlocks();
$blocks_settings = UtilsController::getBlocksSettings();
foreach ($settings['categories'] as $category) {
$category['reference'] = str_replace('/', '-', $category['reference']);
$form['gutenberg']['categories']['#default_value'][] = $category['reference'];
$form['gutenberg']['allowed_blocks_details'][$category['reference']] = [
'#type' => 'fieldset',
'#title' => $category['name'],
];
$options = [
$category['reference'] . '/all' => t('All'),
];
foreach ($category['blocks'] as $block) {
if (!in_array($block['id'], $blocks_settings['blacklist'])) {
$options[$block['id']] = $block['name'];
}
}
$default_values = array_combine(array_map(function ($block) {
return $block['id'];
}, $category['blocks']), array_map(function ($block) {
return $block['default'] ? $block['id'] : 0;
}, $category['blocks']));
$config_values = $config
->get($form['type']['#default_value'] . '_allowed_blocks');
$form['gutenberg']['allowed_blocks_details'][$category['reference']]['allowed_blocks_' . $category['reference']] = [
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => array_merge($default_values, $config_values ? $config_values : []),
];
}
$form['gutenberg']['allowed_drupal_blocks_details'] = [
'#type' => 'details',
'#title' => t('Allowed Drupal blocks'),
'#states' => [
'visible' => [
'input[name="enable_gutenberg_experience"]' => [
'checked' => TRUE,
],
],
],
];
$blockManager = \Drupal::service('plugin.manager.block');
$contextRepository = \Drupal::service('context.repository');
// Get blocks definition
// $definitions = $blockManager->getDefinitionsForContexts($contextRepository->getAvailableContexts());
$definitions = $blockManager
->getFilteredDefinitions('block_ui', $contextRepository
->getAvailableContexts());
$groups = $blockManager
->getGroupedDefinitions($definitions);
$form['gutenberg']['categories_drupal'] = [
'#type' => 'hidden',
'#default_value' => [],
];
$default_values = array_fill_keys(array_map(function ($key) {
return str_replace('drupalblock/', '', $key);
}, $settings['default_drupal_blocks']), TRUE);
foreach ($groups as $key => $blocks) {
$group_reference = preg_replace('@[^a-z0-9-]+@', '_', strtolower($key));
$options = [];
$input_default_values = [];
foreach ($blocks as $key_block => $block) {
if (!in_array('drupalblock/' . $key_block, $blocks_settings['blacklist'])) {
$options[$key_block] = $block['admin_label'];
if (isset($default_values[$key_block]) && $default_values[$key_block]) {
$input_default_values[$key_block] = $key_block;
}
}
}
if (count($options) > 0) {
$form['gutenberg']['categories_drupal']['#default_value'][] = $group_reference;
$form['gutenberg']['allowed_drupal_blocks_details'][$group_reference] = [
'#type' => 'fieldset',
'#title' => $key,
];
$config_values = $config
->get($form['type']['#default_value'] . '_allowed_drupal_blocks');
$form['gutenberg']['allowed_drupal_blocks_details'][$group_reference]['allowed_drupal_blocks_' . $group_reference] = [
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => array_merge($input_default_values, $config_values ? $config_values : []),
];
}
}
$form['#attached']['library'][] = 'gutenberg/admin';
$form['actions']['submit']['#submit'][] = '_gutenberg_node_type_form_submit';
if (isset($form['actions']['save_continue']['#submit'])) {
$form['actions']['save_continue']['#submit'][] = '_gutenberg_node_type_form_submit';
}
}
}