Skip to content

Instantly share code, notes, and snippets.

@chx
Last active June 12, 2023 15:41
Show Gist options
  • Save chx/d63ed7405b47255a0d4481d60dd33641 to your computer and use it in GitHub Desktop.
Save chx/d63ed7405b47255a0d4481d60dd33641 to your computer and use it in GitHub Desktop.
name: 'Scale Language Content Settings'
type: module
description: 'Scale Language Content Settings form'
core: 8.x
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
/**
* Implements hook_form_FORM_ID_alter().
*/
function slcs_form_language_content_settings_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// This is a separate module because content_translation weighs itself to 10.
// The form can be simplified to just a single bundle because every validation
// and submit handler will iterate the settings from the form instead of the
// various central registries.
$query = \Drupal::request()->query->all();
$entityTypeId = $query['entity_type'] ?? '';
if (isset($form['settings'][$entityTypeId])) {
$form['back'] = [
'#type' => 'link',
'#title' => t('Back'),
'#url' => Url::fromRoute('<current>'),
'#weight' => -1,
];
$bundle = $query['bundle'] ?? '';
if (isset($form['settings'][$entityTypeId][$bundle])) {
_slcs_keep($form['entity_types']['#options'], $entityTypeId);
_slcs_keep($form['settings'], $entityTypeId);
_slcs_keep($form['settings'][$entityTypeId], $bundle);
$form['settings'][$entityTypeId]['#title'] .= sprintf(' (%s)', $bundle);
return;
}
else {
$element = $form['settings'][$entityTypeId];
foreach (Element::children($element) as $bundle) {
$label = $element[$bundle]['settings']['#label'];
$queries[sprintf('%s (%s)', $label, $bundle)] = [
'entity_type' => $entityTypeId,
'bundle' => $bundle,
];
}
}
}
else {
foreach ($form['entity_types']['#options'] as $entityTypeId => $label) {
$queries[(string) $label] = [
'entity_type' => $entityTypeId,
];
}
}
_slcs_keep($form, 'back');
// Suppress the paragraph error message.
$form['settings']['#access'] = FALSE;
ksort($queries);
foreach ($queries as $label => $query) {
$items[] = Link::fromTextAndUrl($label, Url::fromRoute('<current>', [], ['query' => $query]));
}
$form['list'] = [
'#theme' => 'item_list',
'#items' => $items,
];
}
function _slcs_keep(&$elements, $key) {
$keep = Element::properties($elements);
$keep[] = $key;
$elements = array_intersect_key($elements, array_flip($keep));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment